示例工程 40-layers
40-layers/app/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;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="tokens.css" />
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="app">
<header class="site-header">
<span class="brand">LAYER LAB</span>
<nav aria-label="页面导航">
<a href="#order">顺序</a>
<a href="#nested">嵌套</a>
<a href="#revert">回退</a>
<a href="#notes">注意</a>
</nav>
</header>
<main class="page">
<p class="eyebrow">LESSON / 40</p>
<h1>层叠层:把覆盖顺序写进结构</h1>
<p class="lead">
优先级解决「谁更具体」,层解决「谁先谁后」。用 @layer 声明顺序,覆盖不再靠堆权重。
</p>
<section class="panel" id="order">
<h2>1. 四层的顺序</h2>
<p>同名样式分别写在 base、components、utilities;最后声明的层赢。</p>
<div class="chips">
<span class="chip">base</span>
<span class="chip">components</span>
<span class="chip utility">utilities</span>
</div>
<p class="hint">chip 在 base 是灰色,components 改蓝,utilities 再改橙。</p>
</section>
<section class="panel" id="nested">
<h2>2. 嵌套层</h2>
<p>components 内部再分 card / button;button 在后,所以按钮样式赢。</p>
<div class="nested-demo">
<button class="btn" type="button">主按钮</button>
<div class="card">
<span class="card-tag">CARD</span>
<p>卡片样式来自 components.card。</p>
</div>
</div>
</section>
<section class="panel" id="revert">
<h2>3. revert-layer</h2>
<p>把组件层里的覆盖撤回,回落到 base 层的值。</p>
<div class="revert-demo">
<button class="btn ghost" type="button">组件层</button>
<button class="btn ghost revert" type="button">revert-layer</button>
</div>
<p class="hint">右侧按钮故意回退:背景与文字都取自 base 层。</p>
</section>
<section class="panel" id="notes">
<h2>4. 注意</h2>
<ul class="notes">
<li>未分层的样式排在所有层之后,权重最高。</li>
<li>层的顺序由第一次声明决定,后声明的同名层不会改顺序。</li>
<li>层不改变优先级;层内仍然比较选择器权重。</li>
<li><code>revert-layer</code> 只回退到上一个层,不是回退所有样式。</li>
</ul>
</section>
</main>
<footer class="site-footer">
<p>示例数据,仅用于教程演示。</p>
<p>CASCADE LAYERS · 2026</p>
</footer>
</div>
</body>
</html>