萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML 表格跨列合併(colspan)完整教學:語法、實戰場景與無障礙注意事項

HTML 表格的 `colspan` 屬性讓單一儲存格橫跨多欄,常用於表頭分組、統計總計列、複雜表單佈局等場景。本文詳細說明 `colspan` 語法規則、常見巢狀合併模式、無障礙標記要點、CSS 樣式控制技巧,並提供完整實戰範例,助你正確構建跨欄表格結構。

基本語法

<td colspan="N">內容</td>
<th colspan="N">標題</th>
  • N 為正整數,表示跨越的欄數(含自身)。
  • 該儲存格**不再佔用後續欄位**,同列後續 ``/`` 數量需相應減少。
  • 可套用於 ``、``,``/``/`` 皆可使用。

簡單範例:表頭分組

<table>
  <thead>
    <tr>
      <th rowspan="2">學期</th>
      <th colspan="3">上學期</th>
      <th colspan="3">下學期</th>
    </tr>
    <tr>
      <th>國文</th><th>數學</th><th>英文</th>
      <th>國文</th><th>數學</th><th>英文</th>
    </tr>
  </thead>
  <tbody>
    <tr><td>大一</td><td>85</td><td>90</td><td>78</td><td>88</td><td>92</td><td>80</td></tr>
  </tbody>
</table>

進階範例:多層巢狀跨列合併

<table class="complex-merge">
  <thead>
    <tr>
      <th rowspan="3">區域</th>
      <th colspan="4">2023 年季度營收 (萬元)</th>
      <th rowspan="3">年增率</th>
    </tr>
    <tr>
      <th colspan="2">上半年</th>
      <th colspan="2">下半年</th>
    </tr>
    <tr>
      <th>Q1</th><th>Q2</th><th>Q3</th><th>Q4</th>
    </tr>
  </thead>
  <tbody>
    <tr><td>北區</td><td>120</td><td>135</td><td>140</td><td>155</td><td>+12%</td></tr>
    <tr><td>中區</td><td>95</td><td>102</td><td>110</td><td>118</td><td>+15%</td></tr>
    <tr><td>南區</td><td>88</td><td>90</td><td>95</td><td>100</td><td>+8%</td></tr>
  </tbody>
  <tfoot>
    <tr>
      <th colspan="2">全年總計</th>
      <td>415</td><td>453</td>
      <td colspan="2">868</th>
      <td>+11.5%</td>
    </tr>
  </tfoot>
</table>

無障礙關鍵:scope 與 headers 屬性

跨列合併的表頭儲存格,必須明確宣告其範圍,螢幕閱讀器才能正確宣讀:

<th colspan="3" scope="colgroup">上學期</th>
<th colspan="2" scope="colgroup">上半年</th>
  • `scope="colgroup"`:宣告此表頭對應一組欄(跨列)。
  • `scope="rowgroup"`:橫跨多列的列表頭(配合 `rowspan`)。
  • 複雜表格可用 `id` + `headers`:
    `上半年` → `120`

CSS 樣式控制技巧

跨列儲存格文字置中與換行

.complex-merge th[colspan],
.complex-merge td[colspan] {
  text-align: center;
  white-space: normal;      /* 允許長標題換行 */
  word-break: keep-all;     /* 中文不斷詞 */
}

視覺區分合併區塊

.complex-merge thead th[colspan="4"] {
  background: linear-gradient(90deg, #e8f4fd 0%, #d6eaff 100%);
  border-bottom: 2px solid #3498db;
}
.complex-merge thead th[colspan="2"] {
  background: #f0f7ff;
  border-bottom: 1px solid #3498db;
}

列印時避免跨頁斷裂

@media print {
  .complex-merge thead { display: table-header-group; }
  .complex-merge tfoot { display: table-footer-group; }
  .complex-merge tr { page-break-inside: avoid; }
}

常見錯誤與除錯

❌ 欄位數不匹配
某列 `` 總數(含 `colspan` 計算)與表頭欄數不一致。
✅ 每列有效欄數 = Σ(`colspan` 或 1) 必須相等。使用開發者工具「Elements」面板查看 Computed Column Count。
❌ `colspan="0"` 或負數
無效值,瀏覽器會忽略或視為 1。
✅ 僅允許正整數(≥1)。
❌ 巢狀合併邏輯錯誤
同時使用 `rowspan` 與 `colspan` 時,需以矩陣座標思維規劃,建議先在紙上畫格子或用試算表模擬。
✅ 由外而內、由上而下逐層檢查每列欄位數。
❌ 忽略無障礙屬性
跨列表頭缺少 `scope="colgroup"`,螢幕閱讀器無法關聯欄位。
✅ 所有跨列/跨行表頭**必加** `scope` 或 `headers`/`id`。

JavaScript 動態操作 colspan

// 合併第 0 列第 1~3 欄
const cell = table.rows[0].cells[1];
cell.colSpan = 3;
cell.textContent = '合併後標題';
// 移除被合併的儲存格
for (let i = 0; i < 2; i++) {
  table.rows[0].deleteCell(2); // 索引會隨刪除變動,固定刪除第 2 格
}

// 拆分已合併儲存格
cell.colSpan = 1;
// 需手動插入被移除的儲存格,建議重建整列或使用模板引擎

總結

`colspan` 是構建複雜表格結構的關鍵屬性。核心原則:欄位數平衡、語意用 `scope="colgroup"`、樣式配合 `text-align: center`、列印加 `page-break-inside: avoid`。掌握矩陣座標思維規劃巢狀合併,善用開發者工具驗證欄位數,即可建立結構正確、無障礙友善、樣式優雅的跨欄表格。

copyright © 萬盛學電腦網 all rights reserved