示例工程 52-browser-compat

52-browser-compat/app/index.html

html · 3.1 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="tokens.css" />
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="app">
      <header class="site-header">
        <span class="brand">COMPAT LAB</span>
        <nav aria-label="页面导航">
          <a href="#supports">特性检测</a>
          <a href="#fallback">回退</a>
          <a href="#matrix">矩阵</a>
          <a href="#notes">策略</a>
        </nav>
      </header>

      <main class="page">
        <p class="eyebrow">LESSON / 52</p>
        <h1>让新特性在不支持时也能用</h1>
        <p class="lead">
          @supports 做特性检测,旧值 → 新值做回退,var() 的第二个参数兜底;不要靠 UA 猜。
        </p>

        <section class="panel" id="supports">
          <h2>1. @supports 特性检测</h2>
          <p>默认 flex,支持 grid 时升级;不支持时布局仍然可用。</p>

          <div class="grid-demo">
            <div class="grid-item">一</div>
            <div class="grid-item">二</div>
            <div class="grid-item">三</div>
          </div>

          <p class="support-note supports-grid">当前浏览器支持 grid。</p>
        </section>

        <section class="panel" id="fallback">
          <h2>2. 回退顺序与变量兜底</h2>
          <p>旧值在前,新值在后;变量缺失时用第二个参数。</p>

          <div class="fallback-row">
            <div class="fallback-card color-fallback">hex → oklch</div>
            <div class="fallback-card var-fallback">var(--missing, 兜底)</div>
          </div>
        </section>

        <section class="panel" id="matrix">
          <h2>3. 支持矩阵</h2>
          <p>团队要明确「支持到哪一版」,而不是「尽量兼容」。</p>

          <table class="matrix">
            <thead>
              <tr><th scope="col">特性</th><th scope="col">Chrome</th><th scope="col">Firefox</th><th scope="col">Safari</th></tr>
            </thead>
            <tbody>
              <tr><td>Grid</td><td>✓</td><td>✓</td><td>✓</td></tr>
              <tr><td>:has()</td><td>✓</td><td>✓</td><td>✓</td></tr>
              <tr><td>oklch()</td><td>✓</td><td>✓</td><td>✓</td></tr>
              <tr><td>@scope</td><td>✓</td><td>✓</td><td>✓</td></tr>
            </tbody>
          </table>
        </section>

        <section class="panel" id="notes">
          <h2>4. 策略</h2>
          <ul class="notes">
            <li>先写可用的基础样式,再用 @supports 增强。</li>
            <li>旧值必须在旧浏览器能解析,不要写它也不认识的语法。</li>
            <li>@supports 检测的是语法支持,不检测 bug。</li>
            <li>支持矩阵写进项目文档,并配测试。</li>
          </ul>
        </section>
      </main>

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