Combinators

A combinator joins two compound selectors and states how the elements they match must be related in the document tree: ancestor and descendant, parent and child, or earlier and later sibling. The selector always matches the element on the right. The pieces being joined are under Basic selectors; to select an element by what comes after or inside it, use :has().

Tree combinators

Descendant combinator (space)

Whitespace between two selectors matches an element that is anywhere inside an element matching the left-hand selector, at any depth.

SyntaxA B
SpecificityAdds nothing; the sum of A and B
SupportChrome 1 · Firefox 1 · Safari 1
Specselectors-4
nav a { text-decoration: none; }
Every link inside a nav, however deeply nested.

>

The child combinator matches an element whose parent matches the left-hand selector. Unlike the descendant combinator, it does not reach grandchildren.

SyntaxA > B
SpecificityAdds nothing; the sum of A and B
SupportChrome 1 · Firefox 1 · Safari 1
Specselectors-4
ul > li { margin-block: 0.25rem; }
Only the list's own items, not items of nested lists.

Sibling combinators

+

The next-sibling combinator matches an element that comes immediately after an element matching the left-hand selector, with the same parent. Text and comments between them are ignored.

SyntaxA + B
SpecificityAdds nothing; the sum of A and B
SupportChrome 1 · Firefox 1 · Safari 1
Specselectors-4
h2 + p { margin-block-start: 0; }
The first paragraph straight after each h2.

~

The subsequent-sibling combinator matches an element that comes anywhere after an element matching the left-hand selector, with the same parent. It only looks forward: earlier siblings are not matched.

SyntaxA ~ B
SpecificityAdds nothing; the sum of A and B
SupportChrome 1 · Firefox 1 · Safari 3
Specselectors-4
input:checked ~ .panel { display: block; }
Show any later sibling panel once the input is checked.