Sub topic 5
Attaching classes and pseudo-classes with &
This chapter covers compound selectors written with the & nesting selector. You should already know CSS class, type, attribute, and pseudo-class selectors, the difference between a descendant selector and a compound selector, and the basics of specificity. Earlier chapters introduced & as a stand-in for the parent rule's selector. Here you will use it to attach more conditions to the parent element itself.
Compound selectors: same element, more conditions
A compound selector is a sequence of simple selectors with no combinator between them. button.primary:hover is compound. It matches one element that is a button, has the class primary, and is hovered. button .primary is not compound, because the space is a descendant combinator and it matches two different elements.
Inside a nested rule, & is the parent selector. If you write something directly against it, with no space, you get a compound selector.
.button {
padding: 0.5rem 1rem;
background: #eee;
&:hover {
background: #ddd;
}
&.primary {
background: #06c;
color: white;
}
&[disabled] {
opacity: 0.5;
}
}This is equivalent to the following flat CSS:
.button { padding: 0.5rem 1rem; background: #eee; }
.button:hover { background: #ddd; }
.button.primary { background: #06c; color: white; }
.button[disabled] { opacity: 0.5; }Every nested rule here targets the same .button element as the parent. Each one adds a state (:hover), a modifier class (.primary), or an attribute condition ([disabled]).
Why the space matters
Whitespace changes the meaning. Compare these two nested rules:
.card {
&.featured {
border-color: gold;
}
& .featured {
border-color: orange;
}
}The first rule matches an element that is both .card and .featured. The second matches any .featured element inside a .card. They look similar and behave very differently.
| Nested selector | Resolves to | Meaning |
|---|---|---|
| `&.featured` | `.card.featured` | The card itself has the class |
| `& .featured` | `.card .featured` | A descendant has the class |
| `&:hover` | `.card:hover` | The card is hovered |
| `& :hover` | `.card :hover` | Any hovered descendant |
| `&::before` | `.card::before` | Pseudo-element of the card |
A common bug is & :hover when you meant &:hover. The first rule fires when the pointer is over any child, and styles the child, not the card.
Pseudo-classes and pseudo-elements
All the usual pseudo-classes work after &. This includes state pseudo-classes such as :focus-visible, :checked, and :disabled, and structural ones such as :first-child and :nth-child(). It also includes functional ones such as :not(), :is(), and :has().
.field {
border: 1px solid #999;
&:focus-within {
border-color: #06c;
}
&:not(:last-child) {
margin-block-end: 1rem;
}
&:has(input:invalid) {
border-color: crimson;
}
&::after {
content: "";
}
}Pseudo-elements also attach with &. &::before becomes .field::before. You can combine them, as in &:hover::after, which becomes .field:hover::after.
Several conditions at once
You can chain as many simple selectors onto & as you need. They all apply to the same element:
.tab {
color: #444;
&.active:not([aria-disabled="true"]):hover {
color: #06c;
}
}This resolves to .tab.active:not([aria-disabled="true"]):hover.
Type selectors cannot follow `&` directly
A type selector such as div or a cannot come right after & without a separator. &div is invalid, because a type selector must come first in a compound selector, and & stands for the whole parent, not a prefix. If you want an element of a certain type inside the parent, use a descendant or child relationship, such as & div or & > div. Both are valid.
If you want the parent only when it is a particular element type, put the type first, as in div&. This is allowed in modern implementations, and it resolves to div.card when the parent is .card. Use it sparingly, because it is easy to misread.
.card {
a& {
text-decoration: none;
}
}This matches a.card only. It styles the card when the card is a link element.
Attaching to a parent that is itself compound
The parent may already be compound, such as a.link. Then & represents the whole compound, and anything attached to it adds to the conditions:
a.link {
color: #06c;
&:visited {
color: purple;
}
&.external::after {
content: " ↗";
}
}The results are a.link:visited and a.link.external::after. Nothing is lost from the parent. This differs from simple text pasting, which matters in the next section.
Why `&` is not string concatenation
Sass users know the &-suffix trick:
.card {
&-title { font-weight: 600; }
}In Sass this produces .card-title. Native CSS does not do this. Native & is a selector reference, not text, so &-title is invalid. If you need .card-title, write it as a separate rule or nest .card-title as a descendant:
.card {
& .card-title {
font-weight: 600;
}
}Later chapters compare native nesting and Sass in more detail. For now, remember the rule: you can attach a complete simple selector (class, ID, attribute, pseudo-class, pseudo-element), but you cannot attach a partial name.
Specificity of compound nested selectors
A compound nested selector adds up like any other. &.primary inside .button is .button.primary, which has specificity (0,2,0). &:hover is also (0,2,0), because a pseudo-class counts the same as a class. This is the same as in flat CSS.
Specificity matters when you order modifier states:
.button {
background: #eee;
&:hover {
background: #ddd;
}
&.primary {
background: #06c;
}
}Both &:hover and &.primary have specificity (0,2,0), so the later one wins when both apply. A hovered primary button gets #06c and the hover color is lost. To fix it, add a more specific rule or reorder:
.button {
background: #eee;
&.primary {
background: #06c;
&:hover {
background: #05a;
}
}
}Here the inner rule is .button.primary:hover, with specificity (0,3,0). It beats both of the other rules. This also shows that nested rules can nest further, and that & always refers to the full resolved selector of the immediate parent.
Common errors
- A stray space.
& .activeand&.activeare different selectors. If a state style never applies, check for a space after&. - Trying
&-suffixor&__element. This is Sass syntax. It is invalid in native CSS, and the browser drops the rule. - Equal specificity and order. Modifier and state rules of equal specificity are resolved by source order. Put the more important state last, or nest the combination explicitly.
- Using
&where the parent is a list. With.a, .bas the parent,&.xacts like:is(.a, .b).x. Later chapters cover how this affects specificity. - Reading
&::beforeas the parent's child. A pseudo-element is attached to the parent element. It is not a separate element in the DOM.
A worked example: a form control
This example puts compound selectors, pseudo-classes, and a modifier together:
.input {
border: 2px solid #999;
border-radius: 4px;
padding: 0.5rem;
&:focus-visible {
outline: 3px solid #06c;
outline-offset: 2px;
}
&[aria-invalid="true"] {
border-color: crimson;
}
&:disabled {
background: #f3f3f3;
cursor: not-allowed;
}
&.input-large {
font-size: 1.25rem;
padding: 0.75rem;
}
&::placeholder {
color: #777;
}
}Every rule here targets the .input element itself. If you had written & :disabled, the browser would look for a disabled element inside the input. No such element exists, so the rule would never match, and nothing would warn you.
Exercise
Inside .menu-item, what does each nested selector resolve to: &.open, & .open, and &:not(.open):hover? Which one would you use to style a .menu-item that has the class open?
Show answer
&.open resolves to .menu-item.open, & .open resolves to .menu-item .open, and &:not(.open):hover resolves to .menu-item:not(.open):hover. Use &.open to style a .menu-item that has the class open, because there is no space and so it is a compound selector on the same element. & .open targets a descendant instead.
Exercise
A .button has nested rules &:hover { background: #ddd; } followed by &.primary { background: #06c; }. A hovered primary button shows #06c instead of a hover color. Why, and how do you fix it by nesting?
Show answer
Both selectors have specificity (0,2,0), so the later rule wins by source order. Fix it by nesting a hover rule inside the primary rule: &.primary { background: #06c; &:hover { background: #05a; } }. The result is .button.primary:hover with specificity (0,3,0), which beats both other rules.