HTML 表格由 10 個核心標籤組成,各司其職。正確使用能建立語意清晰、無障礙友善、易於樣式化的資料表。
| 標籤 | 全名 | 角色 | 父容器 | 子元素 | 必要性 |
|---|---|---|---|---|---|
<table> | Table | 根容器 | 流式內容父元素 | caption, colgroup, thead, tbody, tfoot, tr | 必填 |
<caption> | Caption | 表格標題/說明 | table (第一個子元素) | 流式內容 | 強烈建議(無障礙) |
<colgroup> | Column Group | 欄群組 | table (caption 後) | col | 選填(固定佈局需用) |
<col> | Column | 欄屬性 | colgroup | 空元素 | 選填 |
<thead> | Table Head | 表頭區塊 | table (colgroup 後) | tr | 建議(語意/列印重複) |
<tbody> | Table Body | 主體區塊 | table (thead 後) | tr | 隱含(可省略標籤) |
<tfoot> | Table Foot | 表尾區塊 | table (tbody 後) | tr | 選填(合計/摘要) |
<tr> | Table Row | 列 | thead, tbody, tfoot | th, td | 必填 |
<th> | Table Header | 表頭儲存格 | tr | 流式內容 | 表頭必填 |
<td> | Table Data | 資料儲存格 | tr | 流式內容 | 資料列必填 |
| 屬性 | 適用標籤 | 說明 | 範例 |
|---|---|---|---|
scope | th | 關聯範圍:col/row/colgroup/rowgroup | <th scope="col">姓名</th> |
colspan | th, td | 跨欄合併 | <th colspan="2">姓名</th> |
rowspan | th, td | 跨列合併 | <td rowspan="3">群組 A</td> |
headers | td, th | 關聯表頭 ID 群組(複雜表格) | <td headers="h1 h2"> |
abbr | th | 表頭縮寫(螢幕閱讀器讀取) | <th abbr="Q1">第一季</th> |
align | th, td, tr, thead... | ❌ 已棄用,改用 CSS text-align | — |
valign | th, td, tr... | ❌ 已棄用,改用 CSS vertical-align | — |
bgcolor | table, tr, td... | ❌ 已棄用,改用 CSS background | — |
border | table | ❌ 已棄用,改用 CSS border | — |
cellpadding | table | ❌ 已棄用,改用 CSS padding | — |
cellspacing | table | ❌ 已棄用,改用 CSS border-spacing | — |
width | table, td, th | ❌ 已棄用,改用 CSS width | — |
<table class="financial-report">
<caption>2024 年季度營收統計(單位:萬)</caption>
<colgroup>
<col span="1" style="width: 100px;"> <!-- 部門 -->
<col span="4" style="width: 120px;"> <!-- Q1-Q4 -->
<col span="1" style="width: 100px;"> <!-- 年度總計 -->
</colgroup>
<thead>
<tr>
<th scope="col">部門</th>
<th scope="col" abbr="Q1">第一季</th>
<th scope="col" abbr="Q2">第二季</th>
<th scope="col" abbr="Q3">第三季</th>
<th scope="col" abbr="Q4">第四季</th>
<th scope="col">年度總計</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">業務部</th>
<td>1,200</td>
<td>1,350</td>
<td>1,480</td>
<td>1,620</td>
<td>5,650</td>
</tr>
<tr>
<th scope="row">技術部</th>
<td>980</td>
<td>1,100</td>
<td>1,250</td>
<td>1,400</td>
<td>4,730</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">合計</th>
<td>2,180</td>
<td>2,450</td>
<td>2,730</td>
<td>3,020</td>
<td>10,380</td>
</tr>
</tfoot>
</table>
<table>
<thead>
<tr>
<th rowspan="2" id="dept">部門</th>
<th colspan="3" id="q1">Q1</th>
<th colspan="3" id="q2">Q2</th>
</tr>
<tr>
<th id="q1-sales" headers="q1">業務</th>
<th id="q1-tech" headers="q1">技術</th>
<th id="q1-mkt" headers="q1">行銷</th>
<th id="q2-sales" headers="q2">業務</th>
<th id="q2-tech" headers="q2">技術</th>
<th id="q2-mkt" headers="q2">行銷</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row" headers="dept">總公司</th>
<td headers="dept q1 q1-sales">120</td>
<td headers="dept q1 q1-tech">90</td>
...
</tr>
</tbody>
</table>
table {
border-collapse: collapse;
width: 100%;
font-variant-numeric: tabular-nums;
}
th, td {
padding: 12px 16px;
border: 1px solid #dee2e6;
text-align: left;
vertical-align: middle;
}
thead th {
background: #f8f9fa;
font-weight: 600;
white-space: nowrap;
}
tbody tr:nth-child(even) { background: #f8f9fa; }
tbody tr:hover { background: #e7f1ff; }
caption {
caption-side: top;
padding: 12px;
font-weight: 600;
font-size: 1.1rem;
color: #212529;
}
<caption> 描述表格用途<th scope="col/row">(簡單表格)或 headers/id(複雜表格)abbr 提供表頭縮寫精通表格標籤與屬性,配合現代 CSS 佈局,能建構出結構優雅、無障礙完備、效能優異的資料表元件,是前端工程師處理結構化資料的核心能力。