CSS nesting

Intermediate · 21 min

How native nesting lets a rule contain its own selectors and at-rules, what & means for specificity, and where it differs from Sass nesting.

Before thisCustom properties

Kevin Powell · 21 min

CSS nesting

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Native nesting lets you write child selectors, pseudo-classes and `@media` rules inside a parent rule, so the things that belong to a component sit together.
  • `&` stands for the parent selector. It is optional before a descendant like `.title`, but needed when you want the parent itself plus something, such as `&:hover` or `&.active`.
  • `&` behaves like `:is()` around the parent selector list, so its specificity is that of the most specific selector in the list. Nested rules can be more specific than they look.
  • Unlike Sass, you cannot build names by gluing text onto `&`: `&-title` does not match `.card-title`. BEM-style suffixes still need to be written out.
  • Support has been in all major browsers since late 2023, so native nesting can replace a preprocessor for this one job.

Why nesting exists

Plain CSS repeats the parent selector over and over:

.card { padding: 1rem; }
.card .title { font-size: 1.25rem; }
.card .title:hover { color: crimson; }
.card:focus-within { outline: 2px solid; }

Nothing is wrong with that, but a reader has to scan the file to find everything about a card, and renaming .card means changing four places. Preprocessors like Sass added nesting for exactly this, and it was one of the most-used reasons to compile CSS. Browsers now have their own.

.card {
  padding: 1rem;

  .title {
    font-size: 1.25rem;
    &:hover { color: crimson; }
  }

  &:focus-within { outline: 2px solid; }
}

What & means

& is the selector of the parent rule. Use it when you mean “the parent itself, with something attached”:

  • &:hover, &.active, &[aria-expanded="true"]: the parent with a state.
  • .dark &: the parent when it sits inside .dark. The & can go anywhere in the selector, not just at the start.
  • .title or & .title: both mean a descendant .title. Nested selectors that do not use & are descendant selectors.

The gotcha that bites newcomers is :hover without &. .card { :hover { } } means .card :hover, hovered things inside the card, not the card.

Declarations, nested rules and nested at-rules can be mixed, and declarations after a nested rule still apply to the parent. I confirmed that in current Chrome with p { color: black; & .q { color: red } color: green }, where the parent ended up green.

At-rules inside rules

.card {
  padding: 1rem;

  @media (width >= 40em) {
    padding: 2rem;
  }

  @container (inline-size > 30em) {
    display: grid;
  }
}

@media, @supports, @container and @layer can nest. The declarations inside apply to the parent selector when the condition holds. This is the biggest practical gain: a component’s breakpoints no longer live in a separate block at the bottom of the file.

Specificity: & acts like :is()

The parent selector inside a nested rule is wrapped as if by :is(), so the nested rule takes the highest specificity in the parent’s list. I ran this in Chrome:

.s1, #s2 { & .t { color: red } }
.u .t   { color: blue }

An element .t inside .u inside the #s2 element came out red, because & .t counts as (1,1,0), the weight of the ID in the list, and not (0,2,0). A deeply nested rule starting from an ID-containing selector list is heavy. Keep nesting shallow, two or three levels, and avoid IDs in the outer selector. The specificity lesson has the arithmetic, and :is(), :where() and :has() is the next lesson.

What does not work like Sass

Sass lets you write &-title or &__title and get .card-title. Native & is a selector, not a string. I checked: with .a { &-b { color: blue } }, an element with class a-b did not match. If your methodology depends on suffix gluing (see BEM), write the full class names, which also helps searching a codebase for .card__title.

Sass-only features also stay with Sass: @extend, mixins and @use modules. That is covered in the preprocessors lesson.

When nesting makes things worse

Nesting rewards short, flat structures. If every selector is .page .section .card .title .link, nesting just hides a specificity and maintenance problem inside indentation. The usual guidance is to nest for states, pseudo-elements, media queries and one level of children, and otherwise use a class.

Browser support

All major browsers have supported native nesting since around the end of 2023, per MDN’s compatibility data: Chrome 120, Firefox 117 and Safari 17.2. If you must support older browsers, a build step can unnest for you, which the PostCSS and Lightning CSS lesson shows.

What the video covers

Kevin Powell’s “Getting started with CSS Nesting” (December 2025, about 21 minutes) says in its description that nesting is great but has a few important things to know, which he breaks down, and it links his videos on combinators and :focus-visible. I could not read a transcript, so the specific gotchas it covers are not described here. The specificity example and the &-suffix test above are from my own checks in Chrome. His 2022 video is in further viewing but predates relaxed nesting.

Where this fits

Nesting builds on custom properties as modern, native replacements for Sass features. Sass is still worth understanding, and CSS architecture discusses how much to nest.

Check yourself

  1. 01What does `.card { &:hover { ... } }` select, and what would `.card { :hover { ... } }` select?

    The first selects a hovered `.card`. The second is read as `.card :hover`, a hovered descendant of a card, because without `&` a nested selector is a descendant selector. When you mean the parent itself, write the `&`.

  2. 02You write `.card { &-title { color: red } }`, expecting `.card-title`. What happens?

    Nothing matches. In native CSS `&` is a selector, not text, so `&-title` is invalid. In Sass it is a string concatenation. Write `.card-title` as its own rule, or nest `.card-title` inside a rule for the wrapper.

  3. 03Given `.a, #b { & .c { color: red } }` and `.x .y .c { color: blue }` on an element matching both, which colour wins?

    Red. `&` is treated like `:is(.a, #b)`, and `:is()` takes the specificity of its most specific argument, so the nested rule counts as (1,1,0) however the parent matched. That beats `.x .y .c` at (0,3,0).

  4. 04Can you nest an `@media` rule inside a style rule?

    Yes. `.card { padding: 1rem; @media (width >= 40em) { padding: 2rem; } }` applies the inner declarations to `.card` only when the condition matches. That keeps a component's breakpoints next to the component. `@supports`, `@container` and `@layer` can be nested the same way.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark