Web Analytics

CSS Combinators

Intermediate ~10 min read

What are Combinators?

Combinators define relationships between selectors. They let you target elements based on their position relative to other elements in the DOM tree.

Descendant Selector (Space)

The space combinator selects all descendants (children, grandchildren, etc.) of an element.

HTML
CSS
JS

Child Selector (>)

The > combinator selects only direct children, not deeper descendants.

HTML
CSS
JS

Adjacent Sibling Selector (+)

The + combinator selects the element immediately following another element (same parent).

HTML
CSS
JS

General Sibling Selector (~)

The ~ combinator selects all siblings following an element (not just the immediate one).

HTML
CSS
JS

Combining Multiple Combinators

Chain combinators together for precise targeting. Read from right to left!

HTML
CSS
JS

Practical Example: Form Layout

Use combinators to style form elements based on their relationships.

HTML
CSS
JS
Pro Tip: Use child selector > instead of descendant selector (space) when possible for better performance and more predictable styling!

Summary

  • A B (space) - Descendant: all B inside A
  • A > B - Child: direct children B of A
  • A + B - Adjacent sibling: B immediately after A
  • A ~ B - General sibling: all B siblings after A
  • Combinators can be chained for precise targeting
  • Read complex selectors from right to left

Quick Quiz

Which combinator selects only direct children?

A
Space
B
>
C
+
D
~