萬盛學電腦網

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

HTML 表格標籤完整參考:結構、屬性、無障礙與現代最佳實踐

表格相關標籤完整清單

HTML 表格由 10 個核心標籤組成,各司其職。正確使用能建立語意清晰、無障礙友善、易於樣式化的資料表。

標籤速查表

標籤全名角色父容器子元素必要性
<table>Table根容器流式內容父元素caption, colgroup, thead, tbody, tfoot, tr必填
<caption>Caption表格標題/說明table (第一個子元素)流式內容強烈建議(無障礙)
<colgroup>Column Group欄群組table (caption 後)col選填(固定佈局需用)
<col>Column欄屬性colgroup空元素選填
<thead>Table Head表頭區塊table (colgroup 後)tr建議(語意/列印重複)
<tbody>Table Body主體區塊table (thead 後)tr隱含(可省略標籤)
<tfoot>Table Foot表尾區塊table (tbody 後)tr選填(合計/摘要)
<tr>Table Row列thead, tbody, tfootth, td必填
<th>Table Header表頭儲存格tr流式內容表頭必填
<td>Table Data資料儲存格tr流式內容資料列必填

關鍵屬性詳解

屬性適用標籤說明範例
scopeth關聯範圍:col/row/colgroup/rowgroup<th scope="col">姓名</th>
colspanth, td跨欄合併<th colspan="2">姓名</th>
rowspanth, td跨列合併<td rowspan="3">群組 A</td>
headerstd, th關聯表頭 ID 群組(複雜表格)<td headers="h1 h2">
abbrth表頭縮寫(螢幕閱讀器讀取)<th abbr="Q1">第一季</th>
alignth, td, tr, thead...❌ 已棄用,改用 CSS text-align—
valignth, td, tr...❌ 已棄用,改用 CSS vertical-align—
bgcolortable, tr, td...❌ 已棄用,改用 CSS background—
bordertable❌ 已棄用,改用 CSS border—
cellpaddingtable❌ 已棄用,改用 CSS padding—
cellspacingtable❌ 已棄用,改用 CSS border-spacing—
widthtable, td, th❌ 已棄用,改用 CSS width—

完整語意化範例:季度營收報表

<table class="financial-report">
  <caption>2024 年季度營收統計(單位:萬)</caption>
  
  <colgroup>
    <col span="1" style="width: 100px;"> <!-- 部門 -->
    <col span="4" style="width: 120px;"> <!-- Q1-Q4 -->
    <col span="1" style="width: 100px;"> <!-- 年度總計 -->
  </colgroup>
  
  <thead>
    <tr>
      <th scope="col">部門</th>
      <th scope="col" abbr="Q1">第一季</th>
      <th scope="col" abbr="Q2">第二季</th>
      <th scope="col" abbr="Q3">第三季</th>
      <th scope="col" abbr="Q4">第四季</th>
      <th scope="col">年度總計</th>
    </tr>
  </thead>
  
  <tbody>
    <tr>
      <th scope="row">業務部</th>
      <td>1,200</td>
      <td>1,350</td>
      <td>1,480</td>
      <td>1,620</td>
      <td>5,650</td>
    </tr>
    <tr>
      <th scope="row">技術部</th>
      <td>980</td>
      <td>1,100</td>
      <td>1,250</td>
      <td>1,400</td>
      <td>4,730</td>
    </tr>
  </tbody>
  
  <tfoot>
    <tr>
      <th scope="row">合計</th>
      <td>2,180</td>
      <td>2,450</td>
      <td>2,730</td>
      <td>3,020</td>
      <td>10,380</td>
    </tr>
  </tfoot>
</table>

複雜表格:headers/id 關聯(多層表頭)

<table>
  <thead>
    <tr>
      <th rowspan="2" id="dept">部門</th>
      <th colspan="3" id="q1">Q1</th>
      <th colspan="3" id="q2">Q2</th>
    </tr>
    <tr>
      <th id="q1-sales" headers="q1">業務</th>
      <th id="q1-tech" headers="q1">技術</th>
      <th id="q1-mkt" headers="q1">行銷</th>
      <th id="q2-sales" headers="q2">業務</th>
      <th id="q2-tech" headers="q2">技術</th>
      <th id="q2-mkt" headers="q2">行銷</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row" headers="dept">總公司</th>
      <td headers="dept q1 q1-sales">120</td>
      <td headers="dept q1 q1-tech">90</td>
      ...
    </tr>
  </tbody>
</table>

CSS 重置基礎樣式

table {
  border-collapse: collapse;
  width: 100%;
  font-variant-numeric: tabular-nums;
}
th, td {
  padding: 12px 16px;
  border: 1px solid #dee2e6;
  text-align: left;
  vertical-align: middle;
}
thead th {
  background: #f8f9fa;
  font-weight: 600;
  white-space: nowrap;
}
tbody tr:nth-child(even) { background: #f8f9fa; }
tbody tr:hover { background: #e7f1ff; }
caption {
  caption-side: top;
  padding: 12px;
  font-weight: 600;
  font-size: 1.1rem;
  color: #212529;
}

無障礙檢查清單

  • ☐ 有 <caption> 描述表格用途
  • ☐ 表頭用 <th scope="col/row">(簡單表格)或 headers/id(複雜表格)
  • ☐ abbr 提供表頭縮寫
  • ☐ 無版面佈局表格(改用 CSS Grid/Flex)
  • ☐ 色彩對比度達 WCAG AA(4.5:1)
  • ☐ 鍵盤可導航(原生表格支援)

精通表格標籤與屬性,配合現代 CSS 佈局,能建構出結構優雅、無障礙完備、效能優異的資料表元件,是前端工程師處理結構化資料的核心能力。

copyright © 萬盛學電腦網 all rights reserved