CSS nesting

On this page 7

CSS nesting is a native CSS feature that allows one style rule to be written inside another, so that the inner selector is interpreted relative to the outer one. Instead of repeating .card in .card .title and .card:hover, an author writes the child rules inside the .card block and the browser expands them into full selectors at parse time.

The syntax is defined in the CSS Nesting Module from the W3C CSS Working Group and has shipped in all major browsers since 2023. It brings to plain CSS a pattern that developers previously needed a preprocessor such as Sass to use, while keeping the result fully dynamic in the browser.

How nested rules are resolved

A nested rule is resolved by combining its selector with the selector of the enclosing rule. The central tool is the nesting selector `&`, which stands for the parent selector inside a nested rule. Writing &:hover inside .button produces .button:hover, and writing .theme-dark & produces .theme-dark .button.

When a nested selector does not contain &, the browser adds it implicitly in front, followed by a descendant combinator. A rule .title { } inside .card therefore means .card .title. This relative selector behavior lets most nested rules omit the ampersand entirely.

Internally, the browser treats & as if the parent selector were wrapped in :is(). This detail has a direct effect on specificity and matching, which differs in subtle ways from the textual concatenation performed by preprocessors. The table below shows how common nested forms expand.

Nested form inside .cardEquivalent flat selectorMeaning
.title { }.card .titleDescendant of the card
& > img { }.card > imgDirect child image
&:hover { }.card:hoverCard in hover state
&.is-active { }.card.is-activeCard with an extra class
.grid & { }.grid .cardCard inside a grid
+ & { }.card + .cardCard following another card
How common nested forms expand

Nesting at-rules and declarations

Native nesting is not limited to selectors. Conditional group rules such as @media, @supports, @container and @layer can be placed inside a style rule, and their declarations apply to the parent selector. This keeps responsive variations next to the base styles instead of in a distant block at the end of the stylesheet.

Declarations that appear after nested rules were originally hoisted to the top of the rule, which surprised authors. The specification now wraps such trailing declarations in an implicit nested declarations rule, so source order is preserved in current engines. Older browser versions still hoist them, which is why placing declarations first remains good practice.

At-ruleTypical nested useEffect
@mediaChange padding at a breakpointApplies the parent selector under a media condition
@containerAdapt layout to the container widthApplies under a container query
@supportsUse a newer property with a fallbackApplies only if the feature is supported
@layerPlace component styles in a layerAssigns nested declarations to cascade layers
@scopeLimit styles to a subtreeCombines scoping with the parent selector
At-rules that can be nested in a style rule

Native CSS nesting compared with Sass nesting

Native CSS nesting and Sass nesting look similar but follow different rules. Sass concatenates selector text at compile time and emits flat CSS, while the browser resolves native nesting at runtime with :is() semantics. Teams migrating from Sass should check the differences below before removing their build step.

BehaviorNative CSS nestingSass nesting
Processing timeParsed by the browserCompiled before deployment
Suffix concatenation&__title is not supported&__title builds .block__title
Specificity of `&`Highest specificity in a selector listSpecificity of each listed selector
Selector list parentWrapped in :is()Expanded into every combination
Output sizeNo expansion, smaller filesCan multiply selectors greatly
Variables and mixinsNot includedIncluded in the language
Native nesting versus Sass nesting

The missing suffix concatenation affects BEM codebases most, because a pattern like &--large cannot generate .button--large. In native CSS that class must be written in full, for example &.button--large or as a separate top-level rule.

Specificity effects and common mistakes

The :is() wrapping means that a parent written as a selector list takes the specificity of its most specific member. If the parent is #main, .sidebar, every nested rule behaves as if it carried an ID, even when it matches inside .sidebar. This can make nested styles unexpectedly hard to override elsewhere in the cascade.

SymptomCauseFix
Rules too hard to overrideParent list contains an IDAvoid mixed lists or use :where()
Very long selectorsNesting four or more levels deepLimit depth to two or three levels
BEM suffix not workingNo string concatenation in native CSSWrite the full class name
Styles ignored in old browsersEngine predates nesting supportUse a PostCSS transform as a fallback
Declaration order changesOlder engines hoist trailing declarationsPlace declarations before nested rules
Frequent nesting problems and their fixes

Deep nesting also mirrors the HTML structure too closely, which couples styles to markup and makes refactoring fragile. A practical guideline is to nest states, variants and at-rules, while keeping structural descendant chains short.

When to use CSS nesting

CSS nesting works best for component-scoped styles, where a single root selector owns its states, child parts and responsive adjustments. It reduces repetition, groups related rules and makes stylesheets easier to scan.

  • Interaction states such as &:hover, &:focus-visible and &[aria-expanded="true"] read naturally inside the component rule.
  • Responsive overrides with nested @media or @container keep each breakpoint beside the property it changes.
  • Contextual theming with .theme-dark & keeps theme variations in the component block.
  • Legacy browser support still needs a build-time fallback when analytics show meaningful traffic from engines released before 2023.

The idea in brief

Styling a web page often means writing the same name many times, such as card, card title and card button. CSS nesting lets designers write those rules inside one block, the way an outline puts sub-points under a main point.

The browser reads the outline and works out the full names on its own. The main benefit is shorter, better organized stylesheets, with everything about one part of the page kept together in one place.

A practical scenario

A news publisher maintained a stylesheet of about 9,000 lines compiled from Sass, mainly to use nesting. The team wanted to remove the preprocessing step from its deployment pipeline and serve plain CSS directly.

Developers converted the article card, navigation and comment components to native CSS nesting. Hover and focus states moved into &:hover and &:focus-visible blocks, and breakpoint rules became nested @media blocks inside each component. Around 140 BEM suffix patterns such as &__meta had to be rewritten as full class names, because native nesting does not concatenate text.

After the migration the stylesheet shrank by roughly 18 percent, since nested rules were no longer expanded into long selector lists. The team removed the Sass compiler from the pipeline and kept a small PostCSS fallback only for a minority of older mobile browsers.