| +8% | ||
| 合計 | ¥27,000 | +6.5% |
|---|
| 標籤 | 語義 | 必選/可選 |
|---|---|---|
<table> | 表格容器 | 必選 |
<caption> | 表格標題(無障礙關鍵) | 強烈建議 |
<thead> | 表頭行組 | 建議 |
<tbody> | 主體行組 | 建議(可多個) |
<tfoot> | 尾部行組(摘要) | 可選 |
<tr> | 行 | 必選 |
<th> | 標題單元格 | 表頭必選 |
<td> | 數據單元格 | 必選 |
<colgroup>/<col> | 列組/列定義(寬度、樣式) | 可選 |
scope="col|row|colgroup|rowgroup":明確 `colspan="n":橫向合併 n 列。rowspan="n":縱向合併 n 行。headers="id列表":複雜表格中將 `<table>
<tr>
<th rowspan="2">類別</th>
<th colspan="2">2023 年</th>
<th colspan="2">2024 年</th>
</tr>
<tr>
<th>Q1</th><th>Q2</th><th>Q1</th><th>Q2</th>
</tr>
<tr>
<td>手機</td><td>100</td><td>120</td><td>130</td><td>150</td>
</tr>
</table>
@media (max-width: 600px) {
table, thead, tbody, th, td, tr { display: block; }
thead { position: absolute; top: -9999px; } /* 隱藏表頭但保留語義 */
tr { margin-bottom: 1rem; border: 1px solid #ddd; }
td { display: flex; justify-content: space-between; padding: .5rem; }
td::before { content: attr(data-label); font-weight: bold; } /* 需在 td 加 data-label */
}
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background: #f5f5f5; font-weight: 600; }
tbody tr:nth-child(even) { background: #fafafa; }
tbody tr:hover { background: #eef; }
caption { caption-side: top; padding: 10px; font-size: 1.1em; font-weight: bold; }
表格是承載結構化數據的最佳語義容器。規範使用 `