設定 HTML 表格邊框顏色,早期使用 `bordercolor`、`bordercolorlight`、`bordercolordark` 等屬性,現代開發完全改用 CSS `border-color`、`border` 簡寫屬性。本文對比新舊寫法,說明為何應棄用 HTML 屬性,並提供 CSS 完整範例:整體邊框、單側邊框、儲存格邊框、表頭/表身/表尾差異化、合併邊框模型等實用技巧。
| 屬性 | 套用對象 | 說明 |
|---|---|---|
bordercolor | `<table>` | 整體邊框顏色(IE 專屬,非標準) |
bordercolorlight | `<table>` | 立體邊框亮面顏色(IE 專屬) |
bordercolordark | `<table>` | 立體邊框暗面顏色(IE 專屬) |
bgcolor | `table`/`tr`/`td`/`th` | 背景色,非邊框色 |
⚠️ 這些屬性**不在 HTML 標準中**,各瀏覽器渲染差異極大,新專案嚴禁使用。
table {
border-collapse: collapse; /* 關鍵:合併相鄰邊框為單線 */
width: 100%;
}
th, td {
border: 1px solid #ddd; /* 統一儲存格邊框 */
padding: 10px 12px;
}
table {
border: 2px solid #2c3e50; /* 外框顏色、粗細、樣式 */
border-collapse: collapse;
}
thead th {
border-bottom: 3px solid #2c3e50; /* 表頭底部加粗分隔 */
}
tbody tr:last-child td {
border-bottom: 2px solid #2c3e50; /* 表身最後一列底部加粗 */
}
tfoot td {
border-top: 2px solid #2c3e50; /* 表尾頂部分隔 */
background: #f8f9fa;
}
/* 僅顯示垂直分隔線 */
.vertical-lines th,
.vertical-lines td {
border-left: 1px solid #ddd;
border-right: 1px solid #ddd;
border-top: none;
border-bottom: none;
}
.vertical-lines th:first-child,
.vertical-lines td:first-child { border-left: none; }
.vertical-lines th:last-child,
.vertical-lines td:last-child { border-right: none; }
/* 僅顯示水平分隔線 */
.horizontal-lines th,
.horizontal-lines td {
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
border-left: none;
border-right: none;
}
<style>
.highlight-cell { border: 3px solid #e74c3c !important; }
.no-right-border { border-right: none !important; }
</style>
<td class="highlight-cell">重點資料</td>
<td class="no-right-border">無右邊框</td>
tbody tr:nth-child(even) td {
background: #f8f9fa;
border-color: #eee; /* 淺色列邊框更淡 */
}
tbody tr:hover td {
background: #e8f4fd;
border-color: #3498db; /* 懸停列邊框變色 */
}
<style>
.table-pro {
border-collapse: collapse;
width: 100%;
font-family: system-ui, sans-serif;
font-size: 0.9rem;
}
.table-pro th,
.table-pro td {
padding: 12px 16px;
text-align: left;
vertical-align: middle;
}
/* 外框 */
.table-pro {
border: 2px solid #1a252f;
border-radius: 6px;
overflow: hidden;
}
/* 表頭 */
.table-pro thead th {
background: #1a252f;
color: #fff;
border-bottom: 2px solid #2c3e50;
border-right: 1px solid #2c3e50;
}
.table-pro thead th:last-child { border-right: none; }
/* 表身 */
.table-pro tbody td {
border-bottom: 1px solid #eaecef;
border-right: 1px solid #eaecef;
}
.table-pro tbody tr:last-child td { border-bottom: none; }
.table-pro tbody td:last-child { border-right: none; }
/* 斑馬紋 */
.table-pro tbody tr:nth-child(even) td { background: #f8f9fa; }
/* 懸停 */
.table-pro tbody tr:hover td { background: #e8f4fd; }
/* 表尾 */
.table-pro tfoot td {
border-top: 2px solid #2c3e50;
background: #f1f3f5;
font-weight: 600;
}
</style>
<table class="table-pro">
<caption>2024 Q1 產品銷售彙總</caption>
<thead>
<tr><th scope="col">產品</th><th scope="col">單價</th><th scope="col">數量</th><th scope="col">金額</th></tr>
</thead>
<tbody>
<tr><td>筆電</td><td>$25,000</td><td>12</td><td>$300,000</td></tr>
<tr><td>滑鼠</td><td>$500</td><td>50</td><td>$25,000</td></tr>
<tr><td>鍵盤</td><td>$1,200</td><td>8</td><td>$9,600</td></tr>
</tbody>
<tfoot>
<tr><td colspan="3">總計</td><td>$334,600</td></tr>
</tfoot>
</table>
表格邊框顏色設定請**完全使用 CSS**,核心原則:border-collapse: collapse 合併邊框 → 統一 `th, td { border: 1px solid #色碼 }` → 再針對 `thead`、`tfoot`、首末列、懸停狀態做差異化調整。建立專案通用的表格樣式模組(如 `.table-pro`),確保全站一致性與維護效率。