標準表格骨架
<table>
<caption>表格標題(螢幕閱讀器會朗讀)</caption>
<thead>
<tr>
<th scope="col">欄位 1</th>
<th scope="col">欄位 2</th>
<th scope="col">欄位 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>資料 1-1</td>
<td>資料 1-2</td>
<td>資料 1-3</td>
</tr>
<tr>
<td>資料 2-1</td>
<td>資料 2-2</td>
<td>資料 2-3</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3">合計/摘要資訊</td>
</tr>
</tfoot>
</table>
核心屬性對照(HTML5 建議改用 CSS)
| 屬性 | 標籤 | 說明 | CSS 替代 |
border | table | 邊框寬度(像素) | border: 1px solid #ccc |
cellpadding | table | 儲存格內邊距 | th,td{padding:8px} |
cellspacing | table | 儲存格間距 | border-spacing: 0 (配合 collapse) |
width | table | 表格寬度 | width: 100% / max-width |
align | table | 表格水平對齊 | margin: 0 auto 置中 |
bgcolor | table/tr/td | 背景色 | background-color |
bordercolor | table | 邊框顏色 | border-color |
現代 CSS 樣式化範例
<style>
.table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.table th, .table td { border: 1px solid #dee2e6; padding: 0.75rem; text-align: left; }
.table thead th { background: #f8f9fa; font-weight: 600; position: sticky; top: 0; z-index: 1; }
.table tbody tr:nth-child(even) { background: #fdfdfd; }
.table tbody tr:hover { background: #eef5ff; }
.table caption { caption-side: bottom; padding: 0.5rem; font-style: italic; color: #666; }
</style>
<div class="table-wrapper"> <!-- 響應式包裹 -->
<table class="table">...</table>
</div>
響應式表格三大策略
- 水平滾動包裹:
.table-wrapper { overflow-x: auto; }(最簡單、資料完整)。
- 關鍵欄固定:
th:first-child, td:first-child { position: sticky; left: 0; }。
- 卡片式轉換:小螢幕下用 CSS Grid 將每列轉為卡片(需 JS 輔助或
data-label 屬性)。