—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.
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.”
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:
nav a → all links inside the nav article p → all paragraphs inside the article footer ul → all lists inside the footer
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.”
Pattern: A > B
Meaning: “Select B only if it is a direct child of A.”
Example:
ul > li → only the top‑level list item(s).When to use it
When to avoid it
AI‑steering cue:
“Use a child selector — only the direct children should match.”
Before we begin section 4, it helps to know that CSS has two sibling combinators:
A + B) — matches the next siblingA ~ B) — matches all later siblings
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:
h2 + p → the first paragraph right after a heading.When to use it
When to avoid it:
AI‑steering cue:
“Use an adjacent sibling selector — style only the element that comes right after.”
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:
h2 ~ p → all paragraphs that follow the heading within the same parentWhen 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.
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.”
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