Sub topic 5
Nesting rules and the & selector
Nesting lets you write a child rule inside its parent rule. Sass compiles it to flat CSS with the full selectors written out. This chapter assumes you know CSS selectors, the cascade, and the variable syntax from the previous chapter.
Nesting a rule inside a rule
In plain CSS, you repeat the parent selector for every descendant. In SCSS, you place the child inside the parent's braces.
.nav {
display: flex;
ul {
list-style: none;
}
a {
color: #3366cc;
}
}This compiles to:
.nav {
display: flex;
}
.nav ul {
list-style: none;
}
.nav a {
color: #3366cc;
}Each nested selector gets the parent selector and a space (the descendant combinator) placed in front of it. Combinators such as >, + and ~ also work at the start of a nested selector.
The parent selector &
The & symbol stands for the full parent selector. Use it when the child must attach to the parent instead of sitting below it. Here is the card example from the topic plan:
$primary: #3366cc;
$radius: 4px;
.card {
padding: 1rem;
border: 1px solid $primary;
border-radius: $radius;
&:hover {
background: rgba($primary, 0.1);
}
&__title {
font-weight: 700;
}
}It compiles to:
.card {
padding: 1rem;
border: 1px solid #3366cc;
border-radius: 4px;
}
.card:hover {
background: rgba(51, 102, 204, 0.1);
}
.card__title {
font-weight: 700;
}Two uses of & appear here:
&:hoverjoins a pseudo-class to the parent, giving.card:hover.&__titleappends a suffix to the parent's name, giving.card__title. This is the usual way to write BEM-style names (block, element, modifier) without repeating the block name.
You can also put & after other text to apply a parent in a different context:
.button {
color: white;
.dark-theme & {
color: #ccc;
}
}This produces .dark-theme .button. The parent moves to the end of the selector.
How nesting expands
Common errors
- Nesting too deep. Each level adds to the selector. Four or five levels give long selectors such as
.page .sidebar .list .item a. These are hard to override and tightly tied to the HTML structure. A common guideline is to stay within three levels. - Forgetting
&when you need it. Writing:hover { ... }inside.cardproduces.card :hover, which matches hovered descendants. It does not match the card itself. Write&:hover. - Expecting a space where there is none.
&-largegives.card-large, with no space. A bare-largeis not a valid selector. - Putting
&in the wrong place in a list.&works in selector lists..a, .b { &:hover {} }gives.a:hover, .b:hover.
Nested properties and media queries
Media queries can sit inside a rule. Sass lifts them out and keeps the selector inside:
.sidebar {
width: 100%;
@media (min-width: 48rem) {
width: 20rem;
}
}This outputs a @media block containing .sidebar { width: 20rem; }. It keeps a component's responsive behavior next to its base styles.
Exercise
Given this SCSS, what selectors does it compile to? .tab { &.active { color: red; } .icon & { margin: 0; } > span { display: block; } }
Show answer
&.active gives .tab.active. .icon & gives .icon .tab. > span gives .tab > span.
Exercise
A developer writes :focus { outline: 2px solid blue; } inside .field and finds that the field itself gets no outline. Why, and how do they fix it?
Show answer
Without &, Sass inserts a descendant combinator and outputs .field :focus, which matches focused elements inside the field. Writing &:focus outputs .field:focus.