萬盛學電腦網

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

HTML 表格背景色設計:漸層、條紋、響應式與無障礙對比度

表格背景色設計完全指南:美觀、可讀、無障礙

表格背景色不只是裝飾,合理的色彩設計能提升資料可讀性、引導視線焦點、滿足無障礙標準。本文涵蓋靜態色彩、動態漸層、條紋效果與響應式適配。

基礎背景色屬性(舊屬性 vs CSS)

舊屬性 (棄用) CSS 替代 範例
bgcolor="#ff9900" background-color: #ff9900 單一背景色
background="img.jpg" background-image: url(img.jpg) 背景圖片
bordercolor="#00ff99" border-color: #00ff99 邊框顏色

現代 CSS 背景色技巧

1. 斑馬紋(改善行追蹤)

.table-striped tbody tr:nth-child(even) {
  background-color: #f5f5f5;
}

/* 更細緻控制:跳過合併儲存格行 */
.table-striped tbody tr:not([style*="display: none"]):nth-child(even) td:not([rowspan]) {
  background-color: #f5f5f5;
}

2. 懸停高亮(聚焦當前行)

.table-hover tbody tr:hover {
  background-color: #e8f4fd;
  transition: background-color 0.15s ease;
}

/* 高亮整行但保持邊框 */
.table-hover tbody tr:hover td {
  background-color: #e8f4fd;
}

3. 漸層背景(視覺層次)

.table-gradient thead {
  background: linear-gradient(135deg, #336699 0%, #224466 100%);
  color: white;
}

.table-gradient tbody tr:nth-child(even) {
  background: linear-gradient(90deg, rgba(51,102,153,0.03) 0%, transparent 100%);
}

/* 欄漸層 */
.col-highlight {
  background: linear-gradient(to bottom, #fff, #f0f4f8);
}

4. 語義化色彩編碼(狀態/類別)

<style>
.status-success { background: #e8f5e9; color: #2e7d32; }
.status-warning { background: #fff8e1; color: #f57f17; }
.status-danger  { background: #fdeaea; color: #c62828; }
.status-info    { background: #e3f2fd; color: #1565c0; }

/* 使用 CSS 變數主題化 */
:root {
  --row-even: #fafafa;
  --row-hover: #e3f2fd;
  --header-bg: #336699;
  --header-color: #fff;
}
</style>

<td class="status-success">通過</td>
<td class="status-warning">待審</td>
<td class="status-danger">拒絕</td>

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

文字大小 一般文字對比度 大號文字 (18px+/14px+bold)
AA 級 4.5:1 3:1
AAA 級 7:1 4.5:1

工具檢測:WebAIM Contrast Checker

深色模式適配

@media (prefers-color-scheme: dark) {
  :root {
    --row-even: #1e1e1e;
    --row-hover: #2d2d2d;
    --header-bg: #0d47a1;
    --header-color: #fff;
    --border-color: #444;
  }
  
  .table-striped tbody tr:nth-child(even) {
    background: var(--row-even);
  }
  
  .status-success { background: #1b5e20; color: #a5d6a7; }
  .status-danger  { background: #b71c1c; color: #ffcdd2; }
}

響應式背景策略

@media (max-width: 640px) {
  /* 卡片模式下移除斑馬紋,改用卡片陰影區分 */
  .table-card tbody tr {
    background: white;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    margin-bottom: 12px;
    border-radius: 8px;
  }
  .table-card tbody tr:nth-child(even) {
    background: white; /* 重置 */
  }
  .table-card tbody tr:hover {
    box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  }
}

完整範例:企業級報表風格

<style>
.report-table {
  border-collapse: collapse;
  width: 100%;
  font-family: 'Microsoft JhengHei', system-ui, sans-serif;
  font-variant-numeric: tabular-nums;
}

.report-table thead {
  background: linear-gradient(180deg, #1a3c6e 0%, #0d2b50 100%);
  color: #fff;
}

.report-table th {
  padding: 14px 16px;
  border: none;
  border-right: 1px solid rgba(255,255,255,0.1);
  font-weight: 500;
  text-align: left;
}

.report-table tbody tr {
  border-bottom: 1px solid #e8e8e8;
  transition: background 0.1s;
}

.report-table tbody tr:nth-child(even) {
  background: #f8fafc;
}

.report-table tbody tr:hover {
  background: #eef6ff;
}

.report-table td {
  padding: 12px 16px;
  border: none;
  color: #333;
}

.report-table tfoot {
  background: #f0f2f5;
  font-weight: 600;
}

.report-table .number { text-align: right; font-family: 'SF Mono', monospace; }
.report-table .positive { color: #00a854; }
.report-table .negative { color: #e02020; }
</style>

設計原則總結

  1. 功能優先:色彩服務於資料辨識,非裝飾
  2. 對比度達標:所有文字/背景組合通過 WCAG AA
  3. 語義一致:相同語義用相同色彩系統
  4. 主題可切換:CSS 變數支援深/淺色模式
  5. 列印友善:@media print 重置背景、保證對比

專業的表格背景設計,讓數據會說話。

copyright © 萬盛學電腦網 all rights reserved