Combinators: Descendant, Child, Next-Sibling and Subsequent-Sibling Selectors
Selecting elements by their place in the document tree: parents, children, ancestors and siblings, the descendant, child (>), next-sibling (+) and subsequent-sibling (~) combinators, reading selectors right to left, why selectors ignore proximity, and when to prefer classes.
kvi5hnuCode Sprout team member9 min readOn this page
Simple selectors describe a single element. Combinators relate two elements by their position in the document: one inside another, or one after another. They let you style a link only when it's in the footer, the first paragraph after a heading, or every item after a selected one. CSS has four combinators: a space, >, + and ~.
The document tree
HTML elements form a tree. Every element except the root <html> has exactly one parent, the element directly containing it. The relationships you need are:
- Children are the elements directly inside an element, one level down. Descendants are all elements inside it, at any depth: children, grandchildren and so on.
- Ancestors are the parent, the parent's parent, and so on up to
<html>, which is the root element. - Siblings share the same parent. A sibling that comes later in the source is a following sibling.
Take this card for one book:
<article class="book-card">
<h2>Salt and Paper</h2>
<p class="author">by Lena Ortiz</p>
<p class="blurb">A novel about a family of papermakers on the coast.</p>
<footer>
<a href="/books/salt-and-paper">Details</a>
<span>€18.00</span>
</footer>
</article>
The <article> is the parent of h2, both paragraphs and footer. The link is a child of footer and a descendant, but not a child, of article. The two paragraphs are siblings of the h2, and following siblings of it.

Descendant combinator (space)
A space between two selectors means "the second, anywhere inside the first":
.book-card a { color: #8a3b12; } /* every link inside a book card, at any depth */
footer span { font-weight: 700; }
nav ul li a { text-decoration: none; }
The depth doesn't matter: .book-card a matches a link that's a child, a grandchild or nested ten levels deep.
Child combinator (>)
> means "the second, directly inside the first":
.book-card > p { margin: 0.25rem 0; } /* only paragraphs that are children of the card */
ul.menu > li { display: inline-block; } /* top-level menu items, not items of nested lists */
The child combinator is useful when the same kind of element is nested at several levels, such as a menu with submenus, and you want only the top level.
Whitespace around >, + and ~ is optional: ul>li and ul > li are the same.
Next-sibling combinator (+)
+ means "the second, immediately after the first, with the same parent". It's often called the adjacent-sibling combinator:
h2 + p { margin-top: 0; } /* the paragraph right after a heading */
.field + .field { margin-top: 1rem; } /* space between fields, but not before the first */
li + li { border-top: 1px solid #ddd; } /* a line between list items */
Only the element that's selected is styled, the one after the +. The rule li + li styles every list item except the first, because each of them is immediately preceded by another li. That makes + a classic way to put spacing or separators between items.
Text between the two elements doesn't break the adjacency, because text isn't an element. A comment doesn't either. Another element does: in <h2>...</h2><img><p>...</p>, the paragraph doesn't match h2 + p.
Subsequent-sibling combinator (~)
~ means "the second, anywhere after the first, with the same parent". It's also called the general sibling combinator:
h2 ~ p { color: #444; } /* every paragraph after the h2, among its siblings */
.selected ~ li { opacity: 0.6; } /* every item after the selected one */
Like +, it only looks forward and only among siblings. There's no combinator for "previous sibling" or "parent". For those, CSS has :has() (article 12): h2:has(+ p) selects an h2 that's followed by a paragraph, and li:has(~ .selected) selects items before the selected one.
.book-card { border: 1px solid #ccc; padding: 0.5rem 1rem; max-width: 26rem; font-family: system-ui, sans-serif; }
.book-card a { color: #8a3b12; } /* descendant */
.book-card > p { margin: 0.25rem 0; } /* child */
h2 + p { font-style: italic; color: #555; } /* next sibling */
h2 ~ p { padding-left: 0.5rem; border-left: 3px solid #f4c95d; } /* later siblings */
footer { display: flex; justify-content: space-between; margin-top: 0.75rem; }
Both paragraphs get the yellow bar (h2 ~ p), but only the author line is italic (h2 + p).

Note the nested Signings item in the third panel: li + li doesn't care which list the items belong to, only that one immediately follows another.
Combining combinators
Selectors can chain any number of combinators and compound selectors:
.site-header nav > ul > li + li { margin-left: 1rem; }
main article.featured h2 + p::first-line { font-weight: 700; }
Read selectors right to left
The element a selector styles is always the last compound in it, called the subject. Everything before it is a condition on the subject's surroundings. Reading from the right makes complex selectors easier to follow:
.site-header nav > ul > li + li: aliright after anotherli, both children of aulthat is a child of anavinside.site-header.
Browsers match the same way. They start from each element, test the last compound, and walk up the tree, or back through siblings, only if it matches. That's why a long selector costs little when its last part is rare, and why the last part matters most for performance.
Selectors don't know about proximity
A combinator only asks whether a relationship exists, not how close it is. This often surprises people:
.light a { color: #1d4ed8; }
.dark a { color: #f4c95d; }
.dark { background: #233; padding: 0.5rem; }
.light { background: #fff8e6; padding: 0.5rem; }
The link is inside both .dark and .light. Both rules match, they have the same specificity, and .dark a comes later, so the link is yellow, on a light background, even though .light is the closer ancestor. Nesting depth never breaks a tie in normal selectors.

Only the rule order changed between the first two panels. The third panel gets the closer color without depending on order, because inheritance does look at the nearest ancestor.
Fixes include giving the inner context higher specificity, using > where the structure allows it, setting colors through custom properties that each section defines, so the nearest one wins by inheritance (article 27), or using @scope, which does prefer the closer scope root (article 22).
Combinators or classes?
Combinators tie your CSS to the structure of the HTML. .book-card > footer > a breaks if someone wraps the link in a <div>. Classes on the elements you style are more robust: .book-card__link.
Combinators remain the best choice when the relationship is the point:
- Spacing between siblings, with
+(the "lobotomized owl" pattern* + *is a well-known example, covered in article 40). - Content inside an area you don't control, such as Markdown output or a CMS:
.article-body h2,.article-body img. - Top level versus nested, with
>, as in menus. - State shown by an earlier sibling, such as
input:checked + label.
Keep selectors short. Two or three compounds is plenty for most rules: long chains are hard to read, hard to override, and break when the markup changes.
What's current
- All four combinators have worked in every browser for many years.
:has()(Baseline since December 2023) adds the missing directions: parents, ancestors and previous siblings (article 12).- The column combinator
||, for selecting table cells by column, is specified but not supported in any browser.
Common questions
Is there a parent selector in CSS?
Yes, now: :has(). .book-card:has(img) selects cards that contain an image, and li:has(> a.active) selects a list item whose child link is active. Before :has(), CSS could only select downward and forward.
Can I select the previous sibling?
With :has(): h2:has(+ p) selects an h2 immediately followed by a p, and .item:has(~ .item.selected) selects every item before a selected one. The combinators alone only look forward.
What's the difference between div p and div > p?
div p matches any paragraph inside a div, at any depth. div > p matches only paragraphs whose parent is a div. In <div><section><p>...</p></section></div>, only the first selector matches the paragraph.
Why does li + li skip the first item?
+ selects an element that's immediately preceded by a matching sibling. The first li has no li before it, so it doesn't match. That's exactly why the pattern is used for borders and spacing between items.
Are long descendant selectors slow? Rarely enough to matter. Browsers optimize selector matching heavily, starting from the rightmost part. Long selectors cause more trouble with specificity and maintenance than with speed.
Summary
- Elements form a tree of parents, children, ancestors, descendants and siblings.
- A space selects descendants at any depth, and
>selects only children. +selects the next sibling, and~selects every later sibling, both with the same parent. Text between siblings doesn't count.- The last compound in a selector is the one styled. Read selectors right to left.
- Combinators ignore proximity: a closer ancestor doesn't win over a farther one.
:has()provides the missing parent and previous-sibling selection.- Prefer classes for components, and use combinators when the relationship itself matters.
Practice questions
Given this markup, which elements does nav > ul > li select, and which does nav li select?
<nav>
<ul>
<li>Books
<ul><li>Fiction</li><li>Poetry</li></ul>
</li>
<li>Events</li>
</ul>
</nav>
Answer
nav > ul > li selects only "Books" and "Events", the items of the list that's a child of nav. nav li selects all four items, including "Fiction" and "Poetry" in the nested list, because they're descendants of nav too.
Write a selector that adds a top border to every event in a list except the first, without using classes or :first-child.
Answer
li + li { border-top: 1px solid #ddd; }, or .events > li + li to limit it to one list. Each item that's immediately preceded by another item gets the border, and the first has none before it.
Does h2 + p match the paragraph in this markup? Why?
<h2>Opening hours</h2>
Mon–Sat
<p>Closed on Sundays.</p>
Answer
Yes. The text "Mon–Sat" isn't an element, so the paragraph is still the element immediately following the h2.
A link sits inside .light, which sits inside .dark, and the CSS is below. What color is the link, and what's one fix that makes the nearest section win?
.light a { color: blue; }
.dark a { color: gold; }
Answer
Gold. Both rules match with equal specificity, so the later one wins, and how close each ancestor is doesn't matter. One fix is to have each section set a custom property, such as .light { --link: blue } .dark { --link: gold } a { color: var(--link) }: the link inherits the value from its nearest ancestor that sets it. @scope with proximity is another (article 22).
Previous: Attribute Selectors: Presence, Exact, Partial Matches and Case Sensitivity

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.