示例工程 20-table-complex

20-table-complex/demo-02-colgroup/index.html

html · 2.4 KB

运行:colgroup 演示

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

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>colgroup 演示</title>
    <style>
      body { margin: 0; padding: 32px; background: #f8fafc; color: #0f172a; font-family: system-ui, sans-serif; line-height: 1.7; }
      .box { max-width: 760px; 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; }
      col.dept { width: 30%; }
      col.data { background: #fcfdff; }
      pre { margin: 8px 0 0; padding: 12px; overflow-x: auto; border-radius: 8px; background: #0f172a; color: #e2e8f0; font-size: 12px; }
    </style>
  </head>
  <body>
    <h1>colgroup 演示</h1>

    <div class="box">
      <h2>列宽与列背景</h2>
      <table>
        <caption>区域库存(件)</caption>
        <colgroup>
          <col class="dept" />
          <col span="3" class="data" />
        </colgroup>
        <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">8</td><td class="num">40</td></tr>
          <tr><th scope="row">成都</th><td class="num">0</td><td class="num">0</td><td class="num">20</td></tr>
        </tbody>
      </table>
    </div>

    <div class="box">
      <h2>位置</h2>
      <pre>&lt;table&gt;
  &lt;caption&gt;…&lt;/caption&gt;
  &lt;colgroup&gt;
    &lt;col class="dept"&gt;
    &lt;col span="3" class="data"&gt;
  &lt;/colgroup&gt;
  &lt;thead&gt;…&lt;/thead&gt;</pre>
      <p>colgroup 在 caption 之后、thead 之前,只影响呈现。</p>
    </div>
  </body>
</html>