掌握基本表格結構是 HTML 入門必修課。本文提供標準、語義化的 3 行 3 列表格模板。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>三行三列空白表格示例</title>
<style>
table { border-collapse: collapse; margin: 20px auto; }
td, th { border: 1px solid #999; padding: 15px; min-width: 80px; text-align: center; }
</style>
</head>
<body>
<table>
<tr>
<td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td>
</tr>
</table>
</body>
</html>
| 標籤 | 作用 | 屬性示例 |
|---|---|---|
<table> | 表格容器 | width, border, align (HTML4), CSS 控制樣式 |
<tr> | 表格行 | align, valign (棄用) |
<td> | 數據單元格 | colspan, rowspan, width, height |
<th> | 表頭單元格 | scope="col|row", colspan, rowspan |
<caption> | 表格標題 | 緊跟 table 之後 |
| 屬性 | HTML4 寫法 | HTML5/CSS 推薦 |
|---|---|---|
| 邊框 | <table border="1"> | CSS: border: 1px solid #ccc; border-collapse: collapse; |
| 寬度 | <table width="200"> | CSS: width: 200px; 或 width: 100%; |
| 居中 | <table align="center"> | CSS: margin: 0 auto; |
| 單元格間距 | cellspacing="0" | CSS: border-collapse: collapse; |
| 單元格內邊距 | cellpadding="5" | CSS: td, th { padding: 5px; } |
<table>
<caption>課程安排表</caption>
<thead>
<tr><th scope="col">時間</th><th scope="col">周一</th><th scope="col">周二</th></tr>
</thead>
<tbody>
<tr><th scope="row">08:00-09:40</th><td>數學</td><td>英語</td></tr>
<tr><th scope="row">10:00-11:40</th><td>物理</td><td>化學</td></tr>
<tr><th scope="row">14:00-15:40</th><td colspan="2">體育(操場)</td></tr>
</tbody>
</table>
<caption> 提供表格標題<th scope="col|row"> 明確表頭關聯方向<thead>, <tbody>, <tfoot> 語義化分組summary 屬性(HTML5 棄用)id + headers 屬性關聯<div style="overflow-x: auto;">
<table>...</table>
</div>
/* 或 CSS 卡片式佈局 */
@media (max-width: 600px) {
table, thead, tbody, th, td, tr { display: block; }
tr { margin-bottom: 1rem; border: 1px solid #ddd; }
td { display: flex; justify-content: space-between; padding: 0.5rem; }
td::before { content: attr(data-label); font-weight: bold; }
}
從簡單的 3×3 表格開始,逐步掌握語義化結構、無障礙屬性、響應式適配,是前端開發表格技能的成長路徑。