萬盛學電腦網

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

HTML 表格背景色設置完全指南:整體、列、欄、儲存格多層級控制與 CSS 最佳實踐

核心原則:使用 CSS background-color / background

廢棄 HTML bgcolor 屬性。CSS 提供更強大、可維護、可復用的背景控制能力。

四大層級背景色控制

層級選擇器範例影響範圍優先序 (高→低)典型用途
整張表格table { background: #f5f5f5; }整個表格區域 (含邊框間隙)1 (最低)表格整體底色、漸層背景
列tr:nth-child(odd) { background: #fafafa; }整列所有儲存格2斑馬紋、分組標題列、總計列高亮
欄col.highlight { background: #fff8e1; }整欄所有儲存格3關鍵欄位標示 (如價格、狀態欄)
單一儲存格td.status-critical { background: #ffebee; }單一儲存格4 (最高)狀態指示、條件格式化、編輯高亮

層疊規則:儲存格 > 欄 > 列 > 表格。後設定者勝出 (同權重下),或用 !important 強制 (不建議)。

實作範例:多層級背景色疊加

<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<title>表格多層背景色示範</title>
<style>
  .demo-table {
    border-collapse: collapse;
    width: 100%;
    max-width: 800px;
    margin: 2rem auto;
    table-layout: fixed;
    font-family: system-ui, sans-serif;
  }
  .demo-table col:nth-child(1) { width: 50px; }
  .demo-table col:nth-child(2) { width: 30%; }
  .demo-table col:nth-child(3) { width: 120px; background: #fff3e0; } /* 欄層級:價格欄淡橙底 */
  .demo-table col:nth-child(4) { width: 100px; }

  .demo-table th, .demo-table td {
    border: 1px solid #e0e0e0;
    padding: 10px 12px;
    vertical-align: middle;
  }
  .demo-table thead th {
    background: #263238;  /* 表頭深灰 */
    color: #fff;
    font-weight: 600;
  }
  .demo-table tbody tr:nth-child(odd) {
    background: #fafafa;  /* 列層級:斑馬紋 */
  }
  .demo-table tbody tr:hover {
    background: #e3f2fd;  /* 列層級:懸停高亮 (優先於斑馬紋) */
  }
  .demo-table .total-row {
    background: #fff8e1 !important; /* 列層級:總計列強制黃底 */
    font-weight: bold;
  }
  .demo-table .highlight-cell {
    background: #ffcdd2 !important; /* 儲存格層級:異常值紅底 */
    color: #c62828;
  }
</style>
</head>
<body>
<table class="demo-table">
  <colgroup><col><col><col class="highlight"><col></colgroup>
  <thead>
    <tr><th scope="col">#</th><th scope="col">商品</th><th scope="col" class="num">單價</th><th scope="col">狀態</th></tr>
  </thead>
  <tbody>
    <tr><td>1</td><td>筆電</td><td class="num">$45,900</td><td>上架</td></tr>
    <tr><td>2</td><td>滑鼠</td><td class="num highlight-cell">$1,290</td><td>缺貨</td></tr>
    <tr><td>3</td><td>螢幕</td><td class="num">$8,900</td><td>上架</td></tr>
    <tr class="total-row"><td colspan="2">合計</td><td class="num">$56,090</td><td></td></tr>
  </tbody>
</table>
</body>
</html>

進階背景效果

1. 漸層背景

table {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  /* 或針對列 */
  tr.gradient-row { background: linear-gradient(90deg, #fdfbfb 0%, #ebedee 100%); }

2. 圖片背景 (需注意文字對比度)

table {
  background: url('pattern.png') center / cover no-repeat;
  /* 儲存格需半透明背景保證可讀性 */
}
td { background: rgba(255, 255, 255, 0.9); }

3. 條件格式化 (JavaScript 動態套用類別)

function applyConditionalFormatting(tableId) {
  const table = document.getElementById(tableId);
  const rows = table.tBodies[0].rows;
  const priceCol = 2; // 價格欄索引

  for (const row of rows) {
    const cell = row.cells[priceCol];
    const value = parseFloat(cell.textContent.replace(/[^0-9.-]/g, ''));
    if (isNaN(value)) continue;

    cell.classList.remove('highlight-low', 'highlight-high');
    if (value < 1000) cell.classList.add('highlight-low');    // 綠底低價
    else if (value > 50000) cell.classList.add('highlight-high'); // 紅底高價
  }
}

/* CSS */
.highlight-low { background: #e8f5e9 !important; color: #1b5e20; }
.highlight-high { background: #ffebee !important; color: #c62828; }

4. 列群組背景 (thead/tbody/tfoot)

thead { background: #37474f; color: white; }
tbody { background: white; }
tfoot { background: #eceff1; font-weight: bold; }

無障礙與對比度要求 (WCAG 2.1 AA)

  • **文字對比度**:一般文字 ≥ 4.5:1,大字 (18pt/14pt bold) ≥ 3:1。
  • **背景色不可單獨傳達資訊** (如「紅底=錯誤」),需配合文字、圖示、邊框。
  • 測試工具:WebAIM Contrast Checker、瀏覽器開發工具 Accessibility 面板。

郵件模板 (HTML Email) 背景色相容寫法

<table width="600" border="0" cellpadding="0" cellspacing="0" bgcolor="#f5f5f5" style="background:#f5f5f5; width:600px; max-width:100%;">
  <tr>
    <td bgcolor="#ffffff" style="background:#fff; padding:20px;">
      內容區白底
    </td>
  </tr>
  <tr>
    <td bgcolor="#ffebee" style="background:#ffebee; padding:10px; text-align:center; color:#c62828;">
      <strong>⚠ 重要通知</strong>
    </td>
  </tr>
</table>
  • **雙重保險**:bgcolor + style="background:..."。
  • Outlook 不支援 background-image、漸層、nth-child、<col> 背景。
  • 全表背景常用「大表格包小表格」技巧:外層大表格設 bgcolor 填滿視窗,內層內容表格設白底。

常見坑點與解決

問題原因解決
設定 tr:hover 背景無效td 有背景色覆蓋、border-collapse: collapse 邊框疊加確保 td 無背景色,或用 tr:hover td { background: ...; } 選中儲存格
<col> 背景不生效<col> 只支援 width, background, visibility, border (有限)確認 table-layout: fixed,背景色寫在 <col style="background:..."> 或 CSS col.highlight { background:... }
合併儲存格 (rowspan/colspan) 背景異常合併儲存格背景覆蓋範圍不可預期在合併儲存格上直接設定背景,避免依賴列/欄層級疊加
列印時背景色消失瀏覽器預設不列印背景用戶需勾選「列印背景圖形」;或用 @media print { .print-bg { -webkit-print-color-adjust: exact; print-color-adjust: exact; } }

結語

表格背景色控制核心在於**理解層疊優先序** (Cell > Column > Row > Table) 並善用 CSS 選擇器 (nth-child, :hover, col, 類別) 精準著色。配合 table-layout: fixed 與 <colgroup>,能建立結構清晰、視覺優美、無障礙友善的專業資料表格。

copyright © 萬盛學電腦網 all rights reserved