表格專為「二維結構化資料」設計。正確的標記包含 <caption>、<thead>/<tbody>/<tfoot>、<th scope>,確保語意、無障礙與 SEO 最佳化。
<table>
<caption>月度銷售報表</caption>
<colgroup>
<col span="2" style="width: 30%;">
<col style="width: 40%;">
</colgroup>
<thead>
<tr>
<th scope="col">產品</th>
<th scope="col">數量</th>
<th scope="col">金額</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">筆記型電腦</th>
<td>120</td>
<td>NT$ 7,200,000</td>
</tr>
<tr>
<th scope="row">滑鼠</th>
<td>850</td>
<td>NT$ 425,000</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">合計</th>
<td>970</td>
<td>NT$ 7,625,000</td>
</tr>
</tfoot>
</table>
| 屬性 | 適用元素 | 說明 | 現代替代 |
|---|---|---|---|
border | table | 邊框寬度 (HTML 屬性,建議用 CSS) | CSS border |
cellspacing | table | 儲存格間距 (HTML 屬性,建議用 CSS) | CSS border-collapse: collapse + border-spacing |
cellpadding | table | 儲存格內邊距 (HTML 屬性) | CSS padding on th, td |
align | table | 表格水平對齊 (left/center/right) | CSS margin-inline: auto 或 Flex/Grid |
width | table, td, th | 寬度 (像素/百分比) | CSS width |
bgcolor | table, tr, td, th | 背景色 (HTML 屬性) | CSS background-color |
colspan | td, th | 跨欄合併 | 標準屬性,保留使用 |
rowspan | td, th | 跨列合併 | 標準屬性,保留使用 |
scope | th | 標題關聯範圍 (col/row/colgroup/rowgroup) | 無障礙關鍵,必填 |
headers | td, th | 關聯標題 ID 列表 (複雜表格) | 標準屬性,保留使用 |
nowrap | td, th | 禁止換行 (已棄用) | CSS white-space: nowrap |
.table-wrap { overflow-x: auto; } 保持資料完整th:first-child { position: sticky; left: 0; } 凍結首欄tr 變 display: block,配合 data-label 偽元素顯示欄位名<caption> 簡述表格內容<th> 有 scope 屬性id/headers 明確關聯