Sub topic 4
Child and sibling combinators
This chapter assumes you know the descendant, child (>), next-sibling (+) and subsequent-sibling (~) combinators in ordinary CSS. It also assumes you know the & nesting selector from the previous chapters. Here you will learn how a nested rule that starts with a combinator is resolved, how to use it for child and sibling relationships, and which mistakes come up most often.
The default relationship is "descendant"
A nested selector that has no combinator in front of it is relative to the parent with a descendant relationship. The parent rule is the reference point, and the nested selector describes something inside it.
.menu {
a {
color: navy;
}
}This is the same as .menu a. The browser inserts an implicit & and a descendant combinator (a space) in front of a. It matches every a anywhere inside .menu, however deep.
That is often too broad. If a .menu contains a submenu, the rule above also styles the links in the submenu. A child combinator narrows the match to direct children only.
Starting a nested selector with a combinator
Native nesting lets a nested selector begin with a combinator. The combinator is then applied between the parent and the rest of the selector.
.menu {
> li {
list-style: none;
}
+ .menu {
margin-block-start: 2rem;
}
~ .footnote {
font-size: 0.875rem;
}
}Each nested rule resolves as if & stood in front of the combinator:
| Nested form | Equivalent flat selector | Matches |
|---|---|---|
| `> li` | `.menu > li` | `li` elements that are direct children of `.menu` |
| `+ .menu` | `.menu + .menu` | A `.menu` immediately after a `.menu` |
| `~ .footnote` | `.menu ~ .footnote` | Any `.footnote` after a `.menu`, with the same parent |
| `li` | `.menu li` | `li` elements at any depth inside `.menu` |
Writing the & yourself gives the same result. & > li and > li are identical, so choose one style and keep to it. The version without & is shorter. The version with & makes the reference point explicit, which helps when a rule is long.
.menu {
& > li {
list-style: none;
}
}This works in browsers that support relaxed nesting syntax. Early implementations of nesting required & for some forms. Browser support is covered in a later chapter.
How the browser resolves a nested selector
Resolution is mechanical. For each nested selector, the browser checks whether it contains &. If it does, & is replaced by the parent selector, treated as if it were wrapped in :is(). If it does not, the browser puts & and a descendant combinator in front of it. A selector that starts with a combinator is treated as & followed by that combinator.
Because the diagram fence must follow the required format, here it is in full.
This order explains most surprises. For example, :hover written alone inside .menu has no & and no leading combinator. It therefore becomes .menu :hover, which matches hovered descendants and not the menu itself. To style the menu on hover, write &:hover.
Sibling combinators
Sibling combinators relate elements that share a parent. They are a good fit for nesting, because the nested rule reads as "what comes after this element".
Next sibling: `+`
The next-sibling combinator matches an element that comes directly after another one.
.alert {
padding: 1rem;
border: 1px solid #c00;
+ .alert {
margin-block-start: 0.5rem;
}
}This resolves to .alert + .alert. Only an .alert that directly follows another .alert gets the top margin, so the first one in a run has none.
Subsequent sibling: `~`
The subsequent-sibling combinator matches every later sibling, not only the next one, as long as they share a parent.
.step.is-current {
font-weight: 700;
~ .step {
opacity: 0.5;
}
}This resolves to .step.is-current ~ .step. Every step after the current one is dimmed. Steps before it are not affected, because ~ only looks forward in document order.
Repeating the parent with `& + &`
You can use & on both sides of a combinator. This selects an element that follows another element of the same kind.
.card {
padding: 1rem;
& + & {
margin-block-start: 1rem;
}
}This resolves to .card + .card. It has the same effect as the + .card form, but you do not have to repeat the class name, so renaming .card later takes one edit.
The "owl" spacing pattern
A common layout rule adds space between consecutive children without adding space before the first one. In flat CSS it is .stack > * + *. Nested, it becomes:
.stack {
> * + * {
margin-block-start: 1rem;
}
}The selector starts with >, so it is relative to .stack. The rest, * + *, is an ordinary selector: any element that has a previous sibling. The result is the flat selector .stack > * + *, which matches every direct child of .stack except the first.
Selector lists with combinators
A comma-separated list of nested selectors can mix forms. Each item is resolved independently.
.toolbar {
> button,
> a,
+ .toolbar {
padding: 0.5rem 1rem;
}
}This is equivalent to .toolbar > button, .toolbar > a, .toolbar + .toolbar. Each item in the list gets its own implicit &, so you do not need to repeat it on every line.
When the parent is itself a list, the combinator applies to the whole list.
.sidebar, .drawer {
> nav {
display: grid;
}
}This behaves like :is(.sidebar, .drawer) > nav. It matches a nav that is a direct child of either .sidebar or .drawer. Specificity follows the :is() rule, which the specificity chapter covers in detail.
Specificity of combinators in nested rules
Combinators add nothing to specificity. Only the selectors on either side count. A nested rule has the same specificity as its flat equivalent:
.menu { > li {} }is.menu > li, with specificity (0,1,1)..card { & + & {} }is.card + .card, with specificity (0,2,0).
If the parent is a selector list, & takes the specificity of the most specific item in the list. A parent list of #main, .note gives (1,0,0) for & even when the element matched through .note. Keep this in mind when you nest combinator rules under a list that contains an ID selector.
Common errors
- Expecting
>to be needed for child matching. Without it, the selector matches descendants at any depth. If a rule leaks into deeper levels, add>. - Putting the combinator at the end.
.menu { li > {} }is invalid. A combinator needs a selector on its right side. The parent can sit on the left only through&or the implicit leading&. - Using
~and expecting earlier siblings. Sibling combinators only look forward. To style an element based on a later sibling, you need:has(), for example&:has(+ .error). - Forgetting the shared parent.
+and~only match elements with the same parent. A.menu + .menurule will not match two menus in different containers. - Writing
.menu { + li {} }and expecting a child. The rule matches a siblingliafter.menu. Use> lifor children.
Putting it together
This example combines a child combinator, both sibling combinators and & on both sides.
.tabs {
display: flex;
> .tab {
padding: 0.5rem 1rem;
border-bottom: 2px solid transparent;
& + & {
margin-inline-start: 0.25rem;
}
&.is-active {
border-bottom-color: currentColor;
~ .tab {
color: #666;
}
}
}
}The rules resolve as follows:
.tabs > .tabstyles every tab that is a direct child..tabs > .tab + .tabadds the gap between tabs. Here&inside the nested rule means the full.tabs > .tab..tabs > .tab.is-activemarks the active tab..tabs > .tab.is-active ~ .tabgreys every tab after the active one.
Notice that & in a deeper rule stands for the whole chain of selectors above it, not only the nearest parent.
Exercise
Write the flat (non-nested) selector that this rule produces for its nested part, and say which elements it matches. .gallery { > figure { ~ figure { margin-block-start: 1rem; } } }
Show answer
The flat selector is .gallery > figure ~ figure. It matches any figure that is a direct child of .gallery and that comes after another figure with the same parent. The first figure is not matched.
Exercise
A page has a .list containing .item elements. You want a 1rem gap above every .item except the first, and you want to write the class name .item only once. Which nested rule achieves this?
Show answer
Write it inside .item with & on both sides: .item { & + & { margin-block-start: 1rem; } }. This resolves to .item + .item, so only an .item that directly follows another .item gets the margin.
Exercise
Why does .menu { :hover { color: red; } } not change the color of the menu when you hover over it, and how do you fix it?
Show answer
The nested selector has no & and no leading combinator, so it is resolved as .menu :hover, which matches hovered descendants of .menu. To style the menu itself, write &:hover.