示例工程 48-preprocessors

48-preprocessors/demo-01-variables/index.html

html · 0.8 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>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <h1>变量对照</h1>
    <p>同一个组件,换一个 data-theme。</p>

    <div class="card">
      <span class="label">DEFAULT</span>
      <h2>自定义属性</h2>
      <p>运行时可以改。</p>
    </div>

    <div class="card" data-theme="alt">
      <span class="label">ALT</span>
      <h2>局部覆盖</h2>
      <p>只改这个子树的变量。</p>
    </div>

    <pre><code>/* Sass:编译后消失 */
$brand: #2563eb;
.card { background: $brand; }

/* 原生:运行时仍在 */
:root { --brand: #2563eb; }
.card { background: var(--brand); }</code></pre>
  </body>
</html>