Sub topic 5
Descendant, child, and sibling combinators
A combinator is the symbol between two selectors that says how the elements must relate to each other in the document tree. This chapter assumes you can write type, class, and ID selectors, and that you can read basic HTML nesting. You should also know the specificity triple (IDs, classes, types) from the earlier chapters.
What a combinator does
A compound selector such as a.nav-link[href] describes one element. A complex selector joins compound selectors with combinators to describe a relationship. The browser matches a complex selector from right to left. The rightmost compound selector is the subject, which is the element that receives the styles. Everything to its left is a condition on its ancestors or earlier siblings.
CSS has four combinators:
| Combinator | Written as | Matches the subject when |
|---|---|---|
| Descendant | a space | any ancestor matches the left side |
| Child | `>` | the direct parent matches the left side |
| Adjacent sibling | `+` | the immediately preceding sibling element matches the left side |
| General sibling | `~` | any earlier sibling element matches the left side |
The descendant combinator
The descendant combinator is whitespace. nav a matches every a that has a nav anywhere above it, however deep.
<nav>
<a href="/">Home</a>
<ul>
<li><a href="/docs">Docs</a></li>
</ul>
</nav>nav a { text-decoration: none; }Both links match, because the second link is inside li, inside ul, inside nav. The number of levels between the two does not matter.
The space has a meaning, so it changes the selector. .card.featured is one element with both classes. .card .featured is an element with class featured inside an element with class card. Adding or removing a space is a common source of selectors that silently match nothing.
The child combinator
The child combinator > requires the left side to be the direct parent. Whitespace around it is optional, so nav>a and nav > a are the same.
nav > a { padding: 0.5rem; }Against the HTML above, only the Home link matches. The Docs link has li as its parent, so it is a descendant of nav but not a child.
You can chain combinators. This selector matches the Docs link, because it requires a specific path through the tree:
nav > ul > li > a { font-weight: bold; }Long chains tie your CSS to the exact markup. If someone wraps the ul in a div, the rule stops matching. A class on the target is usually more stable, and the last section returns to this.
The adjacent sibling combinator
Siblings are elements with the same parent. The + combinator matches an element whose immediately preceding sibling element matches the left side.
<article>
<h2>Title</h2>
<p>Intro</p>
<p>Second</p>
<img src="chart.png" alt="Chart">
<p>Third</p>
</article>h2 + p { margin-top: 0; }Only "Intro" matches. "Second" follows a p, not an h2. Two details matter here:
- Only elements count. Text nodes and comments between two elements are ignored, so whitespace in the markup does not break
+. - Direction matters.
+looks backward from the subject. Theh2cannot be styled based on thepthat follows it.
A selector can chain + as well. p + p matches "Second" only, because "Third" is preceded by the img.
The general sibling combinator
The ~ combinator matches any later sibling, not just the next one.
h2 ~ p { color: #333; }All three paragraphs match, because each has an h2 earlier in the same parent. The img does not match, because the subject must be a p. Siblings must share a parent, so a p inside a different div never matches.
A common use is a state-driven pattern with form controls:
input:checked ~ .panel { display: block; }The panel appears when a checkbox that comes earlier in the same parent is checked.
Spacing with the sibling combinators
A well-known pattern adds space between stacked siblings without adding space before the first one:
.stack > * + * { margin-top: 1rem; }The selector reads as "any child that follows another child". The first child has no preceding sibling, so it gets no margin. This works for any content, with no class needed on the children. * adds nothing to specificity, so the rule stays easy to override.
Combinators and specificity
Combinators contribute nothing to specificity. Only the compound selectors on either side count. Using the triple (IDs, classes/attributes/pseudo-classes, types/pseudo-elements):
nav a { color: navy; } /* (0,0,2) */
nav > a { color: teal; } /* (0,0,2) */
.card > .title { color: black; } /* (0,2,0) */
h2 + p { color: gray; } /* (0,0,2) */
ul li.active > a[href] { color: red; } /* (0,2,3) */The first two have equal specificity, so source order decides between them wherever both match. The > does not make the second rule stronger, even though it is more specific about structure. The same holds for +, ~, and the space. Specificity measures the parts of a selector, not how restrictive the match is.
Common errors
- Using a space when you mean a direct child.
ul astyles links in nested lists as well. Useul > li > awhen only the first level should match. - Expecting
+or~to look backward. They only look at earlier siblings of the subject. To style a parent or an earlier element based on what follows it, you need:has(), which a later chapter covers. - Expecting siblings across different parents to match.
h2 + pfails if a wrapperdivsurrounds thep. - Forgetting that a leading combinator has no left side. A selector such as
> ais not valid in a stylesheet rule. - Writing deep chains that mirror the markup. They have low specificity per part but break whenever the structure changes.
Choosing a combinator
- Use the descendant combinator for broad, context-based styling, such as
article pfor body text. - Use
>when nested content of the same type must not be affected. - Use
+for the element directly after another, such as spacing after a heading. - Use
~when the target may have other elements in between. - Prefer a class on the element when the style belongs to that element regardless of where it sits. A selector like
.nav-linkhas the same (0,1,0) score in every context and does not depend on the markup.
Putting it together
This page uses all four combinators. Save it and open it in a browser to check each result.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Combinators</title>
<style>
nav a { color: gray; }
nav > a { font-weight: bold; }
h2 + p { font-size: 1.25rem; }
h2 ~ p { color: darkgreen; }
article > * + * { margin-top: 1rem; }
</style>
</head>
<body>
<nav>
<a href="/">Home</a>
<ul><li><a href="/docs">Docs</a></li></ul>
</nav>
<article>
<h2>Title</h2>
<p>Intro</p>
<p>Second</p>
</article>
</body>
</html>Home is gray and bold. Docs is gray but not bold. "Intro" is large and green. "Second" is only green.
Exercise
Given <div class="box"><p>A</p><section><p>B</p></section><p>C</p></div>, which paragraphs does each selector match: .box p, .box > p, and p + p? What is the specificity of .box > p?
Show answer
.box p matches A, B, and C, since all are descendants of the box. .box > p matches A and C, because B's parent is the section. p + p matches nothing: C's previous sibling is the section, and A and B have different parents. .box > p has specificity (0,1,1), because the combinator adds nothing.
Exercise
A rule nav > a { color: teal; } appears before nav a { color: navy; } in the same stylesheet. A link is a direct child of nav. What colour is it, and why?
Show answer
Navy. Both selectors score (0,0,2), and the child combinator adds no weight, so the later rule wins by source order.