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>
table {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
/* 或針對列 */
tr.gradient-row { background: linear-gradient(90deg, #fdfbfb 0%, #ebedee 100%); }
table {
background: url('pattern.png') center / cover no-repeat;
/* 儲存格需半透明背景保證可讀性 */
}
td { background: rgba(255, 255, 255, 0.9); }
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; }
thead { background: #37474f; color: white; }
tbody { background: white; }
tfoot { background: #eceff1; font-weight: bold; }
<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:..."。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>,能建立結構清晰、視覺優美、無障礙友善的專業資料表格。