示例工程 20-table-complex

20-table-complex/app/index.html

html · 6.7 KB

运行:合并单元格与分组 · colspan / rowspan / colgroup

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>合并单元格与分组 · colspan / rowspan / colgroup</title>
    <meta name="description" content="多级表头、合并单元格、列分组与可访问写法。" />
    <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%, 880px); 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 12px; border-bottom: 1px solid #e2e8f0; text-align: left; }
      thead th { background: #f1f5f9; color: #0f172a; }
      tbody th { background: #f8fafc; color: #334155; }
      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; }
      .group-head { background: #e2e8f0; }
      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 9px; }
      }
    </style>
  </head>
  <body>
    <div class="page">
      <header class="site-header">
        <span class="brand">DATA NOTES</span>
        <nav aria-label="页内导航">
          <a href="#spans">合并表头</a>
          <a href="#colgroup">列分组</a>
          <a href="#rules">规则</a>
        </nav>
      </header>

      <main>
        <p class="eyebrow">LESSON / 20</p>
        <h1>合并单元格与分组</h1>
        <p class="lead">
          <code>colspan</code> 与 <code>rowspan</code> 让一个单元格横跨多列或多行;<code>colgroup</code> 给整列统一设置。它们强大,也容易把表格结构弄乱。
        </p>

        <section id="spans">
          <h2>1. 多级表头</h2>
          <p>「部门」跨两行,上半年与下半年各跨两列:</p>
          <div class="table-wrap">
            <table>
              <caption>2026 年部门季度预算(万元)</caption>
              <colgroup>
                <col class="dept" />
                <col span="4" class="data" />
              </colgroup>
              <thead>
                <tr>
                  <th id="dept" rowspan="2" scope="col">部门</th>
                  <th id="h1" colspan="2" scope="colgroup" class="group-head">上半年</th>
                  <th id="h2" colspan="2" scope="colgroup" class="group-head">下半年</th>
                </tr>
                <tr>
                  <th id="q1" scope="col">Q1</th>
                  <th id="q2" scope="col">Q2</th>
                  <th id="q3" scope="col">Q3</th>
                  <th id="q4" scope="col">Q4</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <th id="product" scope="row">产品部</th>
                  <td headers="product h1 q1" class="num">60</td>
                  <td headers="product h1 q2" class="num">60</td>
                  <td headers="product h2 q3" class="num">70</td>
                  <td headers="product h2 q4" class="num">70</td>
                </tr>
                <tr>
                  <th id="engineering" scope="row">工程部</th>
                  <td headers="engineering h1 q1" class="num">90</td>
                  <td headers="engineering h1 q2" class="num">90</td>
                  <td headers="engineering h2 q3" class="num">110</td>
                  <td headers="engineering h2 q4" class="num">110</td>
                </tr>
              </tbody>
              <tfoot>
                <tr>
                  <th scope="row">合计</th>
                  <td colspan="4" class="num">730</td>
                </tr>
              </tfoot>
            </table>
          </div>
          <p class="note">合并后仍要保证网格完整:每一行占用的列数相同,不能出现空洞。</p>
        </section>

        <section id="colgroup">
          <h2>2. colgroup 列分组</h2>
          <p><code>colgroup</code> 放在 caption 之后、thead 之前,用来给整列设置宽度或样式。</p>
          <ul>
            <li><code>col span="4"</code> 一次覆盖四列。</li>
            <li>列分组不改变数据语义,只影响呈现。</li>
            <li>读屏不会因为 colgroup 改变表头关联。</li>
          </ul>
        </section>

        <section id="rules">
          <h2>3. 规则</h2>
          <ul>
            <li>先画网格,再合并:确认每行占满同样列数</li>
            <li>表头分组用 <code>scope="colgroup"</code></li>
            <li>合并表头时,数据单元格的 <code>headers</code> 要覆盖所有层级</li>
            <li>不要用合并单元格做视觉布局</li>
            <li>合并越多,越要测试读屏与窄屏</li>
            <li><code>colgroup</code> 只做呈现,不替代表头</li>
          </ul>
        </section>
      </main>

      <footer>
        <p>示例数据为虚构,仅用于教学。</p>
      </footer>
    </div>
  </body>
</html>