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:

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:

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:

This is the most common pattern in forms.


4. Matching Values That Start With Something

[attribute^=“value”]

The caret (^) means starts with.

Examples:


5. Matching Values That End With Something

[attribute$=“value”]

The dollar sign ($) means ends with.

Example:

Great for targeting file types.


6. Matching Values That Contain Something

[attribute*=“value”]

The asterisk (*) means “contains”.

Example:

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:

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:

This is mostly used for language and locale.


9. When We Use Attribute Selectors

We reach for attribute selectors when:

Examples:

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:

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.


Tony de Araujo —New York | Lisbon