<table> 專為表格化資料設計,非佈局工具。正確使用語義化表格標籤,能確保螢幕閱讀器正確朗讀、搜尋引擎理解資料結構、CSS 精準樣式控制。本文從基礎骨架到無障礙進階,完整掌握表格建構。
<table>
<tr>
<td>儲存格 1-1</td>
<td>儲存格 1-2</td>
<td>儲存格 1-3</td>
</tr>
<tr>
<td>儲存格 2-1</td>
<td>儲存格 2-2</td>
<td>儲存格 2-3</td>
</tr>
</table>
<table>
<caption>2024 年 Q1 部門營收統計 (單位:萬元)</caption>
<thead>
<tr>
<th scope="col">部門</th>
<th scope="col">1 月</th>
<th scope="col">2 月</th>
<th scope="col">3 月</th>
<th scope="col">小計</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">業務部</th>
<td>120</td>
<td>135</td>
<td>142</td>
<td>397</td>
</tr>
<tr>
<th scope="row">技術部</th>
<td>200</td>
<td>210</td>
<td>225</td>
<td>635</td>
</tr>
<tr>
<th scope="row">行政部</th>
<td>80</td>
<td>85</td>
<td>90</td>
<td>255</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">總計</th>
<td>400</td>
<td>430</td>
<td>457</td>
<td>1287</td>
</tr>
</tfoot>
</table>
| 標籤 | 全名 | 關鍵屬性 | 說明 |
|---|---|---|---|
<table> | table | - | 表格容器,不建議用 layout 屬性 |
<caption> | caption | - | 表格標題/說明,緊接 table 之後,螢幕閱讀器優先朗讀 |
<thead> | table head | - | 表頭區塊,列印時每頁重複 |
<tbody> | table body | - | 主體資料區,可多個分組 |
<tfoot> | table foot | - | 表尾摘要區,列印每頁重複,建議放在 tbody 前或後 |
<tr> | table row | - | 列容器 |
<th> | table header | scope="col|row|colgroup|rowgroup" | 表頭儲存格,**scope 必填** 告知螢幕閱讀器範圍 |
<td> | table data | colspan, rowspan, headers | 資料儲存格 |
<colgroup> | column group | span | 欄群組,統一設定寬度/樣式 |
<col> | column | span, width | 單一欄設定,隱藏元素 |
<table>
<tr>
<th rowspan="2">類別</th>
<th colspan="2">2024 上半年</th>
<th colspan="2">2024 下半年</th>
</tr>
<tr>
<th>Q1</th> <th>Q2</th> <th>Q3</th> <th>Q4</th>
</tr>
<tr>
<td>產品 A</td> <td>100</td> <td>120</td> <td>130</td> <td>140</td>
</tr>
</table>
colspan="n":橫向跨併 n 欄rowspan="n":縱向跨併 n 列scope="col" (欄標題) / scope="row" (列標題) / scope="colgroup" / scope="rowgroup"<th id="dept-sales"> + <td headers="dept-sales q1">table { display: block; overflow-x: auto; } 或重構為卡片列表/*** 基礎表格樣式 ***/
.table {
width: 100%;
border-collapse: collapse; /* 合併邊框,避免雙線 */
font-size: 0.9rem;
}
.table th,
.table td {
border: 1px solid #dee2e6;
padding: 0.75rem;
text-align: left;
vertical-align: middle;
}
.table thead th {
background-color: #f8f9fa;
font-weight: 600;
white-space: nowrap; /* 表頭不換行 */
}
.table tbody tr:nth-child(even) {
background-color: rgba(0,0,0,.03); /* 斑馬紋 */
}
.table tbody tr:hover {
background-color: rgba(0,123,255,.08); /* 懸停高亮 */
}
.table caption {
caption-side: bottom; /* 標題置底 */
padding: 0.5rem;
font-style: italic;
color: #6c757d;
}
/*** 響應式:小螢幕橫向捲動 ***/
@media (max-width: 767px) {
.table-responsive {
display: block;
width: 100%;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
.table-responsive > .table {
min-width: 600px; /* 保證最小可讀寬度 */
}
}
| ✅ 適合使用 table | ❌ 不適合使用 table |
|---|---|
| 財務報表、統計資料、價格比較表 | 頁面整體佈局(header、sidebar、footer) |
| 時刻表、課程表、賽程表 | 導航選單、按鈕群組 |
| 產品規格對比、參數表 | 卡片式商品列表 |
| 表單密集輸入區(如試算表風格) | 文章內容排版 |
| 日曆/日期選擇器 | 任何非表格化資料呈現 |
<Table> 組件封裝表頭、分頁、排序、篩選邏輯columns 定義欄位渲染函數,data 陣列傳入資料react-window / vue-virtual-scrollerSheetJS (xlsx) 或 csv-stringify 一鍵下載表格是資訊密度最高的 UI 元件之一。遵循語義化結構、無障礙屬性、響應式策略,配合現代 CSS 與框架封裝,即能打造專業、可訪問、高效能的資料呈現體驗!