Native nesting lets a style rule hold other style rules and group rules (@media, @supports, @container, @layer, @scope):
.card {
padding: 1rem;
> h2 { margin: 0; } /* .card > h2 */
p { color: gray; } /* .card p */
&:hover { outline: 1px solid; }
.theme-dark & { color: white; }
@media (width > 40rem) { padding: 2rem; }
}
A nested selector is relative: without & it means "descendant of the parent", and it may start with a combinator. & places the parent anywhere in the selector. Starting with a type selector (p) works since Chrome 120, Firefox 117 and Safari 17.2.
& isn't text substitution. It represents the elements the parent rule matches, as if written :is(<parent selector>). Sass concatenates strings at build time instead, which gives three differences:
- Specificity.
:is()takes its most specific argument. In#nav, .menu { & a {} }, the nested rule is (1, 0, 1) even for.menu a; Sass emits#nav a, .menu a, each with its own specificity. - No suffixes. Sass's
&__titlebuilds.card__title. In CSS,&-baris invalid and the nested rule is dropped. - Matching.
.a .b { .c & {} }is.c :is(.a .b): it matches a.bwith.aand.cancestors in either order. Sass writes.c .a .b, which requires that order.
& also can't represent pseudo-elements, the same restriction as :is().
Declarations after nested rules keep their place, wrapped in a nested declarations rule (Chrome 130, Firefox 132, Safari 18.2); earlier versions moved them to the top. The CSSOM keeps the structure: a nested p is a child rule with selectorText & p.