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.
A B
(space)A > BA + BA ~ BCombinators 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:
:has() pseudo-class (Chapter 34) is the modern workaround for "parent-like"
selection.
Descendant Combinator — A B (space)
nav a
<nav><a>
direct child<nav><ul><li><a> nested<a> outside nav
/* 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
A > B- Item 1 — direct child ✓
- Item 2 — direct child ✓
- Nested A — NOT direct child ✗
- Nested B — NOT direct child ✗
- Item 3 — direct child ✓
ul > li<li>
children of <ul><li> inside
nested <ul>/* 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
A + BSection 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.
h2 + p
/* 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
A ~ Bh2 ~ p matches all purple items (any p that comes after the h2, same parent)
h2 ~ p
/* 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:
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
/* 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 { }
/* ── 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 |
> look down (parent → children). + and ~ look across (older sibling →
younger siblings). None of them look up (child → parent) or backward (younger → older sibling).
Try It Yourself
All four combinators in one editor. Change the HTML structure and watch the selectors respond:
Quick Check — Chapter 37
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.
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.
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
classesThis 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.
* + * { 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); }
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.
<ul><li><ul><li>inner</li></ul></li></ul>,
which selector matches ONLY the outer <li>?<p> elements does h2 + p match per heading?.nav > ul > li > a?input:checked ~ .panel { display: block },
what must be true for .panel to appear?- 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.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.