示例工程 18-table-basics
18-table-basics/app/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>表格基础 · table / caption / thead / tbody</title>
<meta name="description" content="用 table、caption、thead、tbody、tfoot 表达二维数据。" />
<style>
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
padding: 32px 20px;
background: #f8fafc;
color: #0f172a;
font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
line-height: 1.7;
}
.page { width: min(100%, 820px); margin: 0 auto; }
.site-header { display: flex; justify-content: space-between; gap: 16px; padding-bottom: 14px; border-bottom: 1px solid #e2e8f0; }
.brand { font-weight: 700; letter-spacing: 0.14em; font-size: 13px; }
nav a { color: #2563eb; font-size: 13px; text-decoration: none; }
nav a + a { margin-left: 14px; }
main { padding: 28px 0; }
.eyebrow { margin: 0; color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
h1 { margin: 6px 0 12px; font-size: clamp(30px, 6vw, 44px); line-height: 1.15; }
.lead { margin: 0 0 22px; color: #334155; font-size: 17px; }
section { margin-top: 28px; padding-top: 20px; border-top: 1px solid #e2e8f0; }
h2 { margin: 0 0 10px; font-size: 22px; }
p { margin: 0 0 12px; color: #334155; }
ul { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
li + li { margin-top: 4px; }
.table-wrap { overflow-x: auto; border: 1px solid #e2e8f0; border-radius: 10px; background: #fff; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
caption { padding: 12px 14px 6px; text-align: left; color: #475569; font-size: 13px; }
th, td { padding: 9px 14px; border-bottom: 1px solid #e2e8f0; text-align: left; }
thead th { background: #f1f5f9; color: #0f172a; font-weight: 600; }
tbody th { font-weight: 600; color: #334155; }
tbody tr:last-child td, tbody tr:last-child th { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tfoot th, tfoot td { background: #f8fafc; font-weight: 600; border-top: 2px solid #cbd5e1; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, Menlo, monospace; font-size: 0.92em; }
.note { padding: 12px 14px; border-left: 4px solid #2563eb; border-radius: 6px; background: #eff6ff; color: #1e3a8a; font-size: 14px; }
footer { margin-top: 32px; padding-top: 16px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 12px; }
@media (max-width: 560px) {
body { padding: 20px 14px 32px; }
.site-header { flex-direction: column; }
th, td { padding: 8px 10px; }
}
</style>
</head>
<body>
<div class="page">
<header class="site-header">
<span class="brand">DATA NOTES</span>
<nav aria-label="页内导航">
<a href="#basic">基本表格</a>
<a href="#rowheaders">行表头</a>
<a href="#rules">规则</a>
</nav>
</header>
<main>
<p class="eyebrow">LESSON / 18</p>
<h1>表格基础</h1>
<p class="lead">
表格用于二维数据:行与列交叉出单元格。它能表达「谁和谁的交叉值」,这是列表做不到的。
</p>
<section id="basic">
<h2>1. 基本表格</h2>
<p>表头在 <code>thead</code>,数据在 <code>tbody</code>,合计在 <code>tfoot</code>。</p>
<div class="table-wrap">
<table>
<caption>2026 年季度访问与转化</caption>
<thead>
<tr>
<th scope="col">季度</th>
<th scope="col" class="num">访问量</th>
<th scope="col" class="num">转化</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Q1</th>
<td class="num">1,200</td>
<td class="num">36</td>
</tr>
<tr>
<th scope="row">Q2</th>
<td class="num">1,500</td>
<td class="num">45</td>
</tr>
<tr>
<th scope="row">Q3</th>
<td class="num">1,800</td>
<td class="num">54</td>
</tr>
<tr>
<th scope="row">Q4</th>
<td class="num">2,000</td>
<td class="num">60</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">合计</th>
<td class="num">6,500</td>
<td class="num">195</td>
</tr>
</tfoot>
</table>
</div>
<p class="note">数字列右对齐、等宽数字,读者才能快速比较大小。</p>
</section>
<section id="rowheaders">
<h2>2. 行表头</h2>
<p>第一列是行名时,用 <code>th scope="row"</code>,读屏才能把行名与数值关联起来。</p>
<div class="table-wrap">
<table>
<caption>部门预算执行(万元)</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>
<th scope="row">产品部</th>
<td class="num">120</td>
<td class="num">110</td>
<td class="num">-10</td>
</tr>
<tr>
<th scope="row">设计部</th>
<td class="num">80</td>
<td class="num">86</td>
<td class="num">+6</td>
</tr>
<tr>
<th scope="row">工程部</th>
<td class="num">200</td>
<td class="num">190</td>
<td class="num">-10</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">合计</th>
<td class="num">400</td>
<td class="num">386</td>
<td class="num">-14</td>
</tr>
</tfoot>
</table>
</div>
</section>
<section id="rules">
<h2>3. 规则</h2>
<ul>
<li>只用于二维数据,不用于页面布局</li>
<li>有 <code>caption</code> 说明表格内容</li>
<li>表头用 <code>th</code>,不要用加粗的 <code>td</code></li>
<li><code>thead</code> / <code>tbody</code> / <code>tfoot</code> 分组</li>
<li>数字右对齐,单位写在表头或标题里</li>
<li>空单元格用「—」而不是留空</li>
<li>窄屏用容器横向滚动,不压缩到不可读</li>
</ul>
</section>
</main>
<footer>
<p>示例数据为虚构,仅用于教学。</p>
</footer>
</div>
</body>
</html>