Table of Contents

11. Selectors — A Summary of What They Can and Cannot Do

—Part of CSS Primer for the AI Era — Selectors

CSS selectors are powerful, but they are not universal.
They can move down the DOM tree and forward through siblings — but they cannot move up, backward, or sideways in certain ways.

Understanding these boundaries helps us:

This page gives us a clear sense of what CSS selectors cannot do —
and why those limitations exist.

And beneath all of it sits one quiet truth:

CSS can style anything —
but we must give it a way to target it
.


1. CSS Selectors Cannot Select a Parent Element

Selectors can target:

But selectors cannot target a parent.

We cannot write:

p:parent
.child ^ .parent

CSS has no mechanism to “climb upward.”

Why this matters

If we need to style a parent based on something inside it,
we must:

AI‑steering cue:

“CSS selectors can’t select parents — 
add a class to the parent instead.”

2. CSS Selectors Cannot Select a Previous Sibling

Selectors can move forward through siblings:

A + B   → next sibling  
A ~ B   → all later siblings

But they cannot move backward.

CSS selectors can only match siblings that appear later in the parent’s list of children — never earlier ones.

Example of what CSS cannot do:

p ~ h2   ← impossible

Why this matters

If we need to style something based on what comes before it,
we must:

AI‑steering cue:

“CSS selectors can only select later siblings — 
not earlier ones.”

3. CSS Selectors Cannot Match Text Content

CSS can match:

…but it cannot match text.

This does not exist:

p:contains("Warning")

Why this matters

If we need to style based on content, we must:

AI‑steering cue:

“CSS selectors can’t match text — 
use a class to express meaning.”

4. CSS Selectors Cannot Select Based on Computed Values

Selectors operate on:

…but not on:

This does not exist:

div:width-greater-than(200px)

Why this matters

If we need layout‑dependent styling, we must:

AI‑steering cue:

“Selectors can’t see computed values — 
use a query instead.”

5. CSS Selectors Cannot Select Arbitrary “Next” or “Previous” Elements

CSS understands only:

A + B   → the next sibling  
A ~ B   → all later siblings

It cannot select:

the second next sibling

the third next sibling

a sibling that appears earlier

a sibling in a different parent Why this matters

If we need to target a specific element in a sequence,
we must:

AI‑steering cue:

“CSS can only move forward through siblings — 
not backward or arbitrarily.”

6. Why These Limitations Exist

CSS selectors were designed to be:

They intentionally avoid patterns that require:

This keeps CSS selectors simple, stable, and evergreen.


7. The Mental Model

CSS selectors can:

CSS selectors cannot:

Once we internalize this,
we stop fighting CSS
and start working with it
.


Tony de Araujo —New York | Lisbon