萬盛學電腦網

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

HTML 表格製作完整教學:從基本結構到響應式設計

HTML 表格 (<table>) 用於呈現「表格式資料」,非版面佈局工具。本文從基本結構、語義標籤、合併儲存格、無障礙屬性,延伸至 CSS 樣式與響應式處理,完整掌握表格開發。

1. 基本表格結構

<table>
  <caption>月份銷售報表</caption>
  <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">1 月</th>
      <td>$10,000</td>
      <td>$4,000</td>
      <td>$6,000</td>
    </tr>
    <tr>
      <th scope="row">2 月</th>
      <td>$12,000</td>
      <td>$5,000</td>
      <td>$7,000</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th scope="row">總計</th>
      <td>$22,000</td>
      <td>$9,000</td>
      <td>$13,000</td>
    </tr>
  </tfoot>
</table>

關鍵標籤說明

  • <table>:表格容器
  • <caption>:表格標題/說明,放在 <table> 之後,利於無障礙與 SEO
  • <thead>, <tbody>, <tfoot>:語義分組表頭、主體、表尾,支援列印分頁、捲動固定表頭
  • <tr> (Table Row):表格列
  • <th> (Table Header):表頭儲存格,**必加 scope="col|row|colgroup|rowgroup"** 明確關聯範圍
  • <td> (Table Data):資料儲存格

2. 合併儲存格

  • 跨列合併 (橫向) : <td colspan="2"> — 佔據 2 欄
  • 跨行合併 (縱向) : <td rowspan="3"> — 佔據 3 列
  • 合併時,被合併的儲存格標籤需移除,避免多餘儲存格導致欄位錯位。
<tr>
  <th rowspan="2">類別</th>
  <th colspan="2">2023 年</th>
  <th colspan="2">2024 年</th>
</tr>
<tr>
  <th>Q1</th><th>Q2</th>
  <th>Q1</th><th>Q2</th>
</tr>

3. 無障礙關鍵屬性

屬性用於說明
scope<th>col (欄標題)、row (列標題)、colgroup, rowgroup — 螢幕閱讀器關鍵
headers<td>引用對應 <th id="..."> 的 id,複雜表格必要
<caption><table>表格標題,螢幕閱讀器優先朗讀
summary<table>HTML5 已棄用,改用 <caption> 或 <details> 包裹說明

4. CSS 樣式美化

table {
  width: 100%;
  border-collapse: collapse;  /* 合併邊框,避免雙線 */
  font-family: system-ui, sans-serif;
}
th, td {
  border: 1px solid #ddd;
  padding: 0.75rem 1rem;
  text-align: left;
}
th {
  background: #f8f9fa;
  font-weight: 600;
  white-space: nowrap;  /* 表頭不換行 */
}
tbody tr:nth-child(even) {
  background: #fafafa;  /* 斑馬紋 */
}
tbody tr:hover {
  background: #eef;     /* 懸停高亮 */
}
caption {
  caption-side: bottom;  /* 標題置底 */
  padding: 0.5rem;
  font-style: italic;
  color: #666;
}

5. 響應式表格策略

行動裝置寬度不足顯示完整表格時,常見三種方案:

方案 A:水平捲動容器 (最簡單、資料完整)

<div class="table-responsive">
  <table>...</table>
</div>

.table-responsive {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
table { min-width: 600px; } /* 強制最小寬度觸發捲動 */

方案 B:CSS 重排為卡片/清單 (需較多 CSS)

@media (max-width: 600px) {
  table, thead, tbody, th, td, tr { display: block; }
  thead { display: none; } /* 隱藏表頭,改用 data-label */
  tr { margin-bottom: 1rem; border: 1px solid #ddd; border-radius: 4px; }
  td { 
    display: flex; justify-content: space-between; 
    padding: 0.5rem; border: none; border-bottom: 1px solid #eee;
  }
  td::before { 
    content: attr(data-label); 
    font-weight: bold; color: #333; 
  }
  td:last-child { border-bottom: none; }
}

HTML 需加 data-label 屬性:<td data-label="收入">$10,000</td>

方案 C:關鍵欄固定,其餘捲動 (進階)

使用 position: sticky 固定第一欄:

th:first-child, td:first-child {
  position: sticky;
  left: 0;
  background: white;
  z-index: 1;
  box-shadow: 2px 0 4px rgba(0,0,0,0.1);
}

6. 何時「不」該用表格

  • 版面佈局 → 使用 Flexbox / Grid
  • 單純列表 → <ul>/<ol>
  • 鍵值對資料 → <dl> (定義清單)
  • 複雜儀表板 → 考慮圖表庫或資料網格元件

7. 進階功能 (需 JavaScript)

  • 排序、篩選、分頁 → DataTables, TanStack Table
  • 可編輯儲存格、拖曳欄位調整
  • 虛擬滾動 (大量資料效能)
  • 匯出 CSV/Excel/PDF

掌握語義化結構、無障礙屬性、CSS 美化與響應式策略,就能製作出專業、易用、可維護的 HTML 表格。

copyright © 萬盛學電腦網 all rights reserved