More CSS answers

by Danny
Published 2 min read

Yes. Native CSS nesting works in every major browser. Learn the syntax, when you need the & selector, and how it differs from Sass nesting.

The short answer

#

Yes. Native CSS nesting has worked in all major browsers since late 2023, so you can write nested rules in a plain .css file with no preprocessor. Use & when you want to join a selector to the parent, such as &:hover or &.is-active.


_16
.card {
_16
padding: 1rem;
_16
border-radius: 0.5rem;
_16
_16
h2 {
_16
margin-block: 0 0.5rem;
_16
}
_16
_16
&:hover {
_16
box-shadow: 0 2px 8px rgb(0 0 0 / 0.15);
_16
}
_16
_16
@media (width >= 48rem) {
_16
padding: 2rem;
_16
}
_16
}

Why it happens

#

For years nesting was the main reason people reached for Sass or Less. The CSS Nesting module brought the feature into the language itself. The browser reads a nested rule as if you had written the full selector: h2 inside .card becomes .card h2, and &:hover becomes .card:hover.

The first versions shipped in 2023 (Chrome 112, Safari 16.5) needed a symbol at the start of a nested selector, so h2 on its own did not work and you had to write & h2. The spec was relaxed soon after. Chrome and Edge 120, Firefox 117 and Safari 17.2 all support the relaxed syntax, where you can nest a bare element name. Any browser that is still updated today supports it.

How to fix it

#

Write nested rules directly in your CSS

No build step is needed. Nest descendants, pseudo-classes and at-rules such as @media, @supports and @container inside the parent rule.


_18
.nav {
_18
display: flex;
_18
gap: 1rem;
_18
_18
a {
_18
color: inherit;
_18
text-decoration: none;
_18
_18
&:hover,
_18
&:focus-visible {
_18
text-decoration: underline;
_18
}
_18
}
_18
_18
@container (width < 30rem) {
_18
flex-direction: column;
_18
}
_18
}

Know when you need the & selector

Without &, a nested selector is treated as a descendant. That matters for pseudo-classes and compound selectors.


_10
.button {
_10
:hover { color: red; } /* .button :hover (any hovered descendant) */
_10
&:hover { color: red; } /* .button:hover (the button itself) */
_10
_10
.icon { width: 1em; } /* .button .icon */
_10
&.primary { } /* .button.primary */
_10
_10
.dark-theme & { } /* .dark-theme .button */
_10
}

Putting & at the end, as in the last line, lets you style an element based on an ancestor.

Do not expect Sass string joining

In Sass, &__title inside .card produces .card__title. Native CSS does not join strings like that; & stands for the whole parent selector, not text to glue on. If you use BEM naming, write those selectors at the top level, or keep Sass for that part.


_10
/* Works in Sass, not in native CSS */
_10
.card {
_10
&__title { font-weight: 700; }
_10
}
_10
_10
/* Native CSS */
_10
.card__title {
_10
font-weight: 700;
_10
}

Keep a build tool only if you need old browsers

If your analytics show real traffic from browsers older than the versions above, a tool such as Lightning CSS or PostCSS with a nesting plugin can flatten nested rules at build time. You write the same native syntax and drop the plugin later.

Common pitfalls

#
  • Forgetting & on pseudo-classes. :hover nested without & targets hovered children, not the element itself.
  • Specificity surprises. Nested selectors behave as if the parent were wrapped in :is(). If the parent list contains an ID, such as .a, #b, every nested rule gets ID-level specificity, even for elements that only match .a.
  • Deep nesting. Four or five levels produce long, highly specific selectors that are hard to override. Two or three levels is usually plenty.
  • Mixing declarations after nested rules. It works in current browsers, but putting all plain declarations first keeps the rule easy to read.
  • Assuming every Sass feature exists. Nesting is native; mixins, loops and string joining are not.
#

Was this resource helpful?