colspan 與 rowspan| 屬性 | 作用 | 值 | 適用元素 | 預設 |
|---|---|---|---|---|
colspan | 水平合併相鄰儲存格 | 正整數 (1-1000) | <td>, <th> | 1 |
rowspan | 垂直合併相鄰儲存格 | 正整數 (0-65534, 0=合併至表格結尾) | <td>, <th> | 1 |
<table class="table-base">
<thead>
<tr>
<th colspan="3">2024 年度銷售報表</th>
</tr>
<tr>
<th>季度</th><th>營收 (萬元)</th><th>成長率</th>
</tr>
</thead>
<tbody>
<tr><td>Q1</td><td>1,250</td><td>+12.5%</td></tr>
<tr><td>Q2</td><td>1,480</td><td>+18.4%</td></tr>
<tr><td>Q3</td><td>1,320</td><td>-10.8%</td></tr>
<tr><td>Q4</td><td>1,650</td><td>+25.0%</td></tr>
</tbody>
<tfoot>
<tr>
<th>年度總計</th>
<td colspan="2">5,700 萬元</td>
</tr>
</tfoot>
</table>
<table class="table-base">
<thead>
<!-- 第 1 層:大分類 -->
<tr>
<th rowspan="3">產品線</th>
<th colspan="4">上半年</th>
<th colspan="4">下半年</th>
</tr>
<!-- 第 2 層:季度 -->
<tr>
<th colspan="2">Q1</th><th colspan="2">Q2</th>
<th colspan="2">Q3</th><th colspan="2">Q4</th>
</tr>
<!-- 第 3 層:指標 -->
營收 成長
營收 成長
營收 成長
營收 成長
手機
120 +15%
135 +12%
110 -8%
150 +22%
15 +5%
18 +10%
12 -5%
22 +18%
筆電
85 +10%
92 +8%
88 -3%
105 +19%
表格隱含一個「虛擬網格」。每行的 **有效欄位數 = 實際 `
上例驗證:
修正: 第 1 行的 `rowspan="3"` 佔用第 1 欄 3 行。第 2 行**不應**有第 1 欄的 `
<!-- 正確:第 1 行定義總欄位數 = 1 (產品線) + 8 (半年指標) = 9 -->
<tr>
<th rowspan="3">產品線</th>
<th colspan="4">上半年</th> <!-- 佔第 2-5 欄 -->
<th colspan="4">下半年</th> <!-- 佔第 6-9 欄 -->
</tr>
<tr> <!-- 第 2 行:產品線被佔用,從第 2 欄開始 -->
<th colspan="2">Q1</th><th colspan="2">Q2</th>
<th colspan="2">Q3</th><th colspan="2">Q4</th>
</tr>
<tr> <!-- 第 3 行:產品線被佔用,從第 2 欄開始 -->
<th>營收</th><th>成長</th> ... (共 8 個 th)
</tr>
scope、colgroup/rowgroup合併儲存格**必須**聲明其作用範圍,螢幕閱讀器才能正確關聯數據:
| 合併類型 | scope 值 | 說明 | |
|---|---|---|---|
| 單列標題橫跨多欄 | scope="colgroup" | 該 ` | ` 為後續欄群組的標題 |
| 單行標題縱跨多列 | scope="rowgroup" | 該 ` | ` 為後續列群組的標題 |
| 單一欄位標題 | scope="col" | 預設,單欄標題 | |
| 單一列標題 | scope="row" | 預設,單列標題 |
<th colspan="4" scope="colgroup">上半年</th>
<th rowspan="3" scope="rowgroup">產品線</th>
/* 1. 合併標題:置中、加粗、背景區隔 */
th[colspan], th[rowspan] {
text-align: center;
font-weight: 700;
background: linear-gradient(180deg, #f8f9fa, #eef1f5);
}
/* 2. 合併儲存格內容垂直置中 (rowspan) */
td[rowspan], th[rowspan] {
vertical-align: middle;
display: table-cell; /* 確保生效 */
}
/* 3. 視覺分隔:合併邊框加粗/變色 */
th[colspan="2"] { border-right: 2px solid #2c3e50; }
th[colspan="3"] { border-right: 2px solid #2c3e50; }
th[colspan="4"] { border-right: 2px solid #2c3e50; }
th[rowspan="2"] { border-bottom: 2px solid #2c3e50; }
/* 4. 複雜表頭層級色彩區分 */
thead tr:nth-child(1) th { background: #1a252f; color: #fff; } /* 第 1 層:深色 */
thead tr:nth-child(2) th { background: #2c3e50; color: #fff; } /* 第 2 層:中色 */
thead tr:nth-child(3) th { background: #3a506b; color: #ddd; } /* 第 3 層:淺色 */
/* 5. 列群組標題樣式 */
th[scope="rowgroup"] {
writing-mode: vertical-rl; /* 直書顯示列群組標題 */
text-orientation: mixed;
padding: 0 8px;
min-width: 40px;
}
合併儲存格在小螢幕極難處理:
/* 卡片式響應式:隱藏表頭,用 data-label 標記 */
@media (max-width: 640px) {
.table-responsive-card thead { display: none; }
.table-responsive-card tbody tr {
display: block; border: 1px solid #ddd; border-radius: 8px; margin-bottom: 16px; padding: 16px;
}
.table-responsive-card td {
display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid #eee;
}
.table-responsive-card td::before { content: attr(data-label); font-weight: 600; color: #666; }
/* colspan/rowspan 在 display:block 下失效,需 JS 將合併標題轉為獨立區塊標題 */
}
function validateTableColspan(table) {
const headerRows = table.querySelectorAll('thead tr');
let expectedCols = 0;
// 以最底層表頭行 (無 rowspan/colspan 或僅有 colspan) 為基準
const leafRow = [...headerRows].reverse().find(r =>
[...r.cells].every(c => !c.hasAttribute('rowspan'))
);
if (leafRow) {
expectedCols = [...leafRow.cells].reduce((sum, cell) => sum + (cell.colSpan || 1), 0);
}
// 驗證所有行
[...table.querySelectorAll('thead tr, tbody tr, tfoot tr')].forEach((row, i) => {
const actual = [...row.cells].reduce((sum, cell) => sum + (cell.colSpan || 1), 0);
// 扣除被上方 rowspan 佔用的欄位 (簡易版,完整需追蹤佔用矩陣)
if (actual !== expectedCols) {
console.warn(`Row ${i} (${row.sectionRowIndex}) column mismatch: expected ${expectedCols}, got ${actual}`, row);
}
});
return expectedCols;
}
table-inspector (Chrome DevTools Elements 面板顯示表格網格)、螢幕閱讀器 (NVDA/VoiceOver) 實測朗讀邏輯。`colspan`/`rowspan` 是 HTML 表格表達複雜二維關係的強大語意工具。正確使用需像建築師繪製結構圖般精確規劃虛擬網格,配合無障礙屬性與現代 CSS,才能呈現既專業又包容的資料展示體驗。