Tutorials CSS Combinators
Ch 37 / 60
Chapter 37 Intermediate 8 min read CSS

CSS Combinators

Simple selectors target one element type. Combinators create relationships between selectors — they describe how two elements relate to each other in the HTML tree. There are four main combinators: descendant ( space), child (>), adjacent sibling (+), and general sibling (~). Together they let you write highly specific rules without adding extra classes to your HTML.

What you'll learn
Descendant combinator A B (space)
Child combinator A > B
Adjacent sibling A + B
General sibling A ~ B
How combinators affect specificity
Real-world usage patterns and pitfalls

Combinators at a Glance

A combinator is the symbol (or whitespace) placed between two simple selectors. It defines the required DOM relationship for the rightmost selector to match:

A B
Descendant (space)
div p
B is anywhere inside A — any depth.
A > B
Child (>)
ul > li
B is a direct child of A — one level only.
A + B
Adjacent Sibling (+)
h2 + p
B is the immediately next sibling of A.
A ~ B
General Sibling (~)
h2 ~ p
B is any following sibling of A.
Key rule about all combinators
Combinators are directional — they only look forward in the DOM (children and following siblings). There is no CSS combinator for selecting a parent or a preceding sibling. The :has() pseudo-class (Chapter 34) is the modern workaround for "parent-like" selection.

Descendant Combinator — A B (space)

sp
Descendant — whitespace between two selectors
Matches B anywhere inside A at any depth
DOM tree — nav a { color: blue } — ALL anchors inside nav match
<nav> ← nav (selector A) <a href="#">Link 1</a> ← ✓ direct child <ul> <li> <a href="#">Link 2</a> ← ✓ deeply nested — still matches! </li> <li> <a href="#">Link 3</a> ← ✓ any depth </li> </ul> </nav> <a href="#">Outside nav</a> ← ✗ not inside nav
Live demo — nav a → blue · a outside nav → grey
Inside nav: Direct link > ul > li > Nested link Also nested
Outside nav: Grey link (no match)
✓ Matches nav a
✓<nav><a> direct child
✓<nav><ul><li><a> nested
✓Any depth inside nav
✗ Does NOT match
✗<a> outside nav
✗nav itself (wrong selector)
CSS — Descendant combinator
/* Descendant: B anywhere inside A */
nav a           { color: #264de4; }         /* all links inside nav */
article p       { line-height: 1.7; }      /* all p inside article */
.card img       { border-radius: 8px; }     /* images inside .card */
.dark-mode *    { color: #f1f5f9; }       /* everything inside .dark-mode */

/* Chaining descendants */
main article p  { }  /* p inside article inside main */

/* ⚠ Pitfall: too broad, matches unexpected elements */
.container a   { color: red; }  /* ALL links, including nested components */

/* Fix: be more specific */
.nav-menu > li > a { color: blue; }  /* only top-level nav links */

Child Combinator — A > B

>
Child — A > B
Matches B only if it is a direct child of A — one level deep
DOM tree — ul > li { ... } — only direct li children match
<ul> ← ul (selector A) <li>Item 1</li> ← ✓ direct child <li> ← ✓ direct child Item 2 <ul> <li>Nested A</li> ← ✗ grandchild — NOT a direct child of outer ul <li>Nested B</li> ← ✗ </ul> </li> <li>Item 3</li> ← ✓ direct child </ul>
Live demo — ul > li → orange · nested li → normal (grey)
  • Item 1 — direct child ✓
  • Item 2 — direct child ✓
    • Nested A — NOT direct child ✗
    • Nested B — NOT direct child ✗
  • Item 3 — direct child ✓
✓ Matches ul > li
✓Direct <li> children of <ul>
✓One level deep only
✗ Does NOT match
✗<li> inside nested <ul>
✗Any deeper nesting
CSS — Child combinator (>)
/* Child: B must be direct child of A */
ul > li           { list-style: disc; }     /* top-level li only */
.nav > a          { font-weight: bold; }    /* direct link children of .nav */
form > label      { display: block; }      /* form-level labels (not nested) */
.card > .card-body{ padding: 1rem; }      /* direct card-body children */
details > summary { cursor: pointer; }    /* disclosure widget */

/* Real use: multi-level nav — only top level bold */
.nav > li > a { font-weight: 700; }           /* top level */
.nav li li > a{ font-weight: 400; }           /* nested level */

/* Descendant vs Child comparison */
.menu a     { }   /* ALL links inside .menu at any depth */
.menu > a   { }   /* ONLY direct link children of .menu */

Adjacent Sibling Combinator — A + B

+
Adjacent Sibling — A + B
Matches B only if it immediately follows A with the same parent — selects one element
DOM tree — h2 + p → only the FIRST p after each h2 matches
<article> <h2>Section heading</h2> ← h2 (selector A) <p>Lead paragraph</p> ← ✓ immediately follows h2 <p>Second paragraph</p> ← ✗ not adjacent to h2 (h2 is not its prev sibling) <p>Third paragraph</p> ← ✗ <h2>Another heading</h2> ← new h2 <p>Lead again</p> ← ✓ immediately follows this h2 </article>
Live demo — h3 + p → green · other paragraphs → grey

Section Heading

This paragraph immediately follows the h3 — it is green via h3 + p.

This is the second paragraph — it does NOT immediately follow h3, so it stays grey.

Third paragraph — also grey.

Another Heading

This immediately follows the second h3 — green again.

✓ Matches h2 + p
✓p that immediately follows h2
✓Same parent required
✓Only ONE p per h2
✗ Does NOT match
✗2nd, 3rd p after h2
✗p with a different element between it and h2
✗p inside h2
CSS — Adjacent sibling (+)
/* Adjacent sibling: B immediately follows A, same parent */
h2 + p            { font-size: 1.1em; color: #475569; }
label + input     { margin-top: 4px; }
input + span      { font-size: .875rem; color: red; }  /* inline error msg */
img + figcaption  { margin-top: 6px; font-style: italic; }

/* Checkbox trick — checked state affects next sibling */
input[type="checkbox"]:checked + label {
  text-decoration: line-through;
  color: #94a3b8;
}

/* Owl selector — margin between ALL consecutive siblings */
* + *  { margin-top: 1rem; }  /* lobotomised owl — Andy Bell */

/* Cleaner spacing variant */
p + p  { margin-top: .75em; }  /* space between consecutive paragraphs */
li + li{ border-top: 1px solid #e5e7eb; }  /* divider between items */

General Sibling Combinator — A ~ B

~
General Sibling — A ~ B
Matches ALL B elements that follow A (anywhere after, same parent) — not just the immediate one
DOM tree — h2 ~ p → ALL paragraphs after h2 match (not just first)
<section> <h2>Heading</h2> ← h2 (selector A) <p>First paragraph</p> ← ✓ follows h2 <blockquote>...</blockquote> ← skipped (not p) <p>Second paragraph</p> ← ✓ also follows h2, after blockquote <p>Third paragraph</p> ← ✓ all p's after h2 </section> <p>Before h2 — no match</p> ← ✗ precedes h2
Visual — A ~ B: pivot item then all following siblings
Before (no match)
→
h2 / pivot
→
p ✓
→
blockquote (skipped)
→
p ✓
→
p ✓

h2 ~ p matches all purple items (any p that comes after the h2, same parent)

✓ Matches h2 ~ p
✓All p that follow h2 (same parent)
✓Even with other elements between them
✓Multiple matches possible
✗ Does NOT match
✗p that appear BEFORE h2
✗p inside h2
✗p in a different parent
CSS — General sibling (~)
/* General sibling: ALL B that follow A, same parent */
h2 ~ p     { color: #475569; }           /* all p after h2 */
h2 ~ *     { margin-top: 1rem; }         /* all siblings after h2 */

/* CSS-only accordion / toggle via checkbox trick */
.toggle                        { display: none; }
input[type="checkbox"]:checked ~ .toggle {
  display: block;
}
/* Clicking checkbox shows/hides .toggle — no JS! */

/* CSS-only tab panels */
.tab-radio:checked ~ .tab-panel { display: block; }
.tab-panel                      { display: none; }

/* Style all items after a divider */
.divider ~ li { padding-left: 2rem; }

/* + vs ~ comparison */
h2 + p   { }  /* only the FIRST p after each h2 */
h2 ~ p   { }  /* ALL p after h2, including non-adjacent ones */

Specificity & Combining Combinators

Combinators themselves add no specificity. Specificity is calculated only from the simple selectors on both sides. The total is the sum of all simple selectors in the full expression:

Specificity calculation
nav a        → (0,0,2)  2 elements
ul > li      → (0,0,2)  2 elements
h2 + p       → (0,0,2)  2 elements
.nav > a     → (0,1,1)  class + element
.card > .body→ (0,2,0)  2 classes
#nav li > a  → (1,0,2)  ID + 2 elements
Chaining combinators
/* Multi-level: child then descendant */
nav > ul li a { }
/* nav → direct ul → any li → any a */

/* Sibling after child */
.form > label + input { }
/* input that follows label, in .form */

/* All three combined */
header > nav a + span { }
CSS — Real-world combinator patterns
/* ── Navigation patterns ── */
.nav > ul > li        { display: inline-block; }   /* only top-level items */
.nav > ul > li > a    { padding: 8px 16px; }         /* top-level links */
.nav li li > a        { padding: 6px 12px; }         /* dropdown links */

/* ── Form layout ── */
.form-group > label   { display: block; margin-bottom: 4px; }
.form-group > input   { width: 100%; }
label + input         { margin-top: 4px; }
input + .error-msg    { color: red; font-size: .875rem; }

/* ── CSS-only show/hide (checkbox trick) ── */
#menu-toggle                       { display: none; }
.hamburger                         { cursor: pointer; }
#menu-toggle:checked ~ .mobile-menu{ display: block; }

/* ── Owl selector — stack any children ── */
.stack > * + * { margin-top: 1rem; }

/* ── Remove last border in a list ── */
li + li { border-top: 1px solid #e5e7eb; }
/* = give all li a top border except the first one */

/* ── Table styling without extra classes ── */
thead + tbody tr:first-child td { border-top: 2px solid; }
tbody tr + tr td                { border-top: 1px solid #e5e7eb; }

Complete Combinator Reference

Combinator Name Syntax Matches when B… Specificity
space Descendant A B Is anywhere inside A (any depth) Sum of A + B
> Child A > B Is a direct child of A (one level) Sum of A + B
+ Adjacent sibling A + B Is the immediately next sibling of A Sum of A + B
~ General sibling A ~ B Is any following sibling of A Sum of A + B
Memory trick — DOM direction
All four combinators only select elements downward or forward in the DOM tree. Space and > look down (parent → children). + and ~ look across (older sibling → younger siblings). None of them look up (child → parent) or backward (younger → older sibling).
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
Complete reference covering all CSS combinators, selectors and specificity rules.
Download PDF

Try It Yourself

All four combinators in one editor. Change the HTML structure and watch the selectors respond:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 37

Questions & Answers
Both look downward in the DOM tree from parent to child, but at different depths:

Descendant (space) — A B: Matches B anywhere inside A, regardless of how deeply nested. nav a matches an anchor that is a direct child of nav, a grandchild, a great-grandchild — any depth at all.

Child (>) — A > B: Matches B only if it is a direct child of A — exactly one level deep. nav > a matches only anchors that are immediate children of nav; anchors nested inside a <ul> inside nav would NOT match.

When to use which: Use the descendant combinator for general scoping ("all links within this component"). Use the child combinator when you need to be precise about level — particularly in nested structures like multi-level navigation where you want to style only the top-level items, not nested items.
Both select siblings (elements with the same parent) that come after A in the DOM, but they differ in how many they select:

Adjacent sibling (+) — A + B: Selects only the single immediately following sibling that matches B. If there is any element between A and B, the match fails. h2 + p matches the first paragraph immediately after an h2, and only that one.

General sibling (~) — A ~ B: Selects all following siblings that match B, not just the first one. The matching elements don't need to be immediately adjacent — other elements can be between them. h2 ~ p matches all paragraphs that come after an h2 in the same parent, including those after a blockquote or other elements.

Common uses: + for label→input spacing, lead paragraphs, error messages after inputs. ~ for the checkbox/radio CSS toggle trick, styling all items after a divider.
Combinators themselves add zero specificity. The total specificity of a selector expression is simply the sum of the specificities of all the simple selectors it contains — the combinator between them doesn't contribute.

Examples:
— nav a → (0,0,1) + (0,0,1) = (0,0,2) — two element selectors
— ul > li → (0,0,2) — same calculation as ul li
— .nav > a → (0,1,0) + (0,0,1) = (0,1,1) — class + element
— .card > .body → (0,1,0) + (0,1,0) = (0,2,0) — two classes

This means that whether you use space, >, +, or ~, the specificity calculation is identical — just the sum of all simple selectors on both sides of the combinator.
The "lobotomised owl" selector — coined by Heydon Pickering — is a clever use of the adjacent sibling combinator to add spacing between all consecutive elements:

* + * { margin-top: 1rem; }

How it works: The * + * selector matches every element that has an immediately preceding sibling (i.e., every element that is NOT the first child). By giving it a top margin, you add spacing between all consecutive siblings — without adding margin to the very first item.

Why it's elegant: Normally you'd either add margin to every element (then reset on the first one) or use :not(:last-child). The owl selector is concise and expressive.

Scoped version (more practical):
.stack > * + * { margin-top: 1rem; }

This scopes it to direct children of .stack, avoiding unintended side effects. Pair with custom properties for variable spacing: .stack { --space: 1rem; } .stack > * + * { margin-top: var(--space); }
This technique uses a hidden checkbox and combinators to create interactive show/hide behaviour without JavaScript:

HTML structure:
<input type="checkbox" id="toggle">
<label for="toggle">Click me</label>
<div class="panel">Content</div>

CSS:
#toggle { display: none; } — hide the actual checkbox
#toggle:checked + label { background: blue; } — style label when checked (adjacent sibling +)
.panel { display: none; } — hide panel by default
#toggle:checked ~ .panel { display: block; } — show panel when checked (general sibling ~)

How it works: Clicking the label toggles the checkbox's checked state (because of for="toggle"). The :checked pseudo-class then activates the sibling selectors, styling the label and revealing the panel — all in pure CSS.
Multiple Choice Questions
Question 1 of 5
Given <ul><li><ul><li>inner</li></ul></li></ul>, which selector matches ONLY the outer <li>?
Question 2 of 5
How many <p> elements does h2 + p match per heading?
Question 3 of 5
What is the specificity of .nav > ul > li > a?
Question 4 of 5
In input:checked ~ .panel { display: block }, what must be true for .panel to appear?
Question 5 of 5
Which combinator would you use to add a top margin to every element in a stack except the first one?
Chapter Summary
  • Combinators connect two simple selectors and describe the required DOM relationship between them. They add zero specificity — only the simple selectors on each side count.
  • Descendant (space) — A B: B is anywhere inside A at any depth. Broadest match — use when scope is what matters, not depth.
  • Child (>) — A > B: B is a direct child of A, one level only. Use for multi-level lists/nav when you need to style only the top level.
  • Adjacent sibling (+) — A + B: B immediately follows A with the same parent. Selects exactly one element. Use for lead paragraphs, label→input spacing, inline error messages.
  • General sibling (~) — A ~ B: All B that follow A with the same parent. Use for the checkbox toggle trick, styling items after a divider.
  • All combinators are forward-only: they select children and following siblings, never parents or preceding siblings. Use :has() for parent-like selection.
  • Lobotomised owl: .stack > * + * { margin-top: 1rem } — adds spacing between consecutive siblings without affecting the first item.
  • CSS-only toggle: hidden checkbox + :checked ~ .panel + :checked + label — interactive show/hide with no JavaScript.
~8 min read
Intermediate level
Chapter 37 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel