Sub topic 5
Descendant, child, and sibling combinators
This chapter assumes you can write type, class, and ID selectors and that you know how HTML elements nest. A combinator is a symbol between two selectors that describes how the elements relate in the document tree. Combinators narrow a match by position, so you can style an element by where it sits and not only by what it is.
The four combinators
| Combinator | Syntax | Matches |
|---|---|---|
| Descendant | `A B` (a space) | Any `B` inside `A`, at any depth |
| Child | `A > B` | A `B` that is a direct child of `A` |
| Adjacent sibling | `A + B` | A `B` that comes immediately after `A`, with the same parent |
| General sibling | `A ~ B` | Any `B` that comes after `A`, with the same parent |
The browser reads a selector from right to left. The rightmost part, called the key selector, is the element that gets styled. Everything to its left only filters which of those elements qualify.
Descendant and child
The descendant combinator is just whitespace. It matches at any depth, so it is broad.
The child combinator > matches only one level down. Use it when you want to avoid styling nested content by accident.
nav a { color: navy; }
nav > a { padding: 0.5rem; }
h2 + p { margin-top: 0; }Consider this HTML:
<nav>
<a href="/">Home</a>
<ul>
<li><a href="/docs">Docs</a></li>
</ul>
</nav>nav a matches both links. nav > a matches only "Home", because the "Docs" link is a child of li, not of nav.
Sibling combinators
Siblings share a parent. The adjacent sibling combinator + selects the element right after another one. Here h2 + p styles only the first paragraph after each h2.
The general sibling combinator ~ selects every later sibling that matches. It does not require them to be adjacent.
h2 ~ p { color: #333; }Neither sibling combinator looks backward. CSS cannot select an element based on a sibling that comes after it with these combinators alone.
flowchart TDIn this tree, nav a reaches both a nodes. nav > a reaches only the first.
Combinators and specificity
A combinator adds nothing to specificity. Only the selectors on either side count. For example, nav > a has specificity (0,0,2), the same as nav a.
ul li.active > a[href] { color: red; } /* (0,2,3) */The class .active and the attribute [href] give 2 in the middle column. The types ul, li, and a give 3 in the last column.
Common errors
- Over-long chains.
main section article p spanties your CSS to the HTML structure and raises specificity. Prefer a class on the element you want. - Using a space when you meant
>. Nested components then inherit styles meant for the outer one. - Expecting
+to skip elements.h2 + pdoes not match if another element sits between the heading and the paragraph. - Forgetting that text nodes are ignored. Whitespace and text between elements do not break adjacency. Only elements count.
Exercise
Given <div class="card"><p>One</p><section><p>Two</p></section><p>Three</p></div>, which paragraphs does .card > p match, and which does .card p + p match?
Show answer
.card > p matches "One" and "Three", because "Two" is inside the section and is not a direct child. .card p + p matches nothing: no p is immediately followed by another p among siblings, since the section sits between "One" and "Three".