萬盛學電腦網

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

HTML 表格標籤使用方法完整教學:屬性、結構、合併與實戰範例

HTML 表格是呈現二維資料的標準方式。本文系統整理 <table>、<tr>、<td>、<th> 的屬性、巢狀關係、合併儲存格、對齊設定,並提供完整實戰範例與現代 CSS 最佳實務對照。

一、核心標籤與巢狀關係

<table>          <-- 最外層容器
  <caption>標題</caption>  <-- 選擇,放在 table 直後
  <thead>        <-- 表頭區塊(可選)
    <tr><th>...</th></tr>
  </thead>
  <tbody>        <-- 主體區塊
    <tr><td>...</td></tr>
  </tbody>
  <tfoot>        <-- 尾部/總計(可選)
    <tr><td>...</td></tr>
  </tfoot>
</table>

規則:<tr> 只能在 <table>/<thead>/<tbody>/<tfoot> 直層;<td>/<th> 只能在 <tr> 直層。

二、<table> 常用屬性(HTML 屬性 vs CSS 替代)

HTML 屬性說明CSS 替代(建議)
border邊框寬度(0=無)border: 1px solid #ddd
bordercolor邊框顏色border-color
bordercolorlight左/上亮邊色(3D)border-top/left-color
bordercolordark右/下暗邊色(3D)border-bottom/right-color
bgcolor背景色background-color
cellspacing儲存格間距border-spacing(需 border-collapse:separate)
cellpadding儲存格內邊距padding(加在 td/th)
width表格寬度width / max-width
align表格水平對齊margin: 0 auto (置中) / margin-left:0 (靠左)
summary表格摘要(無障礙)<caption> + <details> 或 ARIA

三、<tr>、<td>、<th> 關鍵屬性

列屬性(<tr>)

  • align="left|center|right" → 內容水平對齊(繼承給 td/th)
  • valign="top|middle|bottom" → 內容垂直對齊
  • bgcolor → 列背景色(改用 CSS tr:nth-child())

儲存格屬性(<td>/<th>)

  • width / height:寬高(px/%)
  • colspan="n":跨欄合併(預設 1)
  • rowspan="n":跨列合併(預設 1)
  • nowrap:禁止換行(棄用,改 white-space:nowrap)
  • align/valign:覆蓋列層級設定
  • scope="col|row|colgroup|rowgroup":無障礙關聯方向
  • headers="id1 id2":複雜表格關聯多個表頭

四、表頭 <th> 特性

  • 預設 粗體、置中
  • 語意:代表「標題儲存格」,非單純樣式
  • 配合 scope 或 headers 建立無障礙關聯

五、綜合實戰範例:跨國足球俱樂部表

<table border="1" width="50%" bgcolor="#E8E8E8" cellpadding="2"
       bordercolor="#0000FF" bordercolorlight="#7D7DFF" bordercolordark="#0000A0">
  <tr>
    <th width="33%" colspan="2" valign="bottom">義大利</th>
    <th width="36%" colspan="2" valign="bottom">英格蘭</th>
    <th width="36%" colspan="2" valign="bottom">西班牙</th>
  </tr>
  <tr>
    <td width="16%" align="center">AC米蘭</td>
    <td width="16%" align="center">佛羅倫薩</td>
    <td width="17%" align="center">曼聯</td>
    <td width="17%" align="center">紐卡斯爾</td>
    <td width="17%" align="center">巴塞隆納</td>
    <td width="17%" align="center">皇家社會</td>
  </tr>
  <tr>
    <td align="center">尤文圖斯</td>
    <td align="center">桑普多利亞</td>
    <td align="center">利物浦</td>
    <td align="center">阿森納</td>
    <td align="center">皇家馬德里</td>
    <td align="center">…</td>
  </tr>
  <tr>
    <td align="center">拉齊奧</td>
    <td align="center">國際米蘭</td>
    <td align="center">切爾西</td>
    <td align="center">米德爾斯堡</td>
    <td align="center">馬德里競技</td>
    <td align="center">…</td>
  </tr>
</table>

六、現代 CSS 重寫版(生產環境建議)

<style>
.club-table { border-collapse:collapse; width:100%; max-width:800px; margin:0 auto; font-family:sans-serif; }
.club-table th, .club-table td { border:1px solid #00F; padding:8px; text-align:center; }
.club-table thead th { background:#E8E8E8; border:2px solid #00F; font-weight:700; }
.club-table thead th[colspan="2"] { background:linear-gradient(180deg, #E8E8E8, #D0D0F0); }
.club-table tbody tr:nth-child(even) { background:#F5F5FF; }
.club-table tbody tr:hover { background:#E8F0FF; }
</style>

<table class="club-table">
  <caption>歐洲頂級足球俱樂部分組</caption>
  <thead>
    <tr>
      <th colspan="2" scope="colgroup">義大利</th>
      <th colspan="2" scope="colgroup">英格蘭</th>
      <th colspan="2" scope="colgroup">西班牙</th>
    </tr>
    <tr>
      <th scope="col">球隊</th><th scope="col">球隊</th>
      <th scope="col">球隊</th><th scope="col">球隊</th>
      <th scope="col">球隊</th><th scope="col">球隊</th>
    </tr>
  </thead>
  <tbody>
    <tr><td>AC米蘭</td><td>佛羅倫薩</td><td>曼聯</td><td>紐卡斯爾</td><td>巴塞隆納</td><td>皇家社會</td></tr>
    <tr><td>尤文圖斯</td><td>桑普多利亞</td><td>利物浦</td><td>阿森納</td><td>皇家馬德里</td><td>…</td></tr>
    <tr><td>拉齊奧</td><td>國際米蘭</td><td>切爾西</td><td>米德爾斯堡</td><td>馬德里競技</td><td>…</td></tr>
  </tbody>
</table>

七、避坑檢查清單

  • [ ] 移除所有 HTML 表現屬性(border/bgcolor/align...),改用 CSS
  • [ ] 加上 <caption>、<thead>/<tbody>/<tfoot>
  • [ ] 表頭加 scope="col|row"
  • [ ] 合併儲存格 colspan/rowspan 正確,**被合併的 td 完全刪除**
  • [ ] table-layout:fixed + overflow-x:auto 容器應對行動裝置
  • [ ] 驗證 W3C Validator 通過

掌握表格標籤完整用法,從語意結構到 CSS 樣式化,能產出結構清晰、無障礙友善、響應式優良的資料表格。建議將本文作為團隊 HTML 表格編碼規範參考。

copyright © 萬盛學電腦網 all rights reserved