CSS 原生嵌套实战:告别预处理器的样式架构演进

前端工程(更新于 2026年6月19日)

CSS 原生嵌套语法

CSS Nesting 允许将子选择器直接嵌套在父选择器内部,无需预处理器即可实现样式的作用域组织。

.card {
  padding: 1rem;
  border-radius: 8px;
  background: #fff;

  .title {
    font-size: 1.25rem;
    font-weight: 600;
  }

  .body {
    margin-top: 0.5rem;
    color: #666;
  }

  &:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }
}

编译结果等价于:

.card { padding: 1rem; border-radius: 8px; background: #fff; }
.card .title { font-size: 1.25rem; font-weight: 600; }
.card .body { margin-top: 0.5rem; color: #666; }
.card:hover { box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); }

与 Sass/Less 嵌套的关键差异

特性 CSS 原生嵌套 Sass Less
运行时 浏览器原生 编译时 编译时
& 替换 必须显式使用 可省略 可省略
嵌套规则 必须以 & 或选择器开头 任意嵌套 任意嵌套
:is() 包装 自动隐式包装
特异性影响 :is() 取最高 无影响 无影响
@media 嵌套
@layer 嵌套
依赖 Ruby/Dart Node.js

& 嵌套选择器详解

基础用法

.btn {
  color: blue;

  &:hover { color: darkblue; }
  &:active { color: navy; }
  &:focus-visible { outline: 2px solid blue; }
  &.primary { background: blue; color: white; }
  &.disabled { opacity: 0.5; pointer-events: none; }
}

BEM 模式替代

.block {
  background: #f5f5f5;

  &__element {
    padding: 1rem;

    &--modifier {
      padding: 2rem;
      background: #e0e0e0;
    }
  }

  &--featured {
    border: 2px solid gold;
  }
}

& 在复合选择器中的位置

.card {
  /* & 在前(默认) */
  & .title { font-size: 1.25rem; }

  /* & 在后 — 逆向引用 */
  .wrapper & { margin: 0 auto; }

  /* & 在中间 */
  .sidebar & .icon { width: 16px; }
}

:is() 隐式包装与特异性

CSS 原生嵌套在内部使用 :is() 包装,这会影响特异性计算。

特异性陷阱

/* 原生嵌套 */
#main .content {
  .item { color: red; }
}
/* 等价于:#main .content .item → 特异性 (1,1,1) ✅ */

div .content {
  .item { color: blue; }
}
/* 等价于::is(div) .content .item → 特异性 (0,1,1) ⚠️ */

/* 对比 Sass 编译结果 */
div .content .item { color: blue; }
/* 特异性 (0,2,1) — 与原生嵌套不同! */

特异性规则

:is(#main, .content) {
  .item { color: red; }
}
/* :is() 取参数中最高特异性 → (1,0,0) */
/* 最终:#main .item 或 .content .item → 特异性 (1,1,0) */

@layer 级联层中的嵌套

@layer base, components, utilities;

@layer base {
  html {
    font-size: 16px;
    line-height: 1.6;

    body {
      margin: 0;
      font-family: system-ui;
    }
  }
}

@layer components {
  .card {
    padding: 1rem;
    border: 1px solid #ddd;

    .title {
      font-size: 1.25rem;
    }

    &:hover {
      border-color: #999;
    }

    @media (min-width: 768px) {
      padding: 1.5rem;

      .title { font-size: 1.5rem; }
    }
  }
}

@layer utilities {
  .text-center { text-align: center; }
}

级联层优先级

无层样式 > utilities > components > base
(后声明的层优先级更高,无层样式优先级最高)

媒体查询嵌套

.container {
  width: 100%;
  padding: 1rem;

  @media (min-width: 768px) {
    width: 720px;
    padding: 2rem;

    .sidebar {
      display: block;
      width: 240px;
    }
  }

  @media (min-width: 1024px) {
    width: 960px;

    .sidebar {
      width: 300px;
    }
  }
}

实战:组件样式架构

.form-field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;

  label {
    font-size: 0.875rem;
    font-weight: 500;
    color: #374151;
  }

  input, textarea, select {
    padding: 0.5rem 0.75rem;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    font-size: 1rem;

    &:focus {
      outline: none;
      border-color: #3b82f6;
      box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
    }

    &:disabled {
      background: #f3f4f6;
      cursor: not-allowed;
    }
  }

  .error-msg {
    color: #ef4444;
    font-size: 0.75rem;

    &:empty { display: none; }
  }

  &.has-error {
    input, textarea, select {
      border-color: #ef4444;

      &:focus {
        box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.15);
      }
    }
  }

  @media (prefers-color-scheme: dark) {
    label { color: #d1d5db; }

    input, textarea, select {
      background: #1f2937;
      border-color: #4b5563;
      color: #f9fafb;
    }
  }
}

实战:主题系统

:root {
  --color-primary: #3b82f6;
  --color-surface: #ffffff;
  --color-text: #1f2937;
  --radius: 8px;
}

[data-theme="dark"] {
  --color-primary: #60a5fa;
  --color-surface: #1f2937;
  --color-text: #f9fafb;
}

.button {
  padding: 0.5rem 1rem;
  background: var(--color-primary);
  color: white;
  border-radius: var(--radius);
  border: none;
  cursor: pointer;

  &:hover { filter: brightness(1.1); }
  &:active { filter: brightness(0.95); }

  &--sm { padding: 0.25rem 0.5rem; font-size: 0.875rem; }
  &--lg { padding: 0.75rem 1.5rem; font-size: 1.125rem; }

  &--outline {
    background: transparent;
    border: 2px solid var(--color-primary);
    color: var(--color-primary);

    &:hover { background: var(--color-primary); color: white; }
  }
}

迁移策略:从预处理器到原生 CSS

逐步迁移

/* 阶段1:保留预处理器,混合使用原生嵌套 */
.container {
  max-width: 1200px;

  // Sass 单行注释在原生 CSS 中无效,需替换为 /* */
  .header {
    /* 注释需改为块注释 */
    display: flex;
  }
}

/* 阶段2:移除变量声明,改用 CSS 自定义属性 */
:root {
  --spacing-sm: 0.5rem;
  --spacing-md: 1rem;
  --spacing-lg: 1.5rem;
}

/* 阶段3:移除 mixin,改用 @layer + 原生嵌套 */
@layer reset {
  * { margin: 0; box-sizing: border-box; }
}

需要注意的迁移点

预处理器特性 原生 CSS 替代 状态
嵌套 CSS Nesting ✅ 已支持
变量 CSS Custom Properties ✅ 已支持
@mixin / @include 无直接替代 ❌ 需重构
@extend 无直接替代 ❌ 需重构
@function 无直接替代 ❌ 需重构
数学运算 calc() ✅ 已支持
颜色函数 color-mix() / oklch() ✅ 部分支持
@import @import / @use ⚠️ 需构建工具

浏览器兼容性

浏览器 支持版本 备注
Chrome 120+ 完整支持
Firefox 117+ 完整支持
Safari 17.2+ 完整支持
Edge 120+ 完整支持
npm install postcss-nesting
import postcss from 'postcss';
import postcssNesting from 'postcss-nesting';

const result = await postcss([postcssNesting]).process(css, { from: 'style.css' });

最佳实践总结

  1. 优先使用原生嵌套:新项目直接使用,减少构建依赖
  2. 注意特异性差异:原生嵌套的 :is() 包装可能导致与 Sass 不同的特异性
  3. & 显式使用:伪类和伪元素必须用 & 引用父选择器
  4. 嵌套深度控制在 3-4 层:过深嵌套增加特异性且难以覆盖
  5. 结合 @layer:用级联层管理全局优先级,嵌套管理组件内部结构

本站提供浏览器本地工具,免注册即可试用 →

#CSS嵌套#原生CSS#预处理替代#选择器#样式架构