表格承載著數據報表、商品比較、價格方案等高價值內容。好的表格設計能:
| HTML 屬性(棄用) | CSS 屬性(推薦) | 說明 |
|---|---|---|
width | width | 表格/儲存格寬度,支援 px、%、auto |
height | height | 儲存格高度,通常由內容撐開 |
border | border | 邊框寬度/樣式/顏色,僅影響外框 |
bordercolor | border-color | 邊框顏色 |
border-collapse: collapse 合併邊框<table class="styled-table">
<thead><tr><th>項目</th><th>價格</th></tr></thead>
<tbody>
<tr><td>產品 A</td><td>$100</td></tr>
<tr><td>產品 B</td><td>$200</td></tr>
</tbody>
</table>
<style>
.styled-table {
border-collapse: collapse; /* 關鍵:合併相鄰邊框為單線 */
width: 100%;
max-width: 600px;
margin: 1rem auto;
font-family: system-ui, sans-serif;
}
.styled-table th,
.styled-table td {
border: 1px solid #ddd; /* 統一 1px 細線 */
padding: 12px 16px; /* 內邊距替代 cellpadding */
}
.styled-table th {
background: #336699;
color: white;
font-weight: 600;
text-align: left;
}
.styled-table tr:nth-child(even) td {
background: #f9f9f9; /* 斑馬紋 */
}
</style>
經驗值: 外框邊框寬度建議 ≤ 2px,超過 5px 視覺壓迫感過強。
<table class="bg-demo">
<tr class="row-highlight">
<td>一般儲存格</td>
<td class="cell-special">特殊儲存格</td>
</tr>
</table>
<style>
.bg-demo { background: #e8f4fd; } /* 表格級背景:最底層 */
.row-highlight { background: #fff3cd; } /* 行級背景:覆蓋表格背景 */
.cell-special { background: #d4edda; } /* 儲存格級背景:最高優先級,覆蓋行背景 */
</style>
善用層疊關係:表格整體淡藍底 → 重點行淡黃底 → 關鍵儲存格淡綠底,層次分明。
| 數據類型 | 水平對齊 | 垂直對齊 | CSS 實現 |
|---|---|---|---|
| 文字、名稱、描述 | 左對齊 | 置頂 | text-align: left; vertical-align: top; |
| 數字、金額、日期 | 右對齊 | 置中 | text-align: right; vertical-align: middle; |
| 狀態標籤、單選/複選框 | 置中 | 置中 | text-align: center; vertical-align: middle; |
| 表頭 | 置中或左對齊 | 置中 | text-align: center; vertical-align: middle; |
CSS 可針對 th、td、td:first-child、td.numeric 精準控制。
cellpadding(內邊距)vs cellspacing(外邊距)<!-- HTML 屬性寫法(不推薦) -->
<table cellpadding="10" cellspacing="5">...</table>
<!-- CSS 等效寫法(推薦) -->
<style>
.table-spacing {
border-collapse: separate; /* 必須 separate 才能使用 border-spacing */
border-spacing: 5px; /* 替代 cellspacing:儲存格間距 */
}
.table-spacing td {
padding: 10px; /* 替代 cellpadding:內容與邊框間距 */
}
</style>
視覺判斷:
padding(cellpadding)border-spacing(cellspacing)border-collapse: collapse + padding,border-spacing 失效<table class="pro-table">
<caption>2024 Q1 產品銷售統計</caption>
<thead>
<tr><th scope="col">產品</th><th scope="col" class="num">銷量</th><th scope="col" class="num">營收</th><th scope="col" class="num">增長</th></tr>
</thead>
<tbody>
<tr><td>iPhone 15</td><td class="num">12,500</td><td class="num">$1.25B</td><td class="num positive">+12%</td></tr>
<tr><td>MacBook Air</td><td class="num">8,200</td><td class="num">$980M</td><td class="num positive">+8%</td></tr>
<tr><td>iPad Pro</td><td class="num">5,600</td><td class="num">$670M</td><td class="num negative">-3%</td></tr>
</tbody>
<tfoot>
<tr><th>總計</th><th class="num">26,300</th><th class="num">$2.9B</th><th class="num">+7%</th></tr>
</tfoot>
</table>
<style>
.pro-table {
border-collapse: collapse;
width: 100%;
max-width: 800px;
margin: 2rem auto;
font-size: 0.95rem;
box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
.pro-table caption {
caption-side: top;
font-weight: 600;
font-size: 1.2rem;
padding: 1rem;
color: #333;
}
.pro-table th,
.pro-table td {
border: 1px solid #e0e0e0;
padding: 12px 16px;
}
.pro-table th {
background: linear-gradient(180deg, #4a90d9 0%, #336699 100%);
color: white;
font-weight: 600;
text-align: center;
}
.pro-table td { text-align: left; vertical-align: middle; }
.pro-table td.num { text-align: right; font-variant-numeric: tabular-nums; }
.pro-table td.positive { color: #2e7d32; font-weight: 500; }
.pro-table td.negative { color: #c62828; font-weight: 500; }
.pro-table tbody tr:nth-child(even) { background: #fafafa; }
.pro-table tbody tr:hover { background: #e3f2fd; }
.pro-table tfoot th { background: #f5f5f5; font-weight: 700; }
</style>
<div class="table-responsive">
<table class="pro-table">...</table>
</div>
<style>
.table-responsive {
overflow-x: auto;
-webkit-overflow-scrolling: touch;
margin: 1rem -1rem;
padding: 1rem;
}
@media (max-width: 600px) {
.pro-table { min-width: 600px; } /* 強制最小寬度觸發橫向滾動 */
}
</style>
表格美化的核心邏輯:結構語義化(<thead>/<tbody>/<tfoot>/<caption>/scope) 為基礎,CSS 分層控制(邊框合併 → 背景層疊 → 類型化對齊 → 間距節奏) 為手段,響應式容器 為保障。拒絕 HTML 屬性硬編碼,擁抱 CSS 靈活賦能。