HTML 表格由一組語意明確的標籤組成,正確使用這些標籤不僅能建立清晰的文件結構,更能提升無障礙體驗、SEO 效果與 CSS 樣式控制彈性。本文系統整理所有表格相關標籤的語意、屬性、巢狀規則、無障礙屬性與最佳實踐範例。
表格標籤速查總表
| 標籤 | 語意 | 父層容器 | 子元素 | 關鍵屬性 | 是否必須 |
<table> | 表格根容器 | 流式內容容器 | `caption`, `colgroup`, `thead`, `tbody`, `tfoot`, `tr` | `border`(已廢棄), `width`(已廢棄) | 是 |
<caption> | 表格標題/說明 | `table` (第一個子元素) | 流式內容 | — | 建議有 |
<colgroup> | 欄群組 | `table` (在 `caption` 後、其它前) | `col` | `span` | 選用 |
<col> | 單欄定義 | `colgroup` (或直接在 `table`) | 無 (空標籤) | `span`, `width` (CSS), `style` | 選用 |
<thead> | 表頭區塊 | `table` (在 `caption`/`colgroup` 後、 `tbody` 前) | `tr` | — | 建議有 |
<tbody> | 表身區塊 | `table` (在 `thead` 後、 `tfoot` 前) | `tr` | — | 隱性存在 (省略標籤時瀏覽器自動產生) |
<tfoot> | 表尾區塊 (總計、摘要) | `table` (在 `caption`/`colgroup`/`thead` 後、 `tbody` 前或後) | `tr` | — | 建議有 |
<tr> | 表格列 | `thead`, `tbody`, `tfoot` | `td`, `th` | — | 是 |
<th> | 表頭儲存格 | `tr` | 流式內容 | scope (col/row/colgroup/rowgroup), `colspan`, `rowspan`, `abbr`, `headers` | 表頭列必須 |
<td> | 資料儲存格 | `tr` | 流式內容 | `colspan`, `rowspan`, `headers` | 資料列必須 |
標準表格結構範例
<table class="data-table">
<caption>2024 Q1 部門營收統計 (單位:萬元)</caption>
<colgroup>
<col style="width: 20%"> <!-- 部門 -->
<col style="width: 20%"> <!-- 1月 -->
<col style="width: 20%"> <!-- 2月 -->
<col style="width: 20%"> <!-- 3月 -->
<col style="width: 20%"> <!-- 合計 -->
</colgroup>
<thead>
<tr>
<th scope="row">部門</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>140</td><td>395</td>
</tr>
<tr>
<th scope="row">技術部</th>
<td>200</td><td>210</td><td>220</td><td>630</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">總計</th>
<td>320</td><td>345</td><td>360</td><td>1025</td>
</tr>
</tfoot>
</table>
關鍵無障礙屬性詳細說明
th scope 屬性
| scope 值 | 適用情境 | 範例 |
col | 欄表頭 (單欄) | ` | 1 月 | `
row | 列表頭 (單列) | ` | 業務部 | `
colgroup | 跨欄表頭 (`colspan>1`) | ` | Q1 | `
rowgroup | 跨列表頭 (`rowspan>1`) | ` | 上半年 | `
headers + id 機制(複雜表格專用)
<thead>
<tr>
<th id="h-dept" scope="row">部門</th>
<th id="h-jan" scope="col">1月</th>
<th id="h-feb" scope="col">2月</th>
</tr>
</thead>
<tbody>
<tr>
<th id="r-sales" scope="row">業務部</th>
<td headers="h-jan r-sales">120</td>
<td headers="h-feb r-sales">135</td>
</tr>
</tbody>
每個 `
` 的 `headers` 列出對應的表頭 `id`(空白分隔),支援任意複雜的巢狀/跨行跨列關聯。
CSS 選擇器最佳實踐
/* 結構選擇器,不依賴 class */
.data-table caption { caption-side: top; font-weight: 600; padding: 1rem; color: #333; }
.data-table colgroup col { /* 欄寬、背景色 */ }
.data-table thead th { background: #f5f5f5; font-weight: 600; }
.data-table tbody tr:nth-child(even) td { background: #fafafa; }
.data-table tbody tr:hover td { background: #e8f4fd; }
.data-table tfoot th, .data-table tfoot td { font-weight: 600; background: #f1f3f5; border-top: 2px solid #ddd; }
/* 無障礙:焦點可見 */
.data-table td:focus, .data-table th:focus { outline: 2px solid #3498db; outline-offset: -2px; }
常見錯誤與修正
- ❌ 省略 `` 標籤
- 瀏覽器會自動補上,但 CSS 選擇器 `tbody tr` 可能失效(視瀏覽器實作)。
✅ 明確撰寫 ``,確保選擇器穩定。
- ❌ `` 放在 `` 之後
- HTML5 允許,但舊版瀏覽器要求 `` 在 `` **之前**。
✅ 遵循順序:`caption` → `colgroup` → `thead` → `tfoot` → `tbody`(或 `thead` → `tbody` → `tfoot` 現代寫法,但建議維持舊順序相容)。
- ❌ `
` 缺少 `scope`
- 螢幕閱讀器無法判斷表頭範圍。
✅ 所有 `` 必加 `scope`。
- ❌ 直接在 `
` 設定 `border`、`width`、`cellpadding`、`cellspacing`
- HTML4 屬性已廢棄。
✅ 全部改用 CSS:`border-collapse: collapse; width: 100%; padding: ...`。
- ❌ `
| ` 包含區塊元素 (` | | | |