—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.
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.
These states reflect how a user interacts with an element.
:hover — when the pointer is over the element
button:hover
:focus — when the element is focused
input:focus
:active — when the element is being pressed
button:active Forms are full of meaningful conditions.
:checked — for checkboxes and radio buttons
input[type=“checkbox”]:checked
:disabled — when the element cannot be interacted with
button:disabled
:required and :optional
input:required
:valid and :invalid
input:invalid Links have their own lifecycle.
:link — unvisited links
a:link ← example
:visited — links 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.
Some states describe the element’s position or content.
:first-child and :last-child
li:first-child ← example.
:empty — elements with no children and no text
div:empty ← example
:not() — negation
button:not([disabled]) ← example. 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.
We reach for state selectors when:
Examples:
:hover for interaction:focus for accessibility:checked for toggles:invalid for validation:empty for layout cleanup
These selectors let us style based on behavior,
not just structure.
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