1.2 · Selectors & structure

Native nesting

Nest rules inside rules with &, the way Sass did, in plain CSS.

Nesting rules inside rules was the main reason many people reached for Sass. It is now plain CSS. & stands for the parent selector. Use it for pseudo-classes (&:hover), compound selectors (&.secondary) and context (.compact &). Media and container queries nest too.

One difference to Sass: & can’t glue strings, so &__icon for BEM-style names doesn’t work.

Before
.button { … }
.button:hover { … }
.button .icon { … }
.button:hover .icon { … }
.compact .button { … }
@media (prefers-reduced-motion) {
  .button { … }
}
Now
.button {
  &:hover { … }
  & .icon { … }
  &:hover .icon { … }
  .compact & { … }

  @media (prefers-reduced-motion) {
    …
  }
}