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 .card | Equivalent flat selector | Meaning |
|---|---|---|
.title { } | .card .title | Descendant of the card |
& > img { } | .card > img | Direct child image |
&:hover { } | .card:hover | Card in hover state |
&.is-active { } | .card.is-active | Card with an extra class |
.grid & { } | .grid .card | Card inside a grid |
+ & { } | .card + .card | Card following another card |
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-rule | Typical nested use | Effect |
|---|---|---|
| @media | Change padding at a breakpoint | Applies the parent selector under a media condition |
| @container | Adapt layout to the container width | Applies under a container query |
| @supports | Use a newer property with a fallback | Applies only if the feature is supported |
| @layer | Place component styles in a layer | Assigns nested declarations to cascade layers |
| @scope | Limit styles to a subtree | Combines scoping with the parent selector |
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.
| Behavior | Native CSS nesting | Sass nesting |
|---|---|---|
| Processing time | Parsed by the browser | Compiled before deployment |
| Suffix concatenation | &__title is not supported | &__title builds .block__title |
| Specificity of `&` | Highest specificity in a selector list | Specificity of each listed selector |
| Selector list parent | Wrapped in :is() | Expanded into every combination |
| Output size | No expansion, smaller files | Can multiply selectors greatly |
| Variables and mixins | Not included | Included in the language |
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.
| Symptom | Cause | Fix |
|---|---|---|
| Rules too hard to override | Parent list contains an ID | Avoid mixed lists or use :where() |
| Very long selectors | Nesting four or more levels deep | Limit depth to two or three levels |
| BEM suffix not working | No string concatenation in native CSS | Write the full class name |
| Styles ignored in old browsers | Engine predates nesting support | Use a PostCSS transform as a fallback |
| Declaration order changes | Older engines hoist trailing declarations | Place declarations before nested rules |
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-visibleand&[aria-expanded="true"]read naturally inside the component rule. - Responsive overrides with nested
@mediaor@containerkeep 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.