Table of Contents

05. State Selectors — Targeting Elements by Their Conditions

—Part of CSS Primer for the AI Era — Selectors

State selectors let us target elements
based on their current condition
— whether they are
hovered,
focused,
checked,
active,
visited,
empty,
or in some other temporary or structural state.

Where attribute selectors describe properties,
'state selectors' describe moments
.

They help us style elements
not just by what they are,
but by what they are doing right now.


1. What State Selectors Are

State selectors are pseudo‑classes* that
match elements when
they enter a particular condition.

These conditions may be:

State selectors let us adjust
our styling
as an element moves through
different conditions.
.

* NOTE:
A real CSS class is written directly in the HTML markup.
A pseudo‑class, however, is virtual
— the browser applies it automatically when an element enters a particular condition or state.
We write pseudo‑classes using a single colon (:)
followed by the name of the state we want to target.


2. User‑Interaction States

These states reflect how a user interacts with an element.

:hoverwhen the pointer is over the element

:focuswhen the element is focused

:activewhen the element is being pressed


3. Form‑Related States

Forms are full of meaningful conditions.

:checkedfor checkboxes and radio buttons

:disabledwhen the element cannot be interacted with

:required and :optional

:valid and :invalid


Links have their own lifecycle.

:linkunvisited links

:visitedlinks the user has already visited

a:visited ← example

Browsers protect privacy here, so styling options are intentionally limited.

Example of
Styling Unvisited Links with

a:link

HTML

<a href="https://example.com">Visit Example</a>
<a href="/local-page">Local Page</a>

CSS

a:link {
  color: #0645ad;   /* classic unvisited link color */
  text-decoration: underline;
}

What this does:

The rule applies only to links the visitor has not yet visited.

Once the link is visited, the browser switches to a:visited instead.

This gives us a clean way to differentiate unvisited and visited states without adding classes.


5. Structural States

Some states describe the element’s position or content.

:first-child and :last-child

:emptyelements with no children and no text

:not()negation


6. Why State Selectors Matter

State selectors let us respond to:

They help us build interfaces that feel alive, responsive, and intentional —
without adding extra classes or JavaScript.

They also give us a vocabulary for guiding AI:

“Style this element when it is in this condition.”

This is the heart of state‑based styling.


7. When We Use State Selectors

We reach for state selectors when:

Examples:

These selectors let us style based on behavior,
not just structure.


8. When We Avoid Them

We avoid state selectors when:

For example, “selected tab” is usually a class, not a pseudo‑class.

AI‑Steering Cue:

“Use a state selector — 
style the element when it enters this condition.”

This helps us guide AI
toward behavior‑based
styling rather than structural guessing
.


Tony de Araujo —New York | Lisbon