Native CSS Nesting: The Complete Guide and How It Differs From Sass
Write nested CSS without a preprocessor, and avoid the specificity and BEM traps that catch Sass users
Nesting Is No Longer a Preprocessor Feature
Nesting was the feature that made a generation of developers install Sass. Writing hover states, child elements and media queries inside the component they belong to keeps related styles together and saves a lot of repetition.
Native CSS nesting lets you write style rules inside other style rules directly in a .css file, with no build step. The browser understands it. The syntax is defined in the W3C CSS Nesting specification, and it has been Baseline since 2023: Chrome and Edge 112, Safari 16.5 and Firefox 117 support it, and the more relaxed syntax described below arrived in Chrome and Edge 120 and Safari 17.2.
Native nesting looks almost identical to Sass, which is exactly why it causes bugs. It handles selector lists, string concatenation and specificity differently. This guide covers the syntax, then focuses on those differences so you can migrate safely.
If you are new to preprocessors, our Sass tutorial explains the original feature, and the CSS selectors tutorial covers the selector syntax used throughout.
The Basics: Nested Rules and the & Selector
A nested rule is relative to its parent. With no &, a nested selector is treated as a descendant of the parent:
.card {
padding: 1rem;
h2 { margin-top: 0; } /* .card h2 */
> img { width: 100%; } /* .card > img */
+ .card { margin-top: 1rem; } /* .card + .card */
}The & nesting selector represents the parent selector explicitly. You need it whenever the nested selector should attach to the same element rather than a descendant:
.button {
background: #7c3aed;
&:hover { background: #6d28d9; } /* .button:hover */
&.is-loading { opacity: 0.6; } /* .button.is-loading */
&[disabled] { cursor: not-allowed; } /* .button[disabled] */
}The missing-& bug
Leaving out & before a pseudo-class is the most common nesting mistake. :hover on its own is a descendant selector:
.link {
:hover { color: red; } /* .link :hover, any hovered descendant! */
&:hover { color: red; } /* .link:hover, what you meant */
}& can go anywhere
Unlike most people's Sass habits suggest, & does not have to come first. Put it at the end to style an element based on its context:
.card {
.theme-dark & { background: #111827; } /* .theme-dark .card */
:not(&) { } /* any element that is not .card */
& + & { border-top: 1px solid #e5e7eb; } /* .card + .card */
}The relaxed syntax
The first browser implementations required a nested selector to start with a symbol, so you had to write & h2 instead of h2. That restriction was removed in Chrome and Edge 120 and Safari 17.2, and every browser that supports nesting today accepts bare element selectors. If you must support older versions, prefix element selectors with & .
Nesting Media Queries, Container Queries and Layers
Conditional at-rules can be nested inside a style rule. Declarations inside them apply to the parent selector, so you do not repeat it:
.sidebar {
display: none;
@media (min-width: 64rem) {
display: block;
width: 18rem;
}
@supports (position: sticky) {
position: sticky;
top: 1rem;
}
}This works for @media, @supports, @container, @layer and @scope. Container queries in particular read well when nested inside the component they adapt:
.product-card {
display: grid;
gap: 1rem;
@container (min-width: 32rem) {
grid-template-columns: 12rem 1fr;
}
}Our deep dive on CSS container queries covers the container setup this relies on. A nested @layer block places those declarations into that cascade layer, which is handy for theming utilities inside a component file.
Three Ways Native Nesting Differs From Sass
Sass rewrites nested rules as text before the browser sees them. Native nesting does no text rewriting. The browser treats & as a reference to the parent selector, roughly as if it were wrapped in :is(). That design causes three visible differences.
1. No string concatenation, so BEM suffixes break
In Sass, &__title glues text onto the parent to produce .card__title. Native CSS has no concept of gluing text onto a selector:
.card {
&__title { font-weight: 700; } /* NOT .card__title: invalid, rule dropped */
&--featured { border-color: gold; } /* NOT .card--featured: also dropped */
}Write the full class names instead. Nesting still helps with states and children:
.card__title {
font-weight: 700;
.card--featured & { color: #92400e; } /* .card--featured .card__title */
}If your codebase relies heavily on &__ and &-- patterns, keep Sass for those files or migrate to full class names deliberately. See our BEM tutorial for naming that works well with native nesting.
2. Selector lists become :is()
Sass expands .a, .b { .c {} } into .a .c, .b .c. Native nesting behaves like :is(.a, .b) .c. Usually the result matches the same elements, but specificity changes, as the next section shows.
3. Context selectors behave differently
With a parent like .foo .bar, the Sass output for .theme & {} is .theme .foo .bar, which requires .theme to be outside .foo. Native nesting produces .theme :is(.foo .bar), which also matches when .theme sits between .foo and .bar. Most of the time nobody notices. When it matters, it is a confusing bug.
The :is() Specificity Trap
:is() takes the specificity of its most specific argument. So when a nested rule's parent is a selector list containing an ID, every selector in the list is treated as if it had that ID's weight.
.card, #featured {
.title { color: royalblue; }
}
.card .title { color: tomato; }You might expect the later .card .title rule to win for ordinary cards. It doesn't. The nested rule behaves like :is(.card, #featured) .title, with a specificity of (1,1,0). .card .title is only (0,2,0). Every .title inside every .card turns royal blue, even cards that are nowhere near #featured.
Sass would have output .card .title, #featured .title, two separate selectors with separate specificity, so the same code behaved differently there.
How to avoid it
- Don't mix IDs (or very specific selectors) with classes in a parent selector list. Split them into separate rules.
- Wrap the parent in
:where()to remove its specificity completely::where(.card, #featured) { .title { } }. - Prefer classes for components, which keeps specificity flat.
The !important and specificity lesson explains how to calculate these weights.
Declarations After Nested Rules
What happens when you write declarations after a nested rule?
article {
color: green;
& { color: blue; }
color: red;
}Under the current specification, declarations keep their source order. The rule above is equivalent to article { color: green }, then :is(article) { color: blue }, then article { color: red }, so the text is red. Browsers implement this with a "nested declarations rule" (CSSNestedDeclarations in the CSSOM).
Early implementations behaved differently: they hoisted all declarations to the top of the rule, so the same code produced blue. Some older articles and examples still describe that behaviour.
The practical advice is simple: put a rule's own declarations first, then nested rules. Code written that way behaves identically everywhere and is easier to read.
A Real Component, Before and After
Here is a typical navigation component written in flat CSS:
.nav { display: flex; gap: 1rem; }
.nav a { color: #374151; text-decoration: none; }
.nav a:hover, .nav a:focus-visible { color: #7c3aed; }
.nav a[aria-current="page"] { font-weight: 700; }
@media (max-width: 40rem) {
.nav { flex-direction: column; }
}And with native nesting:
.nav {
display: flex;
gap: 1rem;
@media (max-width: 40rem) {
flex-direction: column;
}
a {
color: #374151;
text-decoration: none;
&:hover,
&:focus-visible { color: #7c3aed; }
&[aria-current="page"] { font-weight: 700; }
}
}The nested version is longer by a few lines, but every style for the component lives in one block, and the media query sits next to the declaration it changes. Note that the nested a rule has the same specificity (0,1,1) as .nav a, so the refactor does not change which rules win.
How Deep Should You Nest?
The specification puts no limit on depth, but deep nesting creates long, highly specific selectors that are hard to override, the same problem Sass codebases have suffered from for years. A practical guideline many teams use:
- One level for children and states of a component.
- Two levels at most, for things like a hover state on a child.
- Never mirror the HTML structure. If you find yourself nesting
header nav ul li a, give the link a class instead.
Nesting is a tool for keeping related rules together, not a way to describe the DOM tree. Our CSS organization tutorial covers structuring larger stylesheets.
Browser Support, Feature Detection and Build Tools
- Chrome and Edge 112+ (relaxed syntax from 120)
- Safari 16.5+ (relaxed syntax from 17.2)
- Firefox 117+
Together that is Baseline 2023 support. For a project that must support older browsers, you have two options.
Detect support in CSS
@supports selector(&) {
/* nested rules here */
}This is rarely worth it for whole stylesheets, because browsers without nesting support also ignore the nested rules, so the fallback would have to be a complete flat copy.
Flatten at build time
Tools such as Lightning CSS and the postcss-nesting plugin can compile native nesting to flat selectors for older targets while you keep writing standard syntax. Because they follow the specification, they also reproduce the :is() behaviour, unlike Sass. If you already run PostCSS, adding nesting support is a one-line change. Remove the transform once your browser targets support nesting natively.
Can You Drop Sass Now?
For many projects, yes. Native CSS now covers the three features that justified Sass for most teams: nesting, variables through custom properties, and modular organisation through cascade layers. Custom properties are also more powerful than Sass variables because they cascade and can change at runtime.
Keep Sass, or another preprocessor, if you depend on:
- Mixins and functions that generate repeated blocks of CSS.
- Loops and maps that generate utility classes.
- BEM concatenation with
&__elementand&--modifierthroughout the codebase. - @use and @forward modules for sharing tokens across many files.
A gradual path works well: write new components in plain CSS with native nesting, and convert existing Sass files only when you touch them. Sass also passes plain CSS nesting through unchanged in .css files, so the two can coexist.
Key Takeaways
- Native nesting works in every modern browser with no build step.
- Use
&to attach to the parent element (&:hover,&.active). Without it, the nested selector is a descendant. &__titledoes not concatenate. Write full BEM class names.- Parent selector lists behave like
:is(), so an ID in the list raises the specificity of every nested rule. Use:where()or split the list. - Put declarations before nested rules.
- Nest one or two levels deep, not more.
Your next step: take one component stylesheet, nest its states and media queries, and search for &__, &- and comma-separated parents before you ship it.
Frequently Asked Questions
Can you nest CSS without Sass?
Yes. Native CSS nesting lets you write rules inside other rules directly in a .css file. It is supported in Chrome and Edge 112+, Safari 16.5+ and Firefox 117+, with no build step required.
Why doesn't &__element work in native CSS nesting?
Native nesting does not concatenate text. & is a reference to the parent selector, not a string, so &__title does not become .card__title. Write the full class name instead, or keep Sass for files that rely on BEM concatenation.
When do I need the & in CSS nesting?
Use & when the nested selector should apply to the same element as the parent, such as &:hover, &.active or &[disabled]. Without &, a nested selector is treated as a descendant, so :hover alone means any hovered descendant.
Does CSS nesting affect specificity?
Yes, in one important case. The parent selector behaves like :is(), which takes the specificity of its most specific argument. If the parent is a list such as .card, #featured, every nested rule gets ID-level specificity. Split the list or wrap it in :where().
Can I nest media queries in CSS?
Yes. @media, @supports, @container, @layer and @scope can be nested inside a style rule. Declarations inside them apply to the parent selector, so you do not need to repeat it.
Is native CSS nesting slower than flat CSS?
There is no meaningful performance difference for typical stylesheets. Performance depends on the resulting selectors, so the same advice applies as for flat CSS: keep selectors short and avoid deep nesting.
References and Further Reading
- Using CSS nesting · MDN Web Docs
- CSS nesting and specificity · MDN Web Docs
- CSS Nesting Module Level 1 · W3C
- Sass and Native Nesting · Sass
Related Articles
Continue your learning journey with these related topics