<table> 核心屬性
| 屬性 | 說明 | CSS 替代 | 狀態 |
width | 表格寬度 (px/%) | width | 棄用 |
height | 表格高度 | height / min-height | 棄用 |
border | 邊框寬度 (0=無) | border-width | 棄用 |
cellspacing | 單元格間距 | border-spacing (separate 模式) | 棄用 |
cellpadding | 單元格內邊距 | td, th { padding } | 棄用 |
align | 表格水平對齊 (left/center/right) | margin-left/right: auto (center), float/Flex | 棄用 |
bgcolor | 背景顏色 | background-color | 棄用 |
background | 背景圖片 URL | background-image | 棄用 |
bordercolor | 邊框顏色 | border-color | 非標準 |
bordercolorlight/dark | 3D 邊框亮/暗色 | CSS border-style/color | 非標準 |
frame | 外框顯示邊 (void/above/below/hsides/vsides/box/border) | border 組合 | 棄用 |
rules | 內部分隔線 (none/groups/rows/cols/all) | border 組合 | 棄用 |
summary | 表格摘要(無障礙) | <caption> + <details> / ARIA | 棄用 |
<tr> 行屬性
| 屬性 | 說明 | CSS 替代 |
align | 單元格內容水平對齊 | td { text-align } |
valign | 垂直對齊 (top/middle/bottom/baseline) | td { vertical-align } |
bgcolor | 行背景色 | tr { background } 或 tr:nth-child(even) |
height | 行高 | height / min-height |
<td> / <th> 單元格屬性
| 屬性 | 說明 | CSS 替代 |
colspan | 跨列數 | 無直接替代(結構屬性保留) |
rowspan | 跨行數 | 無直接替代(結構屬性保留) |
headers | 關聯表頭 id 列表(無障礙) | 保留 |
scope | col/row/colgroup/rowgroup(無障礙) | 保留 |
align/valign | 對齊 | text-align/vertical-align |
width/height | 尺寸 | width/height |
bgcolor | 背景色 | background |
nowrap | 禁止換行 | white-space: nowrap |
結構化標籤(保留、語義化、無障礙關鍵)
<caption>:表格標題,**必須緊跟 <table> 之後**,螢幕閱讀器優先朗讀。
<thead>, <tbody>, <tfoot>:語義分組,打印時 tfoot 可重複底部,滾動時 thead 可固定 (CSS position: sticky)。
<colgroup>, <col>:列組/列定義,可統一設置寬度、樣式(<col span="2" style="width: 200px;">)。
現代最佳實踐模板
<table class="data-table">
<caption>2024 年季度銷售報表</caption>
<colgroup>
<col style="width: 120px;">
<col span="3" style="width: 150px;">
</colgroup>
<thead>
<tr><th scope="col">區域</th><th scope="col">Q1</th><th scope="col">Q2</th><th scope="col">Q3</th></tr>
</thead>
<tbody>
<tr><th scope="row">華東</th><td>120</td><td>135</td><td>142</td></tr>
<tr><th scope="row">華南</th><td>98</td><td>110</td><td>118</td></tr>
</tbody>
<tfoot>
<tr><th scope="row">合計</th><td>218</td><td>245</td><td>260</td></tr>
</tfoot>
</table>
.data-table { border-collapse: collapse; width: 100%; font-size: 0.9rem; }
.data-table th, .data-table td { border: 1px solid #ddd; padding: 0.5rem 0.75rem; text-align: center; }
.data-table thead th { background: #f8f9fa; font-weight: 600; }
.data-table tbody tr:nth-child(even) { background: #fafafa; }
.data-table tfoot th, .data-table tfoot td { font-weight: bold; background: #e9ecef; }