示例工程 41-has-is-where

41-has-is-where/app/index.html

html · 3.4 KB

运行::has / :is / :where

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

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>:has / :is / :where</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">SELECTOR LAB</span>
        <nav aria-label="页面导航">
          <a href="#has">:has</a>
          <a href="#is">:is</a>
          <a href="#where">:where</a>
          <a href="#notes">注意</a>
        </nav>
      </header>

      <main class="page">
        <p class="eyebrow">LESSON / 41</p>
        <h1>用关系选择器减少类名</h1>
        <p class="lead">
          :has 看「里面有什么」,:is 合并选择器列表,:where 把权重归零。三者配合,类名和覆盖都更轻。
        </p>

        <section class="panel" id="has">
          <h2>1. :has 看内容</h2>
          <p>卡片里有徽标时改变边框;邮箱输入无效时整块变红。</p>

          <div class="cards">
            <article class="card">
              <span class="badge">NEW</span>
              <h3>带徽标的卡片</h3>
              <p>父级用 :has(.badge) 命中自己。</p>
            </article>
            <article class="card">
              <h3>普通卡片</h3>
              <p>没有徽标,保持灰色边框。</p>
            </article>
          </div>

          <div class="field">
            <label for="email">邮箱</label>
            <input id="email" type="email" value="not-an-email" required />
            <p class="hint">输入无效时,:has(input:invalid) 让提示变红。</p>
          </div>
        </section>

        <section class="panel" id="is">
          <h2>2. :is 合并列表</h2>
          <p>标题、按钮、链接共用一组规则,不用重复写选择器。</p>

          <div class="is-demo">
            <h3 class="demo-title">三级标题</h3>
            <h4 class="demo-subtitle">四级标题</h4>
            <div class="actions">
              <button class="action primary" type="button">保存</button>
              <button class="action" type="button" disabled>禁用</button>
              <a class="action link" href="#is">链接样式</a>
            </div>
          </div>
        </section>

        <section class="panel" id="where">
          <h2>3. :where 权重归零</h2>
          <p>基础样式用 :where 包住,后面任何单类都能覆盖它。</p>

          <div class="where-demo">
            <span class="tag">普通 tag</span>
            <span class="tag accent">accent tag</span>
          </div>
          <p class="hint">即使 :where(.tag) 写在文件最后,也压不过前面的 .tag。</p>
        </section>

        <section class="panel" id="notes">
          <h2>4. 注意</h2>
          <ul class="notes">
            <li><code>:has()</code> 不能嵌套在另一个 <code>:has()</code> 里。</li>
            <li><code>:is()</code> 的权重取列表里最高的那个。</li>
            <li><code>:where()</code> 的权重恒为 0,适合重置与基础层。</li>
            <li>关系选择器不能替代语义化 HTML;它们只是样式钩子。</li>
          </ul>
        </section>
      </main>

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