Table of Contents

03. Combinators — Describing Relationships Between Elements

—Part of CSS Primer for the AI Era — Selectors

Combinators let us describe how elements relate to each other in the DOM.
They are the grammar that connects one selector to another.

If basic selectors answer:

Combinators answer:

This is where CSS becomes expressive —
and where AI often needs guidance
.


1. Why Combinators Matter

HTML is a tree.
Elements live:

Combinators let us describe these relationships
without adding extra classes.

They help us say things like:

This is the expressive heart of CSS.

AI‑steering cue:

“Use a combinator — 
I want to express the relationship, not add more classes.”

2. The Descendant Combinator (A B)

Pattern: A B
Meaning: “Select every B that lives anywhere inside A”.

This is the most common combinator.
It’s broad, flexible, and expresses meaning well.

Examples:

The descendant combinator (A B)
selects everything inside A,
at any depth
.

When to use it
Use the descendant combinator when:

When to avoid it
Avoid it when:

AI‑steering cue:

“Use a descendant selector — 
I want all matching elements inside this container.”

3. The Child Combinator (A > B)

Pattern: A > B
Meaning: “Select B only if it is a direct child of A.”

Example:

When to use it

When to avoid it

AI‑steering cue:

“Use a child selector — 
only the direct children should match.”

4. The Adjacent Sibling Combinator (A + B)

Before we begin section 4, it helps to know that CSS has two sibling combinators:

We’ll start with the adjacent sibling combinator.
The general sibling version follows next on section 5.


Pattern: A + B
Meaning: “Select B only if it immediately follows A.”

Example:

When to use it

When to avoid it:

AI‑steering cue:

“Use an adjacent sibling selector — 
style only the element that comes right after.”

5. The General Sibling Combinator (A ~ B)

Pattern: A ~ B
Meaning: “Select all B elements that share the same parent as A
and appear later in that parent’s list of children —
not just the next one”.

Example:

When to use it

When to avoid it

AI‑steering cue:

“Use a general sibling selector — 
match all following siblings, not just the first.”

Note: CSS selectors can only select siblings that come later in the parent’s list of children.
Selecting a previous sibling requires a different approach,
covered in the Limitations of CSS Selectors section.
Ultimately, however, CSS can style anything — if you give it a way to target it.


6. Why Combinators Feel Powerful

Combinators let us style based on:

Without adding extra classes.

They help us express intent clearly
— something AI responds to well.

AI‑steering cue:

“Use a combinator to express the relationship 
instead of adding more markup.”

7. The Mental Model

Think of combinators as prepositions:

They describe how elements relate in the DOM.

Once we understand these four relationships,
we can read almost any selector AI produces —
and request changes with clarity.


Tony de Araujo —New York | Lisbon