Skip to content
FEE
Frontend Engineering Essentials
Senior Level
Hardened· 2026-07

[FEE-11202] CSS Nesting ​

INFO

Write CSS component rules as nested, self-contained blocks, directly in the browser, with no preprocessor step required.

Baseline

This feature is Baseline Widely Available. Supported in Chrome 120+, Firefox 117+, Safari 17.2+ (Chrome 112+ also works if you write & explicitly on every nested rule; see Browser Support below).

Context ​

CSS has always required you to repeat the parent selector on every rule that belongs to a component. A simple card with hover and an active variant produces four separate rules, each of which restates .card:

css
.card { background: #fff; border-radius: 8px; padding: 1rem; }
.card:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
.card .title { font-size: 1.25rem; font-weight: 700; }
.card--active .title { color: #0070f3; }

This repetition creates a maintenance hazard. Renaming .card to .article-card requires touching every rule, and moving the component to a different file means re-reading each rule to see which selector it belongs to. In a large codebase this repetition causes selector drift: some rules get updated and others are missed, so components accumulate dead rules and untested combinations over time.

Preprocessors (Sass in 2006, Less in 2009, Stylus in 2010) solved this problem with nesting syntax. For fifteen years, nesting existed only as a build-time transformation performed by the toolchain. Native CSS itself required manual selector repetition regardless of what your preprocessor did.

CSS Nesting, standardized in CSS Nesting Module Level 1, lands this capability directly in the browser with no build step required. The same syntax that Sass developers have used for a decade is now a first-class browser feature.

Visual ​

The browser converts every nested rule into an equivalent flat selector before it computes specificity. This conversion is called de-sugaring, and it is the mechanic the rest of this article builds on:

Written with nestingDe-sugars toResulting specificity
.card { .title { color: navy; } }.card .title { color: navy; }(0,2,0)
.card { &:hover { color: navy; } }.card:hover { color: navy; }(0,2,0) (one class + one pseudo-class)
.card, #featured { .title { color: navy; } }:is(.card, #featured) .title { color: navy; }(1,1,0) (inherits the ID from :is())

The third row is the one exception worth remembering: a comma-separated parent selector list de-sugars through :is(), which takes the specificity of its most specific argument. Design Thinking below explains why.

Example ​

Complete .button component with states, variants, and child element ​

The example below demonstrates the full power of CSS nesting. The first block uses native CSS nesting; the second block shows the equivalent flat CSS it de-sugars to. Both produce identical output; only the authored form differs.

css
/* ── Native CSS nesting ──────────────────────────────────────── */
.button {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  border: 2px solid transparent;
  border-radius: 4px;
  font-weight: 600;
  cursor: pointer;
  background: #0070f3;
  color: white;

  /* Pseudo-class states — & required (non-identifier start) */
  &:hover { background: #0051a8; }
  &:focus-visible { outline: 3px solid #0070f3; outline-offset: 2px; }

  /* Attribute selector — & required */
  &[disabled] { opacity: 0.5; cursor: not-allowed; pointer-events: none; }

  /* Modifier variant — & required (compound selector begins with &) */
  &.button--secondary {
    background: transparent;
    border-color: #0070f3;
    color: #0070f3;

    /* State nested inside variant — level 2 */
    &:hover { background: #e6f0ff; }
  }

  /* Child element — implicit nesting, & optional */
  .button__icon { width: 1em; height: 1em; }
}
css
/* ── Equivalent flat CSS — same specificity, same output ──────── */
.button {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  border: 2px solid transparent;
  border-radius: 4px;
  font-weight: 600;
  cursor: pointer;
  background: #0070f3;
  color: white;
}
.button:hover { background: #0051a8; }
.button:focus-visible { outline: 3px solid #0070f3; outline-offset: 2px; }
.button[disabled] { opacity: 0.5; cursor: not-allowed; pointer-events: none; }
.button.button--secondary { background: transparent; border-color: #0070f3; color: #0070f3; }
.button.button--secondary:hover { background: #e6f0ff; }
.button .button__icon { width: 1em; height: 1em; }

Nesting with @media queries ​

Responsive overrides co-located with the base rule they modify are easier to audit than overrides scattered at the bottom of a file.

css
.card {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
  padding: 1rem;

  /* Responsive override nested inside the component rule */
  @media (min-width: 768px) {
    grid-template-columns: auto 1fr;
    padding: 1.5rem;
  }

  .card__header {
    font-size: 1.125rem;
    font-weight: 700;

    @media (min-width: 768px) {
      font-size: 1.25rem;
    }
  }
}

Nesting with @container queries ​

Container queries nest identically to media queries. The container query reads the width of the nearest container-type ancestor, not the viewport.

css
.sidebar {
  container-type: inline-size;
}

.sidebar .widget {
  display: block;
  padding: 0.75rem;

  @container (min-width: 300px) {
    display: flex;
    gap: 1rem;
    padding: 1rem;
  }

  .widget__label {
    font-size: 0.875rem;
    color: #555;
  }
}

Nesting inside @layer ​

Nesting inside a cascade layer scopes the component rules without changing how nesting itself works.

css
@layer components {
  .chip {
    display: inline-flex;
    align-items: center;
    padding: 0.25rem 0.75rem;
    border-radius: 9999px;
    font-size: 0.875rem;
    background: #f0f0f0;
    color: #333;

    &:hover { background: #e0e0e0; }

    &.chip--active {
      background: #0070f3;
      color: white;
      &:hover { background: #0051a8; }
    }
  }
}

Rules inside @layer components have lower priority than unlayered rules, regardless of specificity. The nesting structure is preserved normally inside the layer block.

Migrating from Sass nesting ​

Sass nesting and native CSS nesting are syntactically nearly identical for the most common patterns. The main differences:

PatternSassNative CSS
Pseudo-class&:hover&:hover (identical)
Child element.button__icon or &__icon.button__icon (no &suffix concatenation)
Modifier variant&--secondary&.button--secondary (no string concatenation)
Type selectorspanspan or & span

The critical difference: native CSS nesting does NOT support the & string concatenation pattern used for BEM modifiers in Sass. &--secondary in Sass produces .button--secondary. In native CSS, &--secondary is invalid (a selector cannot begin with --). Use the full class name: &.button--secondary.

Best Practices ​

  • SHOULD use native CSS nesting for all new code targeting modern browsers. Browser support is Baseline Widely Available as of Chrome 120 / Firefox 117 / Safari 17.2.
  • MUST use & when the nested selector begins with a pseudo-class (&:hover), pseudo-element (&::after), or attribute selector (&[disabled]). Omitting & here is not a syntax error but silently changes the meaning: the browser inserts a descendant combinator, so .card { :hover {} } matches hovered descendants (.card :hover), not the card itself (.card:hover). A leading combinator (> .child, + .sibling) is valid without an explicit &, since & is implied in that position.
  • SHOULD NOT nest more than 3 levels deep. Deep nesting produces high-specificity selectors and makes the cascade harder to override. A component with a modifier variant whose child has a state is already at 3 levels. Consider flattening anything beyond that.
  • SHOULD NOT use @nest. The @nest at-rule appeared in earlier CSS Nesting drafts (2021-2022) but was removed from the specification before Chrome shipped stable nesting: Chrome 112 (April 2023) shipped the &-based syntax without @nest. Modern browsers do not support it; use & instead.
  • SHOULD verify your PostCSS pipeline when working with a legacy build system. postcss-nesting v10.x and earlier implement the deprecated @nest draft and may mishandle native nesting syntax. Modern (non-@nest) syntax has been the default since v11.1.0; upgrade to v11.3.0 or later for a runtime warning when @nest is used.
  • MAY combine nesting with @layer to scope component rules within a layer. Nesting inside a @layer block is valid and composes naturally with the cascade layer system.
  • MAY nest @media and @container queries inside a rule block. This keeps responsive overrides co-located with the base declaration they modify.

Design Thinking ​

Why CSS had no nesting for 25+ years ​

The CSS parser has a structural ambiguity problem. Consider this hypothetical syntax without the & sigil:

css
div {
  span {
    color: red;
  }
}

The parser cannot determine whether span { color: red; } is a nested rule or a malformed declaration. CSS declarations are property: value pairs. The sequence span { ... } does not match that pattern, but parsers are designed to be error-tolerant and skip unknown constructs rather than fail. A naive parser would silently discard the nested block as an invalid declaration.

The & nesting selector (formally: the "nest selector") solves this by providing an unambiguous signal. When the parser sees &, it knows it is reading a nested rule, not a declaration. The selector that follows is relative to the outer rule.

css
div {
  &:hover { color: red; }   /* unambiguously a nested rule */
}

This is why the CSS Working Group required & in the initial nesting design (2023). Browser vendors then shipped "relaxed parsing" in Chrome 120 and Safari 17.2, an extension that allows nested selectors to begin with a class, ID, attribute, or type selector without &:

css
.card {
  .title { font-weight: 700; }   /* implicit nesting — valid since Chrome 120 */
}

The parser resolves the ambiguity by attempting to parse the construct as a declaration first; if that fails to produce a valid property: value pair, it restarts and reparses the same tokens as a nested style rule instead. This requires effectively unbounded lookahead, since the parser cannot know which interpretation is correct until it has consumed the whole construct.

How Sass popularized the pattern ​

Sass (Syntactically Awesome Style Sheets) introduced nesting in 2006 as one of its core selling points. The pattern spread to Less (2009), Stylus (2010), and eventually the CSS-in-JS ecosystem (styled-components, Emotion). By 2015, nesting was the dominant way to write component CSS, though it always compiled away at build time.

When the CSS Working Group began standardizing nesting in earnest (the first draft was proposed in 2013), Sass syntax had already trained an entire generation of frontend developers. The final native syntax closely mirrors what Sass users expect, which lowered the adoption curve significantly.

Specificity is unchanged: nesting is syntactic sugar ​

The most important thing to understand about CSS nesting is that it does not change specificity. A nested rule is de-sugared by the browser into the equivalent flat selector before specificity is computed.

css
/* Written with nesting */
.card {
  .title { color: navy; }
}

/* Equivalent flat selector after de-sugaring */
.card .title { color: navy; }

Both forms have specificity (0,2,0), from two class selectors. Nesting is purely organizational. It does not give nested rules any priority advantage over flat rules that target the same element.

There is one specificity subtlety when the parent selector is a selector list:

css
.card, #featured {
  .title { color: navy; }
}

The browser de-sugars this to :is(.card, #featured) .title. The :is() pseudo-class takes the specificity of its most specific argument: in this case #featured, an ID with specificity (1,0,0). The .title rule therefore has specificity (1,1,0), higher than you might expect from a class-based component selector. Be cautious with mixed ID and class selector lists as parents.

@nest: a false start ​

The original CSS Nesting syntax included an @nest at-rule in earlier drafts (2021-2022). It was removed in the relative-selector "Option 3" revision of late 2022 / January 2023, before Chrome 112 (April 2023) shipped the &-based syntax without @nest:

css
/* @nest — do not use */
.card {
  @nest &:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
}

@nest is not supported in any current browser; code using it is silently ignored. If you encounter @nest in a codebase, replace it with &.

Native nesting and the CSSNestedDeclarations interface ​

In 2024, the CSSOM was extended with the CSSNestedDeclarations interface to support rule blocks that contain bare declarations inside nested rules without creating an unintended "empty" rule. This resolved an edge case where declarations placed after a nested rule in the same block were treated as belonging to a separate rule in the cascade. The fix landed in Chrome 130, Firefox 132, and Safari 18.2.

Browser Support ​

BrowserFirst versionNotes
Chrome112Initial support with & required; relaxed parsing (implicit nesting) in Chrome 120
Firefox117Relaxed parsing included from initial stable release
Safari17.2Relaxed parsing included from initial stable release
Edge112Chromium-based; relaxed parsing in Edge 120

Feature detection in CSS:

css
@supports selector(&) {
  /* CSS nesting is supported */
  .card {
    &:hover { box-shadow: 0 2px 8px rgba(0,0,0,0.12); }
  }
}

Feature detection in JavaScript:

js
const supportsNesting = CSS.supports('selector(&)');

As of 2026, CSS nesting is Baseline Widely Available. The relaxed parsing rules (implicit nesting without & for class/ID/attribute/type selectors) are supported in Chrome 120+, Firefox 117+, and Safari 17.2+. For projects that must support Chrome 112–119, use & explicitly on all nested rules. The result is identical; it is just more verbose.

Build tool compatibility ​

If your project uses a PostCSS pipeline, verify your postcss-nesting version:

VersionStatusNotes
v10.x and earlierunsafe / @nest draftImplements the deprecated @nest spec; may mishandle native nesting syntax
v11.1.0 (2023-01-31) and latermodern syntaxAdopts the modern, non-@nest spec
v11.3.0 (2023-06-14) and latermodern syntax + @nest warningEmits a runtime warning when @nest is used, silenceable via the silenceAtNestWarning option
  • Current latest: v14.0.1 (2026-07-23)
  • postcss-nesting is itself the csstools-maintained plugin. The variant tracking not-yet-stable syntax proposals is @csstools/postcss-nesting-experimental, not a separate "nesting-rules" package.

If your build uses Lightning CSS instead of PostCSS, native nesting needs no separate plugin: Lightning CSS parses and downlevels nested rules itself. This is the default CSS transform for Parcel, and Tailwind CSS v4 builds on Lightning CSS for the same reason, so projects on either toolchain do not need postcss-nesting at all.

If you are targeting browsers that support native nesting and your build pipeline does not transform nesting, no plugin is needed at all. Check your browserslist configuration before adding a nesting transform.

  • FEE-11200 CSS @scope: @scope pairs naturally with nesting. Nest your component styles inside the rule block, then wrap the component in @scope to prevent selector leakage from the outside.
  • FEE-11203 CSS @layer (upcoming): @layer and nesting compose cleanly. Placing a nested component block inside a @layer gives you both the authoring clarity of nesting and the cascade priority control of layers.

References ​