表格根元素 <table> 核心屬性
| 屬性 | 說明 | 值範例 | 現代 CSS 替代 (推薦) | 備註 |
border | 表格外框粗細 (像素)。0 或省略 = 無框線 | 1, 2 | table { border: 1px solid #ccc; border-collapse: collapse; } | HTML5 廢棄。CSS 可分別控制外框、內框、顏色、樣式 |
width | 表格寬度。像素或百分比 (相對父容器) | 600, 100% | width: 100%; max-width: 800px; | 配合 table-layout: fixed 精確控制 |
height | 表格高度 (較少用) | 300, 50% | height, min-height | 通常由內容撐開,不建議固定高度 |
cellspacing | 儲存格**間距** (儲存格邊框之間間隙) | 0, 5, 10 | table { border-collapse: separate; border-spacing: 10px; } | border-collapse: collapse 會忽略 cellspacing,邊框合併 |
cellpadding | 儲存格**內邊距** (內容到邊框距離) | 0, 10 | th, td { padding: 10px; } | CSS padding 更靈活,可分上下左右 |
bgcolor | 表格整體背景色 | #f0f0f0, lightblue | table { background-color: #f0f0f0; } | CSS 可做漸層、圖片、半透明 |
align | 表格在頁面水平對齊 | left, center, right | table { margin: 0 auto; } (置中)
table { margin-left: auto; } (靠右) | 表格為區塊元素,margin auto 置中為標準做法 |
summary | 表格摘要 (給螢幕閱讀器) | 2024年營收統計表 | <caption> + <details> 或 aria-describedby | HTML5 建議用 <caption> 替代,summary 已廢棄 |
frame | 外框顯示方式 | void, above, below, hsides, vsides, lhs, rhs, box, border | border-top/bottom/left/right CSS 控制 | 極少用,已廢棄 |
rules | 內部框線顯示方式 | none, groups, rows, cols, all | CSS border-bottom (rows), border-right (cols) 精確控制 | 已廢棄 |
列元素 <tr> 核心屬性
| 屬性 | 說明 | 值 | 現代 CSS 替代 |
align | 該列所有儲存格文字水平對齊 | left, center, right, justify, char | tr { text-align: center; } 或 td { text-align: center; } |
valign | 該列所有儲存格文字垂直對齊 | top, middle, bottom, baseline | tr { vertical-align: middle; } 或 td { vertical-align: middle; } |
bgcolor | 該列背景色 | 顏色值 | tr { background-color: #f5f5f5; } 或 tr:nth-child(odd) 斑馬紋 |
height | 列高度 | 像素/百分比 | tr { height: 40px; } 或 td { min-height: 40px; } |
儲存格元素 <td> / <th> 核心屬性
| 屬性 | 說明 | 值範例 | 現代 CSS/HTML 替代 |
colspan | 橫跨欄數 (水平合併) | 2, 3 | HTML 屬性保留,**無 CSS 替代**,必須寫在標籤上 |
rowspan | 橫跨列數 (垂直合併) | 2, 3 | HTML 屬性保留,**無 CSS 替代**,必須寫在標籤上 |
width | 儲存格寬度 | 100, 20% | <col style="width:..."> 或 td { width: ...; } |
height | 儲存格高度 | 50 | td { height: 50px; } 或 min-height |
align | 儲存格文字水平對齊 | left, center, right, justify, char | td { text-align: right; } (數字欄常用) |
valign | 儲存格文字垂直對齊 | top, middle, bottom, baseline | td { vertical-align: top; } |
bgcolor | 儲存格背景色 | 顏色值 | td { background-color: #ffffcc; } |
nowrap | 禁止自動換行 (布林屬性) | nowrap 或 nowrap="nowrap" | td { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } |
headers | 關聯標題儲存格 ID (無障礙) | id1 id2 | HTML 屬性保留,**必要**用於複雜合併表格無障礙 |
scope | 標題儲存格範圍 (無障礙) | col, row, colgroup, rowgroup | HTML 屬性保留,**必要**用於 <th> 指示範圍 |
abbr | 標題儲存格縮寫 (螢幕閱讀器讀取) | Q1 營收 | HTML 屬性保留,輔助無障礙 |
<th> (Table Header) 特殊說明
- **語意**:「標題儲存格」,預設 **粗體 + 置中** (瀏覽器預設樣式)。
- **無障礙關鍵**:**務必加上
scope 屬性**:
<th scope="col">:此欄標題 (最常見)
<th scope="row">:此列標題 (列首欄)
<th scope="colgroup">:跨欄群標題 (搭配 colspan)
<th scope="rowgroup">:跨列群標題 (搭配 rowspan)
- 可用
abbr 提供簡短版標題供螢幕閱讀器朗讀。
現代 CSS 表格樣式重構範例
<!-- HTML -->
<table class="data-table">
<colgroup>
<col style="width: 50px;">
<col style="width: 30%;">
<col style="width: 120px;">
<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><span class="badge active">上架</span></td>
</tr>
</tbody>
</table>
<style>
.data-table {
border-collapse: collapse;
width: 100%;
table-layout: fixed;
font-family: system-ui, sans-serif;
font-size: 0.9rem;
line-height: 1.5;
}
.data-table th, .data-table td {
border: 1px solid #e0e0e0;
padding: 12px 16px;
vertical-align: middle;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.data-table thead th {
background: #f5f7fa;
font-weight: 600;
color: #333;
position: sticky; top: 0; z-index: 1;
}
.data-table tbody tr:nth-child(odd) { background: #fcfcfc; }
.data-table tbody tr:hover { background: #eef6ff; }
.data-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table .badge { display: inline-block; padding: 2px 8px; border-radius: 12px; font-size: 0.75rem; font-weight: 500; }
.data-table .badge.active { background: #e8f5e9; color: #2e7d32; }
</style>
郵件模板 (HTML Email) 特殊相容寫法
郵件客戶端 (Outlook Desktop, Gmail App, Apple Mail) CSS 支援極不一致,**必須雙重寫法**:
<table width="600" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffff" style="width:600px; max-width:100%; background:#fff; border-collapse:collapse; margin:0 auto;">
<tr>
<td width="200" align="center" valign="top" bgcolor="#f5f5f5" style="width:200px; text-align:center; vertical-align:top; background:#f5f5f5; padding:20px;">
固定寬度欄位
</td>
<td align="left" valign="top" style="padding:20px;">
自適應欄位
</td>
</tr>
</table>
- 所有尺寸、對齊、背景色**同時寫 HTML 屬性與行內 style**。
- **不要使用**:
table-layout: fixed, <colgroup>, border-spacing, text-overflow: ellipsis, position: sticky, nth-child, 外部/內嵌 <style> (Gmail 剝離)。
- 圖片需
style="display:block; border:0; outline:none; text-decoration:none;" 修正間隙。
總結:屬性遷移對照速查
| 舊屬性 | 標籤 | CSS 替代 | 備註 |
border | table | border, border-collapse | CSS 可分顏色、樣式、粗細 |
cellspacing | table | border-spacing (需 border-collapse:separate) | 預設 2px,collapse 為 0 |
cellpadding | table | padding on th/td | 可分上下左右 |
width/height | table/td/th | width/height, min/max-width/height | table-layout:fixed + <col> 最佳 |
bgcolor | table/tr/td/th | background-color | 支援漸層、圖片、透明度 |
align | table | margin: 0 auto (置中) | 區塊元素置中標準做法 |
align | tr/td/th | text-align | 針對文字內容 |
valign | tr/td/th | vertical-align | 針對行內/儲存格內容 |
nowrap | td/th | white-space:nowrap + overflow | 需搭配 overflow 處理溢出 |
colspan/rowspan | td/th | **保留 HTML 屬性** | 無 CSS 替代,結構性合併 |
scope/headers | th/td | **保留 HTML 屬性** | 無障礙必要 |
結語
HTML 表格屬性多已納入 CSS 標準化管理。**新專案請全面使用 CSS**,僅保留 colspan/rowspan (結構合併)、scope/headers (無障礙) 等結構性屬性於 HTML。善用 table-layout: fixed、<colgroup>、border-collapse: collapse、偽類選擇器,即能打造高效能、高可維護、無障礙的現代化資料表格。