表格背景色不只是裝飾,合理的色彩設計能提升資料可讀性、引導視線焦點、滿足無障礙標準。本文涵蓋靜態色彩、動態漸層、條紋效果與響應式適配。
| 舊屬性 (棄用) | CSS 替代 | 範例 |
|---|---|---|
bgcolor="#ff9900" |
background-color: #ff9900 |
單一背景色 |
background="img.jpg" |
background-image: url(img.jpg) |
背景圖片 |
bordercolor="#00ff99" |
border-color: #00ff99 |
邊框顏色 |
.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;
}
.table-hover tbody tr:hover {
background-color: #e8f4fd;
transition: background-color 0.15s ease;
}
/* 高亮整行但保持邊框 */
.table-hover tbody tr:hover td {
background-color: #e8f4fd;
}
.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);
}
<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>
| 文字大小 | 一般文字對比度 | 大號文字 (18px+/14px+bold) |
|---|---|---|
| AA 級 | 4.5:1 | 3:1 |
| AAA 級 | 7:1 | 4.5:1 |
@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>
專業的表格背景設計,讓數據會說話。