示例工程 12-article-toc

12-article-toc/app/index.html

html · 6.6 KB

运行:为团队写一份可维护的 HTML 规范

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

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>为团队写一份可维护的 HTML 规范</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.75;
      }
      .page { width: min(100%, 820px); margin: 0 auto; }
      .skip { position: absolute; left: -9999px; top: 0; padding: 8px 12px; background: #2563eb; color: #fff; }
      .skip:focus { left: 8px; top: 8px; z-index: 10; }
      .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; }
      .site-header a { color: #2563eb; font-size: 13px; text-decoration: none; }
      main { padding: 28px 0; }
      article { padding: 24px; border: 1px solid #e2e8f0; border-radius: 14px; background: #fff; }
      article > header { padding-bottom: 18px; border-bottom: 1px solid #e2e8f0; }
      .eyebrow { margin: 0; color: #64748b; font-size: 11px; letter-spacing: 0.18em; }
      h1 { margin: 6px 0 10px; font-size: clamp(28px, 5.4vw, 40px); line-height: 1.2; }
      .lead { margin: 0 0 10px; color: #334155; font-size: 17px; }
      .meta { margin: 0; color: #64748b; font-size: 13px; }
      .toc { margin: 22px 0; padding: 16px 18px; border: 1px solid #e2e8f0; border-radius: 10px; background: #f8fafc; }
      .toc h2 { margin: 0 0 8px; font-size: 15px; letter-spacing: 0.08em; color: #475569; }
      .toc ul { margin: 0; padding-left: 20px; }
      .toc li + li { margin-top: 4px; }
      .toc a { color: #2563eb; font-size: 14px; }
      section { margin-top: 28px; }
      h2 { margin: 0 0 10px; font-size: 23px; }
      h3 { margin: 18px 0 6px; font-size: 17px; }
      p { margin: 0 0 12px; color: #334155; }
      ul, ol { margin: 8px 0 12px; padding-left: 24px; color: #334155; }
      li + li { margin-top: 4px; }
      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.article-footer { margin-top: 30px; padding-top: 16px; border-top: 1px solid #e2e8f0; color: #64748b; font-size: 13px; }
      .site-footer { margin-top: 20px; color: #64748b; font-size: 12px; }
      @media (max-width: 560px) {
        body { padding: 20px 14px; }
        article { padding: 18px; }
        .site-header { flex-direction: column; }
      }
    </style>
  </head>
  <body>
    <a class="skip" href="#main">跳到主内容</a>

    <div class="page">
      <header class="site-header" id="top">
        <span class="brand">TEAM NOTES</span>
        <a href="#toc">回到目录</a>
      </header>

      <main id="main">
        <article>
          <header>
            <p class="eyebrow">GUIDE / 2026</p>
            <h1>为团队写一份可维护的 HTML 规范</h1>
            <p class="lead">规范不是文档摆设:它让新人一次写对,让评审有据可依。</p>
            <p class="meta">林小满 · 2026-11-28 · 预计阅读 8 分钟</p>
          </header>

          <nav class="toc" id="toc" aria-label="文章目录">
            <h2>目录</h2>
            <ul>
              <li><a href="#why">1. 为什么需要规范</a></li>
              <li>
                <a href="#structure">2. 页面结构</a>
                <ul>
                  <li><a href="#headings">2.1 标题层级</a></li>
                  <li><a href="#lists">2.2 列表与导航</a></li>
                </ul>
              </li>
              <li><a href="#links">3. 链接与锚点</a></li>
              <li><a href="#checklist">4. 评审清单</a></li>
            </ul>
          </nav>

          <section id="why">
            <h2>1. 为什么需要规范</h2>
            <p>没有规范,每个人按自己的习惯写:有人用 <code>div</code> 堆标题,有人给每个链接加新窗口。</p>
            <p>规范把「怎么做」和「为什么」写在一起,评审时不用重复争论。</p>
            <p>规范也要能演进:先覆盖高频问题,再逐步补充。</p>
          </section>

          <section id="structure">
            <h2>2. 页面结构</h2>
            <p>结构先用语义元素搭好,再考虑样式。结构错了,CSS 只能掩盖问题。</p>

            <section id="headings">
              <h3>2.1 标题层级</h3>
              <p>一个页面一个 <code>h1</code>,分区用 <code>h2</code>,子主题用 <code>h3</code>,不跳级。</p>
              <p>标题写名词短语,不写整段话,也不用来调字号。</p>
            </section>

            <section id="lists">
              <h3>2.2 列表与导航</h3>
              <p>导航、目录、相关阅读都用列表包链接,读屏才能播报项目数。</p>
              <p>子列表放进父项 <code>li</code>,嵌套不超过三层。</p>
            </section>
          </section>

          <section id="links">
            <h2>3. 链接与锚点</h2>
            <p>站内链接用相对路径,锚点指向唯一的 <code>id</code>。</p>
            <p>每个长页面提供「回到目录」或「回到顶部」,读者不用一路滚回去。</p>
            <p class="note">锚点目标要存在,跳转后目标会获得焦点,键盘用户可以继续操作。</p>
          </section>

          <section id="checklist">
            <h2>4. 评审清单</h2>
            <ol>
              <li>一个 <code>h1</code>,标题不跳级</li>
              <li>目录链接与标题一一对应</li>
              <li>导航与目录用列表</li>
              <li>锚点目标存在</li>
              <li>长页面有回到目录的入口</li>
            </ol>
            <p>清单不长,但每次评审都跑一遍,问题会少很多。</p>
          </section>

          <footer class="article-footer">
            <p>标签:HTML · 团队规范 · 可访问性</p>
            <p><a href="#toc">回到目录</a></p>
          </footer>
        </article>
      </main>

      <footer class="site-footer">
        <p>示例数据,仅用于教学。</p>
      </footer>
    </div>
  </body>
</html>