萬盛學電腦網

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

HTML 表格建構完整指南:從基礎結構到無障礙語義化實戰

HTML 表格:結構化資料呈現的標準容器

<table> 專為表格化資料設計,非佈局工具。正確使用語義化表格標籤,能確保螢幕閱讀器正確朗讀、搜尋引擎理解資料結構、CSS 精準樣式控制。本文從基礎骨架到無障礙進階,完整掌握表格建構。

最小可行表格結構

<table>
  <tr>
    <td>儲存格 1-1</td>
    <td>儲存格 1-2</td>
    <td>儲存格 1-3</td>
  </tr>
  <tr>
    <td>儲存格 2-1</td>
    <td>儲存格 2-2</td>
    <td>儲存格 2-3</td>
  </tr>
</table>

完整語義化表格架構

<table>
  <caption>2024 年 Q1 部門營收統計 (單位:萬元)</caption>
  
  <thead>
    <tr>
      <th scope="col">部門</th>
      <th scope="col">1 月</th>
      <th scope="col">2 月</th>
      <th scope="col">3 月</th>
      <th scope="col">小計</th>
    </tr>
  </thead>
  
  <tbody>
    <tr>
      <th scope="row">業務部</th>
      <td>120</td>
      <td>135</td>
      <td>142</td>
      <td>397</td>
    </tr>
    <tr>
      <th scope="row">技術部</th>
      <td>200</td>
      <td>210</td>
      <td>225</td>
      <td>635</td>
    </tr>
    <tr>
      <th scope="row">行政部</th>
      <td>80</td>
      <td>85</td>
      <td>90</td>
      <td>255</td>
    </tr>
  </tbody>
  
  <tfoot>
    <tr>
      <th scope="row">總計</th>
      <td>400</td>
      <td>430</td>
      <td>457</td>
      <td>1287</td>
    </tr>
  </tfoot>
</table>

核心標籤與屬性詳解

標籤全名關鍵屬性說明
<table>table-表格容器,不建議用 layout 屬性
<caption>caption-表格標題/說明,緊接 table 之後,螢幕閱讀器優先朗讀
<thead>table head-表頭區塊,列印時每頁重複
<tbody>table body-主體資料區,可多個分組
<tfoot>table foot-表尾摘要區,列印每頁重複,建議放在 tbody 前或後
<tr>table row-列容器
<th>table headerscope="col|row|colgroup|rowgroup"表頭儲存格,**scope 必填** 告知螢幕閱讀器範圍
<td>table datacolspan, rowspan, headers資料儲存格
<colgroup>column groupspan欄群組,統一設定寬度/樣式
<col>columnspan, width單一欄設定,隱藏元素

合併儲存格:colspan 與 rowspan

<table>
  <tr>
    <th rowspan="2">類別</th>
    <th colspan="2">2024 上半年</th>
    <th colspan="2">2024 下半年</th>
  </tr>
  <tr>
    <th>Q1</th> <th>Q2</th> <th>Q3</th> <th>Q4</th>
  </tr>
  <tr>
    <td>產品 A</td> <td>100</td> <td>120</td> <td>130</td> <td>140</td>
  </tr>
</table>
  • colspan="n":橫向跨併 n 欄
  • rowspan="n":縱向跨併 n 列
  • 被合併的儲存格不寫入 HTML,由瀏覽器自動排版

無障礙強化關鍵點

  1. <caption> 必備:簡述表格用途與範圍
  2. <th scope> 必填:scope="col" (欄標題) / scope="row" (列標題) / scope="colgroup" / scope="rowgroup"
  3. 複雜表格用 headers/id:多層表頭時,<th id="dept-sales"> + <td headers="dept-sales q1">
  4. 資料表格 ≠ 佈局表格:佈局請用 CSS Flex/Grid,表格僅承載資料
  5. 響應式表格:小螢幕 table { display: block; overflow-x: auto; } 或重構為卡片列表

CSS 樣式建議

/*** 基礎表格樣式 ***/
.table {
  width: 100%;
  border-collapse: collapse; /* 合併邊框,避免雙線 */
  font-size: 0.9rem;
}

.table th,
.table td {
  border: 1px solid #dee2e6;
  padding: 0.75rem;
  text-align: left;
  vertical-align: middle;
}

.table thead th {
  background-color: #f8f9fa;
  font-weight: 600;
  white-space: nowrap; /* 表頭不換行 */
}

.table tbody tr:nth-child(even) {
  background-color: rgba(0,0,0,.03); /* 斑馬紋 */
}

.table tbody tr:hover {
  background-color: rgba(0,123,255,.08); /* 懸停高亮 */
}

.table caption {
  caption-side: bottom; /* 標題置底 */
  padding: 0.5rem;
  font-style: italic;
  color: #6c757d;
}

/*** 響應式:小螢幕橫向捲動 ***/
@media (max-width: 767px) {
  .table-responsive {
    display: block;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .table-responsive > .table {
    min-width: 600px; /* 保證最小可讀寬度 */
  }
}

何時使用 / 不使用 table

✅ 適合使用 table❌ 不適合使用 table
財務報表、統計資料、價格比較表頁面整體佈局(header、sidebar、footer)
時刻表、課程表、賽程表導航選單、按鈕群組
產品規格對比、參數表卡片式商品列表
表單密集輸入區(如試算表風格)文章內容排版
日曆/日期選擇器任何非表格化資料呈現

現代框架整合提示

  • React/Vue:建立 <Table> 組件封裝表頭、分頁、排序、篩選邏輯
  • 資料驅動:columns 定義欄位渲染函數,data 陣列傳入資料
  • 虛擬滾動:大量資料 (10000+ 列) 使用 react-window / vue-virtual-scroller
  • 匯出功能:整合 SheetJS (xlsx) 或 csv-stringify 一鍵下載

表格是資訊密度最高的 UI 元件之一。遵循語義化結構、無障礙屬性、響應式策略,配合現代 CSS 與框架封裝,即能打造專業、可訪問、高效能的資料呈現體驗!

copyright © 萬盛學電腦網 all rights reserved