萬盛學電腦網

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

HTML 表格顏色樣式完整指南:CSS 替代屬性、語意結構與現代設計系統整合

HTML 表格 `

` 的顏色控制早已從屬性 (`bgcolor`, `bordercolor`) 遷移至 CSS。本文系統整理表格相關 CSS 屬性、語意結構標籤、斑馬紋、懸停高亮、響應式、列印樣式與設計系統 Token 整合,助你打造美觀、無障礙、可維護的資料表格。

棄用屬性 → CSS 對照表 (必讀)

棄用 HTML 屬性CSS 替代屬性說明
bgcolorbackground-color背景顏色
bordercolorborder-color邊框顏色
border (數值)border-width / border 簡寫邊框寬度
cellspacingborder-collapse: separate; border-spacing: X儲存格間距
cellpaddingpadding (於 td/th)內容與邊框間距
widthwidth表格寬度
alignmargin-inline: auto / `text-align`表格水平置中 / 文字對齊
valignvertical-align (於 td/th)垂直對齊

語意化表格結構 (無障礙基石)

<table>
  <caption>2024 Q3 產品銷售統計</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">產品 A</th>
      <td>$1,200</td>
      <td>150</td>
      <td>$180,000</td>
    </tr>
    <tr>
      <th scope="row">產品 B</th>
      <td>$2,500</td>
      <td>80</td>
      <td>$200,000</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <th scope="row" colspan="2">合計</th>
      <td>230</td>
      <td>$380,000</td>
    </tr>
  </tfoot>
</table>