Native CSS Nesting in Practice: Moving Beyond Preprocessors

前端工程(Updated Jun 19, 2026)

Native CSS Nesting Syntax

CSS Nesting allows child selectors to be directly nested inside parent selectors, enabling scoped style organization without preprocessors.

.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);
  }
}

This compiles to the equivalent of:

.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); }

Key Differences from Sass/Less Nesting

Feature Native CSS Nesting Sass Less
Runtime Browser native Compile time Compile time
& usage Must be explicit Can be omitted Can be omitted
Nesting rules Must start with & or selector Any nesting Any nesting
:is() wrapping Automatic implicit wrapping None None
Specificity impact :is() takes highest No impact No impact
@media nesting
@layer nesting
Dependencies None Ruby/Dart Node.js

The & Nesting Selector Deep Dive

Basic Usage

.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 Pattern Replacement

.block {
  background: #f5f5f5;

  &__element {
    padding: 1rem;

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

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

& Position in Compound Selectors

.card {
  /* & at the front (default) */
  & .title { font-size: 1.25rem; }

  /* & at the end — reverse reference */
  .wrapper & { margin: 0 auto; }

  /* & in the middle */
  .sidebar & .icon { width: 16px; }
}

:is() Implicit Wrapping and Specificity

Native CSS nesting internally uses :is() wrapping, which affects specificity calculation.

The Specificity Trap

/* Native nesting */
#main .content {
  .item { color: red; }
}
/* Equivalent to: #main .content .item → specificity (1,1,1) ✅ */

div .content {
  .item { color: blue; }
}
/* Equivalent to: :is(div) .content .item → specificity (0,1,1) ⚠️ */

/* Compare with Sass compilation */
div .content .item { color: blue; }
/* Specificity (0,2,1) — different from native nesting! */

Specificity Rules

:is(#main, .content) {
  .item { color: red; }
}
/* :is() takes the highest specificity among arguments → (1,0,0) */
/* Final: #main .item or .content .item → specificity (1,1,0) */

Nesting Within @layer Cascade Layers

@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; }
}

Cascade Layer Priority

Unlayered styles > utilities > components > base
(Later-declared layers have higher priority; unlayered styles have the highest)

Media Query Nesting

.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;
    }
  }
}

Practice: Component Style Architecture

.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;
    }
  }
}

Practice: Theme System

: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; }
  }
}

Migration Strategy: From Preprocessors to Native CSS

Gradual Migration

/* Phase 1: Keep preprocessor, mix with native nesting */
.container {
  max-width: 1200px;

  // Sass single-line comments don't work in native CSS, replace with /* */
  .header {
    /* Comments must be block comments */
    display: flex;
  }
}

/* Phase 2: Replace variable declarations with CSS custom properties */
:root {
  --spacing-sm: 0.5rem;
  --spacing-md: 1rem;
  --spacing-lg: 1.5rem;
}

/* Phase 3: Remove mixins, use @layer + native nesting */
@layer reset {
  * { margin: 0; box-sizing: border-box; }
}

Migration Considerations

Preprocessor Feature Native CSS Alternative Status
Nesting CSS Nesting ✅ Supported
Variables CSS Custom Properties ✅ Supported
@mixin / @include No direct replacement ❌ Needs refactoring
@extend No direct replacement ❌ Needs refactoring
@function No direct replacement ❌ Needs refactoring
Math operations calc() ✅ Supported
Color functions color-mix() / oklch() ✅ Partial
@import @import / @use ⚠️ Needs build tool

Browser Compatibility

Browser Support Version Notes
Chrome 120+ Full support
Firefox 117+ Full support
Safari 17.2+ Full support
Edge 120+ Full support
npm install postcss-nesting
import postcss from 'postcss';
import postcssNesting from 'postcss-nesting';

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

Best Practices Summary

  1. Prefer native nesting: Use directly in new projects to reduce build dependencies
  2. Mind specificity differences: Native nesting's :is() wrapping can produce different specificity than Sass
  3. Use & explicitly: Pseudo-classes and pseudo-elements must reference the parent selector with &
  4. Limit nesting depth to 3-4 levels: Deep nesting increases specificity and makes overrides harder
  5. Combine with @layer: Use cascade layers for global priority, nesting for component-internal structure

Try these browser-local tools — no sign-up required →

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