Sub topic 6
Styling by ancestor context with & at the end
This chapter assumes you know CSS selectors, combinators, and basic specificity, and that you have seen the & nesting selector in a compound position such as &.is-open. Here you will put & at the end of a nested selector to style an element based on where it sits in the page.
The problem: context lives outside the component
Components often change appearance based on something above them. A common case is a theme class on <body> or <html>. Without nesting you would write the rule in a separate place:
.nav {
background: #fff;
}
.dark-theme .nav {
background: #222;
}The second rule is far from the first, so the two are easy to separate by accident. Nesting lets you keep both rules together.
Placing & after another selector
When & is not the first thing in a nested selector, the nested selector describes context around the parent. The & stands for the parent selector, wherever it appears.
.nav {
display: flex;
background: #fff;
.dark-theme & {
background: #222;
}
}This is equivalent to:
.nav {
display: flex;
background: #fff;
}
.dark-theme .nav {
background: #222;
}Read .dark-theme & as "a .nav that is a descendant of .dark-theme". The space is a descendant combinator, exactly as in a flat selector.
Compare the three placements
The position of & changes the relationship to the parent.
| Nested selector | Flat equivalent | Meaning |
|---|---|---|
| `a` | `.nav a` | A descendant of `.nav` |
| `&.is-open` | `.nav.is-open` | `.nav` that also has the class |
| `.dark-theme &` | `.dark-theme .nav` | `.nav` inside `.dark-theme` |
Other combinators before &
You can put any combinator between the context selector and &. This example uses a child combinator and a sibling combinator.
.card {
margin: 0;
.grid > & {
margin: 0.5rem;
}
.heading + & {
border-top: 1px solid #ccc;
}
}The first rule matches a .card that is a direct child of .grid. The second matches a .card immediately after a .heading.
Adding more than a class
The context can be any selector, such as an attribute, a pseudo-class, or an element. The element keeps its own identity because & still points at the parent.
.button {
background: royalblue;
[data-theme="contrast"] & {
background: black;
}
:focus-visible > & {
outline: 2px solid orange;
}
}The :focus-visible > & rule matches a .button whose direct parent currently has keyboard focus.
Combining context with other parts
You can use & more than once, and you can mix it with a compound form in the same selector.
.item {
color: #333;
.list:hover &.active {
color: crimson;
}
}This is .list:hover .item.active. The first & position is after the descendant combinator, and the .active class attaches directly to &.
Specificity
Specificity is computed as if you had written the flat selector, with one exception: when the parent is a selector list, & takes the specificity of the most specific item in that list (as :is() does). For a single-class parent such as .nav, .dark-theme & is (0,2,0). That is higher than the plain .nav rule at (0,1,0), so the themed rule wins regardless of source order.
A mistake to avoid
A selector that starts with & always refers to the parent first. Do not write & .dark-theme when you mean "inside .dark-theme". That selector means a .dark-theme descendant of .nav, which is the opposite relationship.
Exercise
Write a nested rule inside .tooltip that sets opacity: 1 when the tooltip is a direct child of an element with the class .is-active. What flat selector does it produce?
Show answer
Use .is-active > & { opacity: 1; } inside .tooltip. It produces .is-active > .tooltip.
Exercise
Inside .link, you write .footer & { color: gray; }. What is its specificity, and does a later plain .link { color: blue; } override it?
Show answer
The specificity is (0,2,0), the same as .footer .link. The later .link rule has (0,1,0), so it does not override the gray color.