Table of Contents
04. Attribute Selectors — Targeting Elements by Their Properties
—Part of CSS Primer for the AI Era — Selectors
Attribute selectors let us target elements based on the attributes they carry
— things like href, type, alt, data-*, and more.
Where combinators help us describe relationships,
attribute selectors help us describe meaning.
They give us a way to style elements by what they are,
not just where they sit in the DOM.
1. What Attribute Selectors Are
Attribute selectors match elements based on:
- the presence of an attribute
- the exact value of an attribute
- part of a value
- patterns inside a value
This gives us a second axis of control:
not structure, but properties.
2. The Basic Pattern
[attribute]
This matches any element that has that attribute.
Example:
[disabled]→ any element with a disabled attribute,
regardless of value.
This is the simplest form — “has this property.”
3. Matching Exact Values
[attribute=“value”]
This matches elements whose attribute value is exactly the string we specify.
Example:
[type=“email”]→ any <input> whose type is exactly email.
This is the most common pattern in forms.
4. Matching Values That Start With Something
[attribute^=“value”]
The caret (^) means starts with.
Examples:
[src^=“icon-”]→ any element whose src attribute starts withicon-
This is clean, safe, and common in UI icon systems.[data-state^=“open”]→ any element whose data-state begins withopen
(e.g.,open,open-soft,open-true).
This is excellent for modern component‑driven markup.[class^=“btn-”]→ any element whose class list begins withbtn-.
5. Matching Values That End With Something
[attribute$=“value”]
The dollar sign ($) means ends with.
Example:
[src$=“.svg”]→ images or icons that end with.svg.
Great for targeting file types.
6. Matching Values That Contain Something
[attribute*=“value”]
The asterisk (*) means “contains”.
Example:
[class*=“icon-”]→ any element whose class list contains something likeicon-home,icon-user, etc.
This is powerful, but we use it carefully — it can match more than we expect.
7. Matching Space‑Separated Values
[attribute~=“value”]
The tilde (~) means “contains this word in a space‑separated list”.
Example:
[rel~=“noopener”]→ matches<a rel=“noopener noreferrer”>.
This is perfect for attributes that behave like class lists.
8. Matching Hyphen‑Separated Values
[attribute|=“value”]
The pipe (|) means value or value‑something.
Example:
[lang|=“en”]→ matchesen,en-US,en-GB, etc.
This is mostly used for language and locale.
9. When We Use Attribute Selectors
We reach for attribute selectors when:
- the attribute expresses meaning
- the attribute expresses state
- the attribute expresses type
- the attribute expresses intent
- classes are not available or not meaningful
- AI‑generated markup uses predictable attributes
Examples:
[aria-expanded=“true”][data-state=“open”][role=“button”][type=“checkbox”]
These selectors let us style based on what the element is doing,
not just what it’s called.
10. When We Avoid Attribute Selectors
We avoid attribute selectors when:
- we need performance on very large documents
- the attribute value is long or unpredictable
- the pattern might match too broadly
- a class would express the meaning more clearly
Classes remain the most intentional way
to express purpose.
AI‑Steering Cue:
“Use an attribute selector — target elements by their properties, not their position.”
This helps us guide AI toward meaning‑based styling
rather than structural guessing.
- Suggested Next Reading: State Selectors — Targeting Elements by Their Conditions
Tony de Araujo —New York | Lisbon
