示例工程 18-table-basics

18-table-basics/demo-02-structure/index.html

html · 2.4 KB

运行:表格结构演示

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

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>表格结构演示</title>
    <style>
      body { margin: 0; padding: 32px; background: #f8fafc; color: #0f172a; font-family: system-ui, sans-serif; line-height: 1.7; }
      .box { max-width: 720px; margin-bottom: 16px; padding: 18px; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
      h1 { margin: 0 0 16px; font-size: 28px; }
      h2 { margin: 0 0 8px; font-size: 17px; }
      p { margin: 0 0 10px; color: #334155; }
      table { width: 100%; border-collapse: collapse; font-size: 14px; }
      caption { padding: 6px 8px; text-align: left; color: #475569; font-size: 13px; }
      th, td { padding: 8px 10px; border-bottom: 1px solid #e2e8f0; text-align: left; }
      thead th { background: #f1f5f9; }
      tbody th { background: #f8fafc; }
      td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
      pre { margin: 8px 0 0; padding: 12px; overflow-x: auto; border-radius: 8px; background: #0f172a; color: #e2e8f0; font-size: 12px; }
      code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, monospace; }
      pre code { padding: 0; background: none; }
    </style>
  </head>
  <body>
    <h1>表格结构</h1>

    <div class="box">
      <h2>行表头与空值</h2>
      <table>
        <caption>库存检查(件)</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">上海</th><td class="num">120</td><td class="num">8</td></tr>
          <tr><th scope="row">成都</th><td class="num">0</td><td class="num">—</td></tr>
          <tr><th scope="row">广州</th><td class="num">64</td><td class="num">3</td></tr>
        </tbody>
      </table>
      <p>没有锁定量时写「—」,比留空更清楚。</p>
    </div>

    <div class="box">
      <h2>不要用表格布局</h2>
      <p>下面这种写法是反例:</p>
      <pre><code>&lt;table&gt;
  &lt;tr&gt;
    &lt;td&gt;左侧导航&lt;/td&gt;
    &lt;td&gt;右侧正文&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;</code></pre>
      <p>布局用 CSS Grid / Flex,表格只表达数据关系。</p>
    </div>
  </body>
</html>