萬盛學電腦網

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

HTML 表格標籤完整教學:語意化結構、合併儲存格、無障礙屬性與樣式最佳實踐

標準表格結構(語意化、無障礙友善)

<table>
  <caption>2024 季度營收統計(單位:萬元)</caption>
  <colgroup>
    <col style="width: 20%;">
    <col style="width: 30%;">
    <col style="width: 25%;">
    <col style="width: 25%;">
  </colgroup>
  <thead>
    <tr>
      <th scope="col">季度</th>
      <th scope="col">營收</th>
      <th scope="col">成本</th>
      <th scope="col">毛利率</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Q1</th>
      <td>1,200</td>
      <td>800</td>
      <td>33.3%</td>
    </tr>
    <tr>
      <th scope="row">Q2</th>
      <td>1,350</td>
      <td>850</td>
      <td>37.0%</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th scope="row">合計</th>
      <td>2,550</td>
      <td>1,650</td>
      <td>35.3%</td>
    </tr>
  </tfoot>
</table>

核心標籤語意對照

標籤語意必要性關鍵屬性
``
表格容器必須`border-collapse`, `table-layout`
`
`
表格標題/摘要**強烈建議**(無障礙關鍵)放在 `` 之後第一個` / `````` 前/後皆可,渲染在底部`
`
欄群組/欄定義選用(固定寬度、樣式分組推薦)`span`, `style="width:..."`
`
表頭群組建議(支援列印重複表頭、捲動固定)
`
主體群組隱性存在,顯式寫出更清晰可多個分組
`
表尾群組(總計、摘要)建議HTML 中寫在 `
`
列必須
``標題儲存格必須(語意化核心)`scope="col|row|colgroup|rowgroup"`, `id`, `colspan`, `rowspan`
``資料儲存格必須`headers`, `colspan`, `rowspan`

合併儲存格:`rowspan`(跨列)與 `colspan`(跨欄)

跨列範例:分類群組標題

<tbody>
  <tr>
    <th rowspan="3" scope="rowgroup">北區</th>
    <th scope="row">台北</th><td>100</td><td>200</td>
  </tr>
  <tr>
    <th scope="row">新北</th><td>150</td><td>250</td>
  </tr>
  <tr>
    <th scope="row">基隆</th><td>80</td><td>120</td>
  </tr>
  <tr>
    <th rowspan="2" scope="rowgroup">南區</th>
    <th scope="row">高雄</th><td>200</td><td>300</td>
  </tr>
  <tr>
    <th scope="row">台南</th><td>180</td><td>280</td>
  </tr>
</tbody>

**規則**:被合併的儲存格**完全不寫在 HTML**,`rowspan="3"` 表示佔據當前列含向下 2 列,共 3 列。

跨欄範例:多層表頭

<thead>
  <tr>
    <th rowspan="2" scope="col">產品</th>
    <th colspan="2" scope="colgroup">2023 年</th>
    <th colspan="2" scope="colgroup">2024 年</th>
  </tr>
  <tr>
    <th scope="col">Q1</th><th scope="col">Q2</th>
    <th scope="col">Q1</th><th scope="col">Q2</th>
  </tr>
</thead>

無障礙屬性:`scope` vs `headers`

  • **簡單表格**:`
`(欄標題)、``(列標題)。
  • **複雜合併表格**:給每個 `
  • ` 唯一 `id`,`` 用 `headers="id1 id2..."` 列出所有相關標題 ID。
  • **行群組標題**:`
  • `。
  • **欄群組標題**:`
  • `。

    CSS 樣式最佳實踐

    table {
      border-collapse: collapse;  /* 合併邊框,避免雙線 */
      width: 100%;
      table-layout: fixed;        /* 大表格效能、寬度可控 */
      font-size: 0.9rem;
    }
    
    caption {
      caption-side: top;          /* 或 bottom */
      padding: 0.75rem;
      font-weight: 600;
      text-align: left;
      background: #f8f9fa;
      border-bottom: 2px solid #333;
    }
    
    th, td {
      padding: 0.5rem 0.75rem;
      border: 1px solid #dee2e6;
      text-align: left;
      vertical-align: middle;
    }
    
    th {
      background: #e9ecef;
      font-weight: 600;
      white-space: nowrap;
    }
    
    tbody tr:nth-child(even) { background: #f8f9fa; } /* 斑馬紋 */
    tbody tr:hover { background: #e2e6ea; }           /* 滑鼠高亮 */
    
    th[scope="rowgroup"], th[scope="colgroup"] {
      background: #dee2e6;
      text-align: center;
    }
    
    /* 響應式:小螢幕水平捲動 */
    .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

    何時**不**該用 ``
    • 版面佈局(改用 Flexbox/Grid)
    • 非表格資料的列表(改用 `
        `/`
          `/`
          `)
        1. 單純對齊文字/圖片(改用 CSS)

      進階:可排序/可篩選/虛擬滾動表格

      純 HTML/CSS 無法實現互動功能,大型專案建議引入成熟庫:

    ` 本身不支援,需庫支援或改用 `
    ` + 虛擬化。

    遵循語意化結構、善用 `

    ` 控寬、正確設定 `scope`/`headers`,即可產出專業、無障礙、可維護的 HTML 表格。

    copyright © 萬盛學電腦網 all rights reserved