| 屬性 | 作用 | 值 | 預設 |
|---|---|---|---|
colspan | 向右合併欄位 | 正整數 | 1 |
rowspan | 向下合併列 | 正整數 | 1 |
合併後,被覆蓋的儲存格必須從 HTML 移除,否則結構錯誤。
<table border="1">
<tr>
<th colspan="2">Name</th> <!-- 合併兩欄 -->
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>30</td>
</tr>
</table>
渲染結果:表頭「Name」跨越兩欄,對應下方 First/Last Name。
<table border="1">
<tr>
<th rowspan="2">Category</th> <!-- 合併兩列 -->
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td>Apple</td>
<td>$1</td>
</tr>
<tr>
<td>Banana</td>
<td>$2</td>
</tr>
</table>
<table>
<thead>
<tr>
<th rowspan="2">Product</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>
</thead>
<tbody>...</tbody>
</table>
合併儲存格時,螢幕閱讀器需明確知道關聯關係:
<th colspan="2" scope="colgroup">2023</th>
<th rowspan="2" scope="rowgroup">Product</th>
更複雜結構使用 headers 屬性:
<th id="y2023" colspan="2">2023</th>
<td headers="y2023 q1">100</td>
th[colspan], td[colspan] { text-align: center; }
th[rowspan], td[rowspan] { vertical-align: middle; }
/* 針對合併儲存格調整對齊 */
<td> 數 + colspan 總和 ≠ 表格總欄數。<thead>/<tbody> 合併:rowspan 不跨區塊,需在同一區塊內。display: flex/grid 破壞表格語意:合併屬性失效,勿在 <table> 使用非預設 display。.grid-table {
display: grid;
grid-template-columns: repeat(4, 1fr);
}
.header-span { grid-column: span 2; }
.side-span { grid-row: span 2; }
適合非表格資料的版面排版,語意上不再是資料表。