CSS Nesting: The & Selector, Nested At-Rules and Their Subtleties
Writing rules inside rules in plain CSS: implicit descendants, the & nesting selector, nested @media, @supports and @layer, how nesting desugars into :is() and what that means for specificity, the 2024 change to declaration order, and Sass differences.
kvi5hnuCode Sprout team member8 min readOn this page
Nesting lets you write a style rule inside another one. The inner selector is relative to the outer one, so related styles stay together and the parent selector isn't repeated. It's been in every browser since December 2023, so plain CSS now does what used to need a preprocessor such as Sass.
/* without nesting */
.book-card { padding: 1rem; border: 1px solid #ddd; }
.book-card > h3 { margin-top: 0; }
.book-card .price { font-weight: 700; }
.book-card:hover { border-color: #8a3b12; }
/* with nesting */
.book-card {
padding: 1rem;
border: 1px solid #ddd;
> h3 { margin-top: 0; }
.price { font-weight: 700; }
&:hover { border-color: #8a3b12; }
}
Both versions produce the same four rules.
Nested selectors and &
A nested rule's selector is joined to its parent's:
- Without
&, the nested selector means a descendant:.priceinside.book-cardmeans.book-card .price. - A leading combinator makes it a relative selector:
> h3means.book-card > h3, and+ .notemeans.book-card + .note. &, the nesting selector, stands for the parent selector exactly where you put it:
| Nested selector | Means |
|---|---|
&:hover | .book-card:hover (no space: the same element) |
&.featured | .book-card.featured |
&::before | .book-card::before |
& .price or .price | .book-card .price |
.sale-page & | .sale-page .book-card (the card in a context) |
& + & | .book-card + .book-card |
:not(&) | :not(.book-card) |
The space matters: &:hover is the card being hovered, while & :hover would be any hovered element inside the card.
Nested selectors can start with a type selector: h3 { ... } inside a rule is valid. The first browser versions (Chrome 112–119 and Safari 16.5–17.1) required an & or a symbol first, so older tutorials write & h3. Both forms work in current browsers.
.book-card {
font-family: system-ui, sans-serif;
padding: 0.75rem 1rem;
border: 1px solid #ddd;
border-radius: 8px;
max-width: 22rem;
margin-bottom: 0.75rem;
h3 { margin: 0 0 0.25rem; font-family: Georgia, serif; }
.price { font-weight: 700; color: #2f7d4f; }
&.featured { border: 2px solid #8a3b12; background: #fff8e6; }
&:hover { box-shadow: 0 2px 8px rgb(0 0 0 / 0.12); }
.sale-banner + & { border-style: dashed; }
}
.sale-banner { font: 700 0.85rem system-ui, sans-serif; color: #b3261e; margin: 0 0 0.25rem; }
Nested at-rules
Conditional at-rules can be nested inside a style rule. Declarations directly inside them apply to the parent selector:
.site-nav {
display: flex;
flex-direction: column;
@media (width >= 48rem) {
flex-direction: row;
}
@supports (position-anchor: --x) {
.submenu { position: absolute; position-anchor: --menu; }
}
@layer components {
gap: 1rem;
}
}
This keeps a component's responsive and fallback styles next to its base styles. @media, @supports, @layer, @container, @scope and @starting-style can be nested. At-rules that don't depend on an element, such as @font-face, @keyframes and @property, can't.
How nesting works: & is :is()
Nesting adds no new selecting power: the browser rewrites every nested rule into an ordinary one, and & becomes the parent selector wrapped in :is(). That explains two behaviors that surprise people.
Specificity follows :is()
:is() takes the specificity of its most specific argument (article 15), so a parent selector list passes its highest specificity to every nested rule:
input, #search button {
&:focus-visible { outline: 2px solid #8a3b12; }
}
/* becomes :is(input, #search button):focus-visible, specificity 1,1,1 */
Every focused input gets an ID's specificity. If that's a problem, split the parent list, or wrap the high-specificity part in :where().
Otherwise, nesting adds specificity like writing the full selector: .book-card { .price { } } is 0,2,0, the same as .book-card .price. Deep nesting therefore builds long, specific selectors. Keep nesting to two or three levels.
Context selectors can match more than expected
Because the parent becomes one :is() unit, its parts aren't fixed in order relative to the context you add:
.card .title {
footer & { color: #555; }
}
/* becomes footer :is(.card .title) */
That matches a title inside a card inside a footer, as intended, but also a title inside a footer inside a card: :is(.card .title) only requires the title to have a .card ancestor somewhere, and the footer can be anywhere above the title. With a preprocessor, the same code would produce the stricter footer .card .title.

The first two panels look like the same selector but highlight different elements. The third shows the rule silently doing nothing.
No string concatenation
In Sass, &__title inside .card produces .card__title. CSS nesting works with selectors, not text, so &__title is invalid and the rule is dropped. BEM-style suffixes have to be written out in full (article 116).
Pseudo-elements in the parent
& can't represent a pseudo-element. Inside .quote::before { ... }, a nested &:hover doesn't produce .quote::before:hover (which wouldn't be valid anyway) and matches nothing. Write .quote:hover::before as its own rule, or nest &:hover::before inside .quote.
Declaration order
Declarations and nested rules are applied in the order written. Declarations that come after a nested rule stay after it:
h1 {
font-size: 2rem;
@media (width >= 60rem) { font-size: 3rem; }
font-size: 2.2rem; /* comes last, so it wins even on wide screens */
}
That's a change from the first implementations, which moved all plain declarations to the top of the rule, before any nested rules. Since Chrome 130, Firefox 132 and Safari 18.2 (2024), trailing declarations are kept in place, represented in the CSSOM as CSSNestedDeclarations rules. Code written for the old behavior can break, most often with @starting-style: a base value written after a nested @starting-style block now overrides it (article 108).

In current browsers, the left panel's trailing declaration wins even on wide screens, because it comes after the nested rule.
The best habit works with both: write the element's own declarations first, then the nested rules. It also helps older browsers, which can still read the declarations before the first nested rule they don't understand.
Error handling
A nested rule with an invalid selector is dropped on its own, and the parent's other declarations and rules still apply:
.toolbar {
gap: 0.5rem;
&:hoover { color: red; } /* invalid: only this rule is dropped */
padding: 0.5rem; /* still applies */
}
A browser that doesn't support nesting at all, such as Chrome before 112, ignores every nested rule. Today that's only a concern for very old browsers.
& outside a rule
At the top level, outside any style rule, & matches the same as :scope, which is the root element. Inside @scope blocks, & refers to the scope root (article 22).
What's current
- CSS nesting is Baseline since December 2023, and widely available since June 2026.
- The relaxed syntax, allowing a nested selector to start with an element name, works in all current browsers.
- Trailing declarations keep their order (
CSSNestedDeclarations) in all current browsers since late 2024. Older articles describe the earlier hoisting behavior. - An earlier proposal for an
@nestrule was dropped, and it never shipped.
Common questions
Do I still need Sass for nesting? Not for nesting itself. Preprocessors still offer features such as loops, mixins and string concatenation, but nesting, variables (custom properties) and many functions are now in CSS.
Is .a { .b { } } the same as .a .b?
For matching and specificity, yes. A difference only appears when the parent is a selector list or a complex selector combined with a context, because & acts like :is().
Why doesn't &--large work?
CSS nesting can't glue text onto a selector. &--large isn't a valid selector, so the rule is dropped. Write .button--large as its own rule, or nest it as &.button--large if both classes are on the element.
How deep should I nest? Two or three levels at most. Every level adds to the specificity and length of the final selector, and deeply nested code is hard to override and to read.
Can I nest inside a style attribute?
No. The style attribute takes only declarations.
Summary
- Nested rules are relative to their parent: plain selectors are descendants, and leading combinators (
>,+,~) relate to the parent. &places the parent selector:&:hover,&.variant,.context &.@media,@supports,@layer,@container,@scopeand@starting-stylecan be nested, applying to the parent selector.&behaves like:is(parent), so a parent list gives nested rules its highest specificity, and context selectors match more loosely than in preprocessors.- Text concatenation like
&__titledoesn't work. - Declarations keep their written order. Write the element's own declarations before nested rules.
- An invalid nested rule is dropped without affecting the rest of its parent.
Practice questions
Rewrite with nesting:
.menu { display: flex; }
.menu > li { list-style: none; }
.menu a:hover { text-decoration: underline; }
.menu.is-open { display: block; }
@media (width < 40rem) { .menu { flex-direction: column; } }
Answer
.menu {
display: flex;
> li { list-style: none; }
a:hover { text-decoration: underline; }
&.is-open { display: block; }
@media (width < 40rem) { flex-direction: column; }
}
What does each of these nested rules select?
.alert {
.dark-theme & { } /* 1 */
}
.alert {
& .dark-theme { } /* 2 */
}
Answer
The first selects .alert elements inside a .dark-theme element (.dark-theme .alert). The second selects .dark-theme elements inside an .alert (.alert .dark-theme).
What's the specificity of the focus rule here, and why?
.field input, #newsletter input {
&:focus { border-color: #8a3b12; }
}
Answer
1,1,1. & becomes :is(.field input, #newsletter input), which has the specificity of its most specific argument, 1,0,1, and :focus adds 0,1,0. Every focused input matched by the rule gets that specificity, including those in .field.
Which font size applies on wide screens, and why?
h2 {
@media (width >= 60rem) { font-size: 2.5rem; }
font-size: 1.75rem;
}
Answer
1.75rem. In current browsers, declarations keep their written order, so the font-size after the media block comes later and wins. Put the base value first and the media query after it.
Previous: Cascade Layers: @layer, Layer Order and Taming Specificity
Next: @scope: Limiting Where Styles Apply, Scope Limits and Proximity

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.