Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 139 ยท Selectors

Attribute Selectors

Learn every CSS attribute selector, from [attr] and exact matches to prefix, suffix and substring matching, word and language matches, and the case-insensitive i flag.

HTML elements carry a lot of information in their attributes: a link's href, an input's type, an image's alt, a button's disabled state, your own data-* values. Attribute selectors let you style elements based on that information, without adding a single class.

They are written in square brackets, and there are seven ways to compare a value, plus a flag for case-insensitive matching. This lesson walks through each one with practical examples: styling links by where they point, form fields by type, and components by their data-* state.

Presence: [attr]

The simplest form matches any element that has the attribute, whatever its value (even an empty one).

CSS
[title] {
  cursor: help;
}

input[required] {
  border-left: 4px solid #f59e0b;
}

The first rule matches every element with a title attribute. The second combines a type selector with an attribute selector, so it only matches <input> elements that have required. Boolean attributes such as required, disabled and hidden are a natural fit for this form, since their presence is all that matters.

Exact value: [attr="value"]

Add = and a value to require an exact match of the whole attribute value.

CSS
input[type="email"] { โ€ฆ }
a[target="_blank"] { โ€ฆ }

Quotes around the value are optional when it is a simple word ([type=email] works), but they are required when the value contains spaces or special characters. Always quoting is the easy, consistent habit.

Styling inputs by type

Attribute selectors read the attribute in the HTML, not the element's live state. input[value="3"] keeps matching the quantity field above even after the user types a different number, because typing changes the value property, not the value attribute. For live states, use pseudo-classes like :checked and :invalid.

Matching parts of a value

Three operators match part of a value. They borrow their symbols from regular expressions, which makes them easy to remember:

Selector Matches when the valueโ€ฆ Memory aid
[attr^="x"] starts with x ^ marks the start in regex
[attr$="x"] ends with x $ marks the end in regex
[attr*="x"] contains x anywhere * as in "anything around it"

These shine with URLs. You can flag links by protocol, by file type or by domain:

Styling links by where they go

The ::after part adds a little arrow after the link text; you will learn how in Pseudo-elements. Notice how the first link matches two rules: it starts with https:// and contains example.

Be careful with *=. It matches any substring, so [class*="btn"] matches btn, btn-primary and also submit-button-wrapper. Broad substring matches tend to catch things you didn't intend.

Word and language matches: ~= and |=

Two more operators handle specific value formats.

[attr~="word"] treats the value as a space-separated list of words and matches when one of them equals word exactly. That is how class matching works internally: .card behaves like [class~="card"]. It's useful for other list-like attributes, such as rel:

CSS
a[rel~="nofollow"] { โ€ฆ }   /* matches rel="nofollow noopener" */

[attr|="value"] matches when the value is exactly value, or starts with value followed by a hyphen. It was designed for language codes:

CSS
[lang|="en"] { quotes: "โ€œ" "โ€"; }   /* en, en-US, en-GBโ€ฆ */

It matches lang="en" and lang="en-GB", but not lang="english".

Word lists and language codes

For styling by language, the :lang() pseudo-class is often better than [lang|=โ€ฆ]. It also matches elements that inherit a language from an ancestor, while the attribute selector only sees elements that carry lang themselves.

Case sensitivity and the i flag

Whether attribute values are compared case-sensitively depends on the attribute. In HTML, values of a few attributes, such as type, are compared case-insensitively by the browser, while most others, such as href, class and data-* attributes, are case-sensitive.

When you want case to be ignored, add an i before the closing bracket:

CSS
a[href$=".pdf" i] { โ€ฆ }   /* .pdf, .PDF, .Pdfโ€ฆ */

There is also an s flag that forces case-sensitive matching, but it has much weaker browser support and is rarely needed.

Ignoring case with the i flag

Styling by data attributes

Attribute selectors pair naturally with data-* attributes. A component can expose its state in HTML, and CSS reacts to it. JavaScript only has to flip one attribute.

HTML
<div class="alert" data-variant="warning">โ€ฆ</div>
CSS
.alert[data-variant="warning"] { background: #fef3c7; }
.alert[data-variant="error"]   { background: #fee2e2; }

The same idea works with ARIA attributes, which are already there for accessibility. Styling [aria-expanded="true"] or [aria-current="page"] means the visual state can never drift out of sync with what screen readers announce:

CSS
.nav a[aria-current="page"] {
  font-weight: 700;
  text-decoration: underline;
}

Common mistakes

A few slips account for most attribute selectors that "don't work":

  • A stray space. input [type="email"] (with a space) looks for an element with that attribute inside an input. Inputs have no children, so it never matches. Write input[type="email"].
  • Expecting = to match part of a value. [class="card"] only matches when the whole attribute is exactly card. An element with class="card featured" is skipped. Use .card or [class~="card"] instead.
  • Forgetting case. [href$=".jpg"] misses PHOTO.JPG. Add the i flag when file names or user-entered values could vary.
  • Matching too broadly. [href*="shop"] also catches /workshop and /shopping-tips. Anchor the match with ^= or $= when you can.

Specificity and performance

Every attribute selector counts the same as a class in specificity, whatever operator it uses. [type="email"] and .email weigh the same.

You may read that attribute selectors are slow. In modern browsers the difference is negligible for real pages; choose them when they express your intent clearly. The bigger concern is readability: a stylesheet full of [class*="col-"] hacks is harder to follow than one built on clear classes and meaningful attributes.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which selector matches links whose href ends with .zip?

    $= means ends with. ^= is starts with, *= is contains anywhere (which would also match, but less precisely), and ~= matches a whole space-separated word.
  2. 2. Which element does [lang|="fr"] NOT match?

    |= matches the exact value or the value followed by a hyphen. french is neither.
  3. 3. What does the i in [href$=".pdf" i] do?

    The i flag makes the comparison ignore letter case, so .PDF and .pdf both match.
  4. 4. An input has value="5" in its HTML. The user changes it to 9. Does input[value="5"] still match?

    Typing changes the value property, not the attribute in the markup, so the attribute selector keeps matching the original value.
  5. 5. What is the specificity of [data-state="open"]?

    All attribute selectors count in the same column as classes and pseudo-classes.

Summary

  • [attr] matches when the attribute is present; [attr="v"] requires an exact value.
  • ^= starts with, $= ends with, *= contains: great for URLs and file types.
  • ~= matches one word in a space-separated list; |= matches a value or its hyphenated prefix, as in language codes.
  • Add i before ] to match regardless of case.
  • Attribute selectors see the markup, not live state; use pseudo-classes for that.
  • They pair well with data-* and ARIA attributes, and weigh the same as a class.

Next: Pseudo-classes โ€” styling elements by their state, like hover, focus and validity.