CSS Combinators
A simple selector like p or .card matches elements on its own. A combinator joins two selectors and describes a relationship between them: "a p inside an article", "an h2 directly after an img". Combinators let you style elements based on where they sit in the document tree without adding extra classes.
The four combinators
| Combinator | Syntax | Selects |
|---|---|---|
| Descendant | A B (space) |
Every B anywhere inside A |
| Child | A > B |
B that is a direct child of A |
| Next sibling | A + B |
The B immediately after A (same parent) |
| Subsequent sibling | A ~ B |
Every B after A (same parent) |
Descendant vs child
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.box { border: 2px solid #90a4ae; padding: 8px; }
.desc span { background: #ffe082; }
.child > span { outline: 3px solid #e53935; }
</style>
</head>
<body>
<div class="box desc child">
<span>direct child</span>
<p>paragraph with a <span>nested span</span> inside</p>
<span>another direct child</span>
</div>
<p>Yellow = <code>.desc span</code> (all descendants).<br>Red outline = <code>.child > span</code> (direct children only).</p>
</body>
</html>The nested span is yellow but has no red outline because its parent is the <p>, not the div.
Sibling combinators
Siblings share the same parent. + targets only the very next sibling; ~ targets all following siblings of that type.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
h3 { margin: 6px 0; color: #3949ab; }
h3 + p { font-weight: bold; color: #c62828; }
h3 ~ p { background: #e8eaf6; padding: 4px 8px; margin: 4px 0; }
</style>
</head>
<body>
<p>Before the heading — not affected.</p>
<h3>Heading</h3>
<p>First p after h3: bold red (+) and shaded (~)</p>
<p>Second p: shaded only (~)</p>
<div>a div</div>
<p>Third p: still shaded, ~ skips over the div</p>
</body>
</html>Tip: A neat trick:
li + li { border-top: 1px solid #ddd; }draws dividers between list items without a line above the first one.
Practical patterns
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; gap: 20px; }
ul { list-style: none; padding: 0; margin: 0; width: 150px; border: 1px solid #ccc; border-radius: 8px; }
li { padding: 8px 10px; }
li + li { border-top: 1px solid #ddd; }
.stack > * + * { margin-top: 12px; }
.stack > * { background: #e0f7fa; padding: 6px; margin: 0; }
input:checked + label { color: #2e7d32; font-weight: bold; text-decoration: line-through; }
</style>
</head>
<body>
<ul><li>Divider</li><li>between</li><li>items</li></ul>
<div class="stack"><p>Stack</p><p>"owl" spacing</p><p>* + *</p></div>
<div>
<input type="checkbox" id="t1"><label for="t1">Tick me</label><br>
<input type="checkbox" id="t2" checked><label for="t2">Done task</label>
</div>
</body>
</html>The * + * pattern (nicknamed the "lobotomized owl") adds spacing between consecutive children, but not before the first one.
Combining and chaining
Combinators can be chained: nav > ul > li > a matches links three levels down in a strict structure. Keep chains short, though — long selectors are fragile (they break when the HTML changes) and have higher specificity, making them harder to override.
| Selector | Reads as |
|---|---|
article p |
paragraphs inside articles |
ul > li |
list items that are direct children of a ul |
.error + .hint |
a hint right after an error |
h2 ~ img |
images that come after an h2 in the same parent |
.card:hover > .title |
the title of a hovered card |
The parent selector :has()
Combinators only look down or forward. The :has() pseudo-class lets you select a parent based on its children: .card:has(img) matches cards that contain an image. It's covered in the Pseudo-classes lesson.
Test yourself
What does div > p select?
- All p elements inside a div at any depth
- p elements that are direct children of a div
- The p right after a div
- divs that contain a p
Which selector matches only the element immediately after an h2?
- h2 ~ p
- h2 > p
- h2 + p
- h2 p
What does li + li { border-top: 1px solid; } achieve?
- A border on every li
- A border on the first li only
- A divider between items but not above the first
- Nothing