萬盛學電腦網

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

HTML 表格跨列與跨列教學:colspan、rowspan 完整指南與實戰範例

核心屬性

屬性作用對象說明值
colspan<td>, <th>**橫跨多欄** (水平合併儲存格)正整數 (預設 1)
rowspan<td>, <th>**橫跨多列** (垂直合併儲存格)正整數 (預設 1)

基礎範例:跨列 (colspan) - 表頭分組

<table border="1">
  <thead>
    <tr>
      <th rowspan="2">學生</th> <!-- 垂直跨兩列:姓名固定 -->
      <th colspan="2">期中考</th>      <!-- 水平跨兩欄:期中考包含國文、數學 -->
      <th colspan="2">期末考</th>
    </tr>
    <tr>
      <th>國文</th><th>數學</th>
      <th>國文</th><th>數學</th>
    </tr>
  </thead>
  <tbody>
    <tr><td>小明</td><td>85</td><td>92</td><td>88</td><td>95</td></tr>
    <tr><td>小華</td><td>78</td><td>80</td><td>82</td><td>85</td></tr>
  </tbody>
</table>

關鍵邏輯:colspan="2" 的儲存格**佔據兩欄位置**,該列後續 <th> 會**自動往後順延**。下一列 <tr> 必須提供**扣除合併後的實際欄數** (此例:1 + 2 + 2 = 5 欄,第二列提供 4 個 <th>,加上第一列 rowspan 佔據的 1 欄,共 5 欄)。

基礎範例:跨列 (rowspan) - 資料分組

<table border="1">
  <tbody>
    <tr>
      <th rowspan="3">2024 年 Q1</th> <!-- 垂直跨三列:季度標籤 -->
      <td>1 月</td><td>$12,000</td>
    </tr>
    <tr><td>2 月</td><td>$15,500</td></tr> <!-- 此列只有 2 個 td,因第一欄被 rowspan 佔據 -->
    <tr><td>3 月</td><td>$13,200</td></tr>
    <tr>
      <th rowspan="2">2024 年 Q2</th>
      <td>4 月</td><td>$14,800</td>
    </tr>
    <tr><td>5 月</td><td>$16,000</td></tr>
  </tbody>
</table>

關鍵邏輯:rowspan="3" 的儲存格向下佔據三列。後續 <tr> **必須省略對應的儲存格**,直接寫入剩餘欄位。瀏覽器會自動將合併儲存格向下延伸。

綜合實戰:複雜合併儲存格 (財報風格)

<table class="report-table">
  <caption>2024 年各部門季度營收彙總 (單位: 萬元)</caption>
  <colgroup>
    <col style="width: 120px;"> <!-- 部門 -->
    <col style="width: 80px;" span="4"> <!-- 4 季 -->
    <col style="width: 100px;"> <!-- 年總計 -->
  </colgroup>
  <thead>
    <tr>
      <th rowspan="2">部門</th>
      <th colspan="4">季度營收</th>
      <th rowspan="2">年總計</th>
    </tr>
    <tr>
      <th>Q1</th><th>Q2</th><th>Q3</th><th>Q4</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>業務部</th>
      <td>120</td><td>135</td><td>140</td><td>155</td>
      <td>550</td>
    </tr>
    <tr>
      <th rowspan="2">研發部</th> <!-- 研發分兩組 -->
      <td>80</td><td>85</td><td>90</td><td>95</td>
      <td rowspan="2">680</th> <!-- 年總計也合併 -->
    </tr>
    <tr>
      <td>70</td><td>75</td><td>80</td><td>85</td>
      <!-- 年總計被上一列 rowspan 佔據,此列不寫 -->
    </tr>
    <tr>
      <th>行政部</th>
      <td>30</td><td>32</td><td>31</td><td>33</td>
      <td>126</td>
    </tr>
    <tr class="total-row">
      <th colspan="2">全公司總計</th> <!-- 合併前兩欄 -->
      <td>300</td><td>327</td><td>341</td><td>368</td>
      <td>1,356</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td colspan="6">資料來源:財務系統自動匯出</td>
    </tr>
  </tfoot>
</table>

CSS 樣式建議 (配合固定佈局)

.report-table {
  border-collapse: collapse;
  width: 100%;
  table-layout: fixed;      /* 固定佈局,配合 colgroup 精確控寬 */
  font-size: 0.9rem;
}
.report-table th, .report-table td {
  border: 1px solid #ddd;
  padding: 8px 12px;
  text-align: center;
  vertical-align: middle;
}
.report-table thead th { background: #eef2f7; font-weight: 600; }
.report-table tbody th { background: #fafafa; text-align: left; }
.report-table .total-row { background: #fff8e1; font-weight: bold; }
.report-table caption { caption-side: bottom; padding: 8px; color: #666; font-size: 0.85rem; }

常見錯誤與除錯技巧

錯誤現象原因修正方法
表格欄位錯位、多出/少了欄合併儲存格後,後續 <tr> 的 <td>/<th> 數量未扣除合併佔用的欄位**手動計算每列實際需要的儲存格數** = 總欄數 - 被上方 rowspan 佔用的欄數 - 本列已合併的 colspan 佔用的額外欄數
合併儲存格內容不置中/對齊異常預設 vertical-align: middle,但若合併儲存格高度不一致明確設定 vertical-align: middle,確保 line-height 足夠,或用 Flexbox 置中 (需 display: flex 於儲存格內容器)
rowspan 跨越 <thead>/<tbody>/<tfoot> 區塊HTML 規範**禁止**跨區塊合併調整結構,將需跨區塊的合併移至同一區塊內,或拆分為獨立表格
響應式斷點下合併儲存格樣式錯亂display: block 或 Flex/Grid 破壞表格佈局小螢幕改用 overflow-x: auto 容器滾動,**勿改變 table/tr/td 的 display 屬性**

無障礙 (Accessibility) 重點

  • <th> **必須** 加上 scope="col" (直欄標題) 或 scope="row" (橫列標題) 或 scope="colgroup"/"rowgroup" (合併標題)。
  • 複雜合併表格,使用 headers 屬性於 <td> 指向對應 <th id="...>:<td headers="dept q1">。
  • <caption> 提供表格摘要說明。

JavaScript 動態操作合併儲存格 (進階)

function mergeCells(tableId, startRow, startCol, rowSpan, colSpan) {
  const table = document.getElementById(tableId);
  const cell = table.rows[startRow].cells[startCol];
  cell.rowSpan = rowSpan;
  cell.colSpan = colSpan;

  // 移除被合併的儲存格 (從後往前刪除索引不會亂)
  for (let r = startRow; r < startRow + rowSpan; r++) {
    const row = table.rows[r];
    const startIdx = r === startRow ? startCol + 1 : startCol;
    const endIdx = startCol + colSpan;
    for (let c = endIdx - 1; c >= startIdx; c--) {
      if (row.cells[c]) row.deleteCell(c);
    }
  }
}
// 使用:mergeCells('report-table', 1, 0, 2, 1); // 第2列第1欄 向下合併2列

結語

colspan 與 rowspan 是表格呈現分組、總計、層級結構的利器。**核心原則:合併儲存格佔據的空間,後續列必須對應減少儲存格數量**。搭配 <colgroup> 定寬、table-layout: fixed、語意化 scope/headers,即能建立專業、無障礙、響應式的複雜資料表格。

copyright © 萬盛學電腦網 all rights reserved