示例工程 19-table-accessibility

19-table-accessibility/app/index.html

html · 6.8 KB

运行:表格可访问性 · caption / scope / headers

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

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>表格可访问性 · caption / scope / headers</title>
    <meta name="description" content="用 caption、scope 与 headers 让表格对读屏可用。" />
    <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%, 860px); 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; }
      tfoot th, tfoot td { background: #f8fafc; font-weight: 600; border-top: 2px solid #cbd5e1; }
      td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
      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">A11Y NOTES</span>
        <nav aria-label="页内导航">
          <a href="#scope">scope</a>
          <a href="#headers">headers</a>
          <a href="#rules">规则</a>
        </nav>
      </header>

      <main>
        <p class="eyebrow">LESSON / 19</p>
        <h1>表格可访问性</h1>
        <p class="lead">
          表格的可访问性不是「加个 caption」就结束。读屏需要知道每个单元格属于哪一行、哪一列。
        </p>

        <section id="scope">
          <h2>1. caption 与 scope</h2>
          <p>简单表格用 <code>th scope</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">1200</td><td class="num">36</td></tr>
                <tr><th scope="row">Q2</th><td class="num">1500</td><td class="num">45</td></tr>
                <tr><th scope="row">Q3</th><td class="num">1800</td><td class="num">54</td></tr>
              </tbody>
              <tfoot>
                <tr><th scope="row">合计</th><td class="num">4500</td><td class="num">135</td></tr>
              </tfoot>
            </table>
          </div>
          <p class="note">读屏读「1500」时,会先播报行名 Q2 与列名访问量。</p>
        </section>

        <section id="headers">
          <h2>2. 复杂表头用 headers</h2>
          <p>两级表头无法只靠 scope 表达时,给表头加 <code>id</code>,数据单元格用 <code>headers</code> 引用。</p>
          <div class="table-wrap">
            <table>
              <caption>2026 年上半年月度数据</caption>
              <thead>
                <tr>
                  <th id="month" rowspan="2" scope="col">月份</th>
                  <th id="q1" colspan="2" scope="colgroup">Q1</th>
                  <th id="q2" colspan="2" scope="colgroup">Q2</th>
                </tr>
                <tr>
                  <th id="q1-visits" scope="col">访问</th>
                  <th id="q1-conv" scope="col">转化</th>
                  <th id="q2-visits" scope="col">访问</th>
                  <th id="q2-conv" scope="col">转化</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <th id="jan" scope="row">1 月</th>
                  <td headers="jan q1-visits">1200</td>
                  <td headers="jan q1-conv">36</td>
                  <td headers="jan q2-visits">1500</td>
                  <td headers="jan q2-conv">45</td>
                </tr>
                <tr>
                  <th id="feb" scope="row">2 月</th>
                  <td headers="feb q1-visits">1300</td>
                  <td headers="feb q1-conv">39</td>
                  <td headers="feb q2-visits">1600</td>
                  <td headers="feb q2-conv">48</td>
                </tr>
              </tbody>
            </table>
          </div>
          <p><code>headers</code> 里的 id 用空格分隔,顺序不影响语义,但要覆盖所有相关表头。</p>
        </section>

        <section id="rules">
          <h2>3. 规则</h2>
          <ul>
            <li>每个表格有 <code>caption</code></li>
            <li>列名与行名用 <code>th</code></li>
            <li>简单表格用 <code>scope</code></li>
            <li>复杂表头用 <code>id</code> + <code>headers</code></li>
            <li>不要用 <code>summary</code>(已废弃)</li>
            <li>不要给单元格加多余的 <code>tabindex</code></li>
            <li>空单元格写「—」,并提供说明</li>
          </ul>
        </section>
      </main>

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