萬盛學電腦網

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

HTML 表格屬性完整參考:table、tr、td/th、caption、colgroup、col 核心屬性與 CSS 替代方案

<table> 核心屬性

屬性說明CSS 替代狀態
width表格寬度 (px/%)width棄用
height表格高度height / min-height棄用
border邊框寬度 (0=無)border-width棄用
cellspacing單元格間距border-spacing (separate 模式)棄用
cellpadding單元格內邊距td, th { padding }棄用
align表格水平對齊 (left/center/right)margin-left/right: auto (center), float/Flex棄用
bgcolor背景顏色background-color棄用
background背景圖片 URLbackground-image棄用
bordercolor邊框顏色border-color非標準
bordercolorlight/dark3D 邊框亮/暗色CSS border-style/color非標準
frame外框顯示邊 (void/above/below/hsides/vsides/box/border)border 組合棄用
rules內部分隔線 (none/groups/rows/cols/all)border 組合棄用
summary表格摘要(無障礙)<caption> + <details> / ARIA棄用

<tr> 行屬性

屬性說明CSS 替代
align單元格內容水平對齊td { text-align }
valign垂直對齊 (top/middle/bottom/baseline)td { vertical-align }
bgcolor行背景色tr { background } 或 tr:nth-child(even)
height行高height / min-height

<td> / <th> 單元格屬性

屬性說明CSS 替代
colspan跨列數無直接替代(結構屬性保留)
rowspan跨行數無直接替代(結構屬性保留)
headers關聯表頭 id 列表(無障礙)保留
scopecol/row/colgroup/rowgroup(無障礙)保留
align/valign對齊text-align/vertical-align
width/height尺寸width/height
bgcolor背景色background
nowrap禁止換行white-space: nowrap

結構化標籤(保留、語義化、無障礙關鍵)

  • <caption>:表格標題,**必須緊跟 <table> 之後**,螢幕閱讀器優先朗讀。
  • <thead>, <tbody>, <tfoot>:語義分組,打印時 tfoot 可重複底部,滾動時 thead 可固定 (CSS position: sticky)。
  • <colgroup>, <col>:列組/列定義,可統一設置寬度、樣式(<col span="2" style="width: 200px;">)。

現代最佳實踐模板

<table class="data-table">
  <caption>2024 年季度銷售報表</caption>
  <colgroup>
    <col style="width: 120px;">
    <col span="3" style="width: 150px;">
  </colgroup>
  <thead>
    <tr><th scope="col">區域</th><th scope="col">Q1</th><th scope="col">Q2</th><th scope="col">Q3</th></tr>
  </thead>
  <tbody>
    <tr><th scope="row">華東</th><td>120</td><td>135</td><td>142</td></tr>
    <tr><th scope="row">華南</th><td>98</td><td>110</td><td>118</td></tr>
  </tbody>
  <tfoot>
    <tr><th scope="row">合計</th><td>218</td><td>245</td><td>260</td></tr>
  </tfoot>
</table>
.data-table { border-collapse: collapse; width: 100%; font-size: 0.9rem; }
.data-table th, .data-table td { border: 1px solid #ddd; padding: 0.5rem 0.75rem; text-align: center; }
.data-table thead th { background: #f8f9fa; font-weight: 600; }
.data-table tbody tr:nth-child(even) { background: #fafafa; }
.data-table tfoot th, .data-table tfoot td { font-weight: bold; background: #e9ecef; }
copyright © 萬盛學電腦網 all rights reserved