示例工程 26-css-variables

26-css-variables/app/index.html

html · 3.7 KB

运行:CSS 变量实验室

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

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>CSS 变量实验室</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <input type="checkbox" id="theme" class="theme-input" />
    <label class="theme-switch" for="theme">深色主题</label>

    <div class="app">
      <header class="site-header">
        <span class="brand">TOKEN LAB</span>
        <nav aria-label="页面导航">
          <a href="#tokens">token</a>
          <a href="#scope">作用域</a>
          <a href="#fallback">回退</a>
          <a href="#calc">计算</a>
        </nav>
      </header>

      <main class="page">
        <p class="eyebrow">LESSON / 26</p>
        <h1>把设计决定收进变量</h1>
        <p class="lead">
          颜色、间距、圆角只写一次;主题切换与组件变体只改变量值,不改规则。
        </p>

        <section class="panel" id="tokens">
          <h2>1. 全局 token</h2>
          <p>这些值定义在 <code>:root</code>,所有组件共用。</p>
          <div class="token-grid">
            <div class="swatch bg-primary">--color-primary</div>
            <div class="swatch bg-accent">--color-accent</div>
            <div class="swatch bg-surface">--color-surface</div>
            <div class="swatch bg-muted">--color-muted</div>
          </div>
          <p class="spacing-demo">这段文字的上下间距来自 <code>--space-4</code>。</p>
        </section>

        <section class="panel" id="scope">
          <h2>2. 组件作用域</h2>
          <p>同一张卡片,第二张覆盖了 <code>--card-accent</code>,其余样式不动。</p>
          <div class="cards">
            <article class="card">
              <span class="card-tag">默认</span>
              <h3>使用全局强调色</h3>
              <p>卡片只引用变量,不写死颜色。</p>
              <a class="card-link" href="#scope">查看</a>
            </article>
            <article class="card alt">
              <span class="card-tag">局部覆盖</span>
              <h3>在卡片上改变量</h3>
              <p><code>--card-accent</code> 只影响这张卡片。</p>
              <a class="card-link" href="#scope">查看</a>
            </article>
          </div>
        </section>

        <section class="panel" id="fallback">
          <h2>3. 回退值</h2>
          <p>变量没定义时,<code>var(--name, 回退)</code> 会使用回退值。</p>
          <div class="pills">
            <span class="pill">未定义,使用回退</span>
            <span class="pill custom">定义后使用变量</span>
          </div>
          <pre><code>.pill {
  background: var(--pill-bg, #f59e0b);
  color: var(--pill-fg, #451a03);
}

.pill.custom {
  --pill-bg: #2563eb;
  --pill-fg: #ffffff;
}</code></pre>
        </section>

        <section class="panel" id="calc">
          <h2>4. 用 calc 组合变量</h2>
          <p><code>--space-2</code> 是 8px;下面这块内边距是 <code>calc(var(--space-2) * 3)</code>。</p>
          <div class="calc-box">padding: calc(var(--space-2) * 3)</div>
        </section>

        <section class="panel">
          <h2>5. 主题切换</h2>
          <p>勾选右上角的「深色主题」,<code>.app</code> 上的变量整体换一套。</p>
          <div class="theme-preview">
            <span class="dot"></span>
            <span>背景、文字、边框、强调色都来自同一组变量。</span>
          </div>
        </section>
      </main>

      <footer class="site-footer">
        <p>示例数据,仅用于教程演示。</p>
        <p>CSS VARIABLES · 2026</p>
      </footer>
    </div>
  </body>
</html>