Sass Nesting
HTML is a tree, and Sass lets your styles follow the same shape. Instead of repeating a parent selector in every rule, you nest child rules inside it. The compiler joins the selectors for you.
Basic nesting
nav {
background: #2d2a32;
ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
}
a {
color: white;
padding: 8px 12px;
display: block;
text-decoration: none;
}
}nav {
background: #2d2a32;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
}
nav a {
color: white;
padding: 8px 12px;
display: block;
text-decoration: none;
}
Each nested selector becomes a descendant selector (nav ul, nav a).
The parent selector &
& stands for the full parent selector. Use it to attach pseudo-classes, modifiers or suffixes instead of creating a descendant:
.btn {
background: #cc6699;
color: white;
&:hover { background: #b35283; }
&:focus-visible { outline: 3px solid #f3c1da; }
&.is-disabled { opacity: 0.5; }
&--large { font-size: 1.25rem; } // suffix: .btn--large
.dark-theme & { color: #111; } // parent placed later: .dark-theme .btn
}.btn {
background: #cc6699;
color: white;
}
.btn:hover {
background: #b35283;
}
.btn:focus-visible {
outline: 3px solid #f3c1da;
}
.btn.is-disabled {
opacity: 0.5;
}
.btn--large {
font-size: 1.25rem;
}
.dark-theme .btn {
color: #111;
}
Written inside .btn |
Compiles to |
|---|---|
&:hover |
.btn:hover |
&.is-disabled |
.btn.is-disabled (same element, both classes) |
& .icon or just .icon |
.btn .icon (descendant) |
&--large |
.btn--large (suffix, great for BEM) |
.dark-theme & |
.dark-theme .btn |
Note:
&.active(no space) and& .active(space) mean different things: the first matches one element with both classes, the second a descendant.
Combinators
You can start a nested selector with >, + or ~:
.menu {
> li { display: inline-block; }
li + li { margin-left: 8px; }
h2 ~ p { color: gray; }
}.menu > li {
display: inline-block;
}
.menu li + li {
margin-left: 8px;
}
.menu h2 ~ p {
color: gray;
}
Nested properties
Properties that share a prefix, like font-family, font-size and font-weight, can be nested under the prefix followed by a colon:
.title {
font: {
family: Georgia, serif;
size: 1.5rem;
weight: 700;
}
margin: {
top: 0;
bottom: 0.5em;
}
}.title {
font-family: Georgia, serif;
font-size: 1.5rem;
font-weight: 700;
margin-top: 0;
margin-bottom: 0.5em;
}
This feature exists but is rarely used — the shorthand properties (font: …, margin: …) are usually clearer.
Don't nest too deep
Every nesting level adds a selector to the output, which raises specificity and couples your CSS to the exact HTML structure. A common rule of thumb: no more than three levels.
// Avoid: compiles to .page .sidebar .widget ul li a span
.page { .sidebar { .widget { ul { li { a { span { color: red; } } } } } } }
// Prefer: a dedicated class
.widget-link__label { color: red; }Sass nesting vs native CSS nesting
Modern browsers now support nesting in plain CSS too. The syntax is similar, but Sass resolves the nesting at compile time, so the output works in every browser, and Sass supports suffixes like &--large, which native CSS nesting does not.
Test yourself
What does .card { &:hover { color: red; } } compile to?
- .card :hover { color: red; }
- .card:hover { color: red; }
- .card &:hover { color: red; }
- &:hover .card { color: red; }
Inside .tab, which selector produces .tab--active?
- & .--active
- .--active
- &--active
- &.--active
Why should you avoid nesting many levels deep?
- Sass refuses to compile it
- It increases specificity and ties CSS to HTML structure
- It makes the CSS file smaller
- Browsers can't read it