Sub topic 5
Attaching classes and pseudo-classes with &
This chapter assumes you know class, attribute, and pseudo-class selectors, the difference between a descendant selector and a compound selector, and the basics of specificity. You should also have seen the & nesting selector from the previous chapters.
Descendant versus compound
A compound selector joins simple selectors with no space between them. .card.featured matches one element that has both classes. .card .featured matches a .featured element inside a .card. The two are different, and the space is the only thing that tells them apart.
In nested CSS, a bare nested selector is a descendant selector. To attach something to the parent element itself, you write & and put the extra selector directly after it, with no space.
.card {
padding: 1rem;
border: 1px solid #ccc;
& .title {
font-weight: 600;
}
&:hover {
border-color: #666;
}
}This is equivalent to:
.card { padding: 1rem; border: 1px solid #ccc; }
.card .title { font-weight: 600; }
.card:hover { border-color: #666; }& .title has a space, so it targets a descendant. &:hover has none, so it targets the .card element itself while the pointer is over it.
Attaching a class
Use &.class to style the parent element when it has an additional class. State classes such as is-open or is-active are the usual case.
.nav {
display: flex;
&.is-open {
background: #eee;
}
}The result is .nav.is-open. It matches an element with both classes. If you had written .is-open or & .is-open instead, the rule would only match an .is-open element inside .nav, and your styles would silently fail to apply to the nav itself.
Attaching pseudo-classes and pseudo-elements
The same pattern works for pseudo-classes, attribute selectors, and pseudo-elements.
.button {
background: #0645ad;
color: white;
&:hover,
&:focus-visible {
background: #04337a;
}
&[disabled] {
opacity: 0.5;
}
&::after {
content: "";
}
}Each nested selector is a compound selector built on .button. The first rule expands to .button:hover, .button:focus-visible.
A common mistake is writing :hover alone. In the relaxed syntax, a nested selector that starts with a colon is still treated as a compound with the parent. Browsers handle the leading :hover as if it were & :hover, which matches hovered descendants, not the parent. Always write &:hover when you mean the element itself.
Stacking several selectors
You can chain as many simple selectors after & as you need.
.tab {
&.is-active:not([disabled]):hover {
color: #0645ad;
}
}This expands to .tab.is-active:not([disabled]):hover. Its specificity is the sum of all the parts: three classes or pseudo-classes plus an attribute, so (0,4,0).
Putting a type selector after &
& can be followed by a class, attribute, or pseudo selector, but a type selector cannot directly follow it. &div is invalid. The reason is that a compound selector must start with a type selector if it has one, and & stands in for the parent selector in a way that cannot be combined with a type name. To select a div inside the parent, use a descendant selector such as & div.
Checking your understanding
Exercise
A .menu element gets the class is-open when it is expanded. You want to give the expanded menu itself a white background. Which nested rule is correct: & .is-open { background: white; } or &.is-open { background: white; }? What does the other one match?
Show answer
&.is-open { background: white; } is correct. It expands to .menu.is-open, which matches the menu element when it has both classes. & .is-open expands to .menu .is-open, which matches only an .is-open element nested inside .menu.
Exercise
Write a nested rule inside .link that changes the color to crimson when the link itself is hovered or has keyboard focus (use :focus-visible).
Show answer
.link { &:hover, &:focus-visible { color: crimson; } }. This expands to .link:hover, .link:focus-visible.