HTML 表格用於呈現表格式資料 (tabular data),而非版面排版。正確結構包含 <table>、<tr> (列)、<th> (標題儲存格)、<td> (資料儲存格),並可加入 <caption>、<thead>/<tbody>/<tfoot> 等語意區段。
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<title>基本表格範例</title>
<style>
table { border-collapse: collapse; width: 100%; max-width: 400px; margin: 1rem auto; }
th, td { border: 1px solid #333; padding: 8px; text-align: center; }
th { background: #f0f0f0; }
</style>
</head>
<body>
<table>
<caption>三行三列範例表格</caption>
<thead>
<tr><th>欄位 A</th><th>欄位 B</th><th>欄位 C</th></tr>
</thead>
<tbody>
<tr><td>資料 1</td><td>資料 2</td><td>資料 3</td></tr>
<tr><td>資料 4</td><td>資料 5</td><td>資料 6</td></tr>
<tr><td>資料 7</td><td>資料 8</td><td>資料 9</td></tr>
</tbody>
</table>
</body>
</html>
| 標籤 | 全名 | 功能 | 必要性 |
|---|---|---|---|
<table> | Table | 表格容器 | 必須 |
<caption> | Caption | 表格標題/說明,置於 table 直後,利於無障礙 | 建議 |
<thead> | Table Head | 表頭區段 (標題列群組) | 建議 |
<tbody> | Table Body | 主要資料區段 | 建議 |
<tfoot> | Table Foot | 表尾區段 (總計、備註) | 選用 |
<tr> | Table Row | 列容器 | 必須 |
<th> | Table Header | 標題儲存格,預設粗體置中,scope="col|row" 指定範圍 | 表頭必須 |
<td> | Table Data | 資料儲存格 | 必須 |
border:邊框寬度 (像素),border="1" 顯示細線。cellpadding:儲存格內邊距 (內容到邊框)。cellspacing:儲存格間距 (邊框之間)。width:表格寬度 (像素或百分比)。align:表格水平對齊 (left|center|right)。現代最佳實務:移除所有表格表現屬性,完全交由 CSS 控制 (border-collapse, padding, margin, width),結構與樣式分離。
<caption> 描述表格用途?<th> 是否加 scope="col" 或 scope="row"?<thead>/<tbody>/<tfoot> 區分區段?<colgroup> 定義欄寬?正確的表格結構,讓螢幕閱讀器使用者也能輕鬆理解資料關係。