Sub topic 4
Child and sibling combinators
This chapter assumes you know the descendant (space), child (>), next-sibling (+), and subsequent-sibling (~) combinators in flat CSS. It also assumes you know the & nesting selector from earlier chapters.
A nested rule can start with a combinator
A nested selector may begin with a combinator. The browser reads it as relative to the parent rule's selector. A leading >, +, or ~ behaves as if & came first.
.menu {
display: flex;
> li {
list-style: none;
}
+ .menu {
margin-top: 1rem;
}
~ .footnote {
color: #666;
}
}This is equivalent to the following flat CSS:
.menu {
display: flex;
}
.menu > li {
list-style: none;
}
.menu + .menu {
margin-top: 1rem;
}
.menu ~ .footnote {
color: #666;
}Writing & > li gives the same result as > li. The explicit & is optional here, so use whichever your team finds easier to read.
What each combinator selects
| Nested selector | Flat equivalent | Matches |
|---|---|---|
| `a` | `.menu a` | Any `a` inside `.menu`, at any depth |
| `> li` | `.menu > li` | `li` elements that are direct children of `.menu` |
| `+ p` | `.menu + p` | The `p` immediately after `.menu` |
| `~ p` | `.menu ~ p` | Every later `p` sibling of `.menu` |
The last two rows are easy to misread. A sibling combinator at the start of a nested rule does not style the parent's children. It styles elements next to the parent, as in the .menu + .menu and .menu ~ .footnote rules above.
Siblings of the same kind
A common use of + is spacing between repeated items. Put & on both sides to repeat the parent selector:
.card {
padding: 1rem;
& + & {
margin-top: 1rem;
}
}The browser resolves this to .card + .card. The rule applies to every .card that directly follows another .card, so the first card gets no extra margin.
You can also put & before a combinator and a different selector. This approach is more explicit and works the same way:
.tab {
opacity: 0.7;
& + .tab {
border-left: 1px solid #ccc;
}
&.active ~ .tab {
opacity: 0.4;
}
}The second rule combines a compound selector with a combinator. It resolves to .tab.active ~ .tab, which dims every tab after the active one.
Common errors
- Forgetting that a bare selector means descendant. Writing
li { ... }inside.menumatches list items at any depth, including those in nested submenus. Use> liwhen you only want direct children. - Reading
+as "inside the parent". The selector+ liinside.menutargets anlithat follows.menu. It does not target the firstliinside it. - Expecting
& +to differ from+. The two are equal. If you see a difference in your output, the cause is somewhere else, such as another rule with higher specificity. - Putting
&where the parent should not repeat.& &means.card .card, a card inside a card. It does not mean two adjacent cards. For adjacent cards use& + &.
Specificity
Combinators add no specificity. .menu > li has the same specificity as .menu li, which is (0,1,1). The nested form > li also gives (0,1,1). Choosing a combinator changes which elements match, not how strongly the rule competes in the cascade.
Exercise
Inside .step { ... }, which nested rule gives every .step after the first one a top border, and what flat selector does it produce?
Show answer
& + & { border-top: 1px solid #ccc; }. It resolves to .step + .step, so only a .step that directly follows another .step matches. The first one has no preceding .step and gets no border.
Exercise
Inside .list { ... }, what is the difference between li { } and > li { }?
Show answer
li { } resolves to .list li and matches li elements at any depth, including those in nested lists. > li { } resolves to .list > li and matches only direct children of .list.