最小可用表格模板
<table class="data-table">
<caption>示例表格標題</caption>
<thead>
<tr>
<th scope="col">表頭 1</th>
<th scope="col">表頭 2</th>
<th scope="col">表頭 3</th>
</tr>
</thead>
<tbody>
<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>
</tbody>
<tfoot>
<tr>
<th scope="row">合計</th>
<td>汇总值</td>
<td>汇总值</td>
</tr>
</tfoot>
</table>
跨行/跨列合併單元格
<table>
<tr>
<th rowspan="2">跨兩行</th>
<th colspan="2">跨兩列</th>
</tr>
<tr>
<td>第2行第2列</td>
<td>第2行第3列</td>
</tr>
<tr>
<td>第3行第1列</td>
<td>第3行第2列</td>
<td>第3行第3列</td>
</tr>
</table>
rowspan="n":當前單元格向下併入 n 行(含自身)。
colspan="n":當前單元格向右併入 n 列(含自身)。
- 被合併的單元格**不再編寫對應的
<td>/<th>**。
CSS 重置基礎樣式
.data-table { border-collapse: collapse; width: 100%; table-layout: fixed; }
.data-table th, .data-table td { border: 1px solid #ddd; padding: 8px; }
.data-table thead th { background: #f5f5f5; font-weight: 600; }
.data-table tbody tr:nth-child(even) { background: #fafafa; }
常見錯誤避坑
- 忘寫
</tr> 導致結構崩潰。
rowspan/colspan 數字算錯,導致列數對不上。
- 在
<tr> 直接放文字(必須包在 <td>/<th>)。
- 用 HTML 屬性
border, cellpadding, cellspacing(棄用)。
- 缺少
scope 導致無障礙失效。