Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Selectors

Attribute selectors

Reference for CSS attribute selectors: presence, exact match, prefix, suffix, substring, word and language matches, plus the i and s case flags.

Attribute selectors match elements by the attributes they carry, written in square brackets. You can test only that an attribute exists, or compare its value in several ways: exact, starts with, ends with, contains, contains a word, or equals a language prefix.

They have the same specificity as a class (0,1,0), and they are the right tool when you cannot or should not add a class, such as styling links to PDF files or form controls by type.

CSS
a[href$=".pdf"] {
  color: #c92a2a;
}

Try it

Matching attribute values

Syntax and arguments

CSS
[attr]            /* attribute exists */
[attr="value"]    /* equals exactly */
[attr~="value"]   /* whitespace-separated list contains the word */
[attr|="value"]   /* equals value, or starts with value followed by - */
[attr^="value"]   /* starts with */
[attr$="value"]   /* ends with */
[attr*="value"]   /* contains the substring */
[attr="value" i]  /* ASCII case-insensitive */
[attr="value" s]  /* case-sensitive */
Form Description
[attr] The element has the attribute, whatever its value (even empty).
[attr=v] The value is exactly v. Quotes are optional for simple identifiers; use them for anything else.
[attr~=v] The value is a space-separated list and one item equals v. [class~="a"] is like .a.
[attr|=v] The value is v or starts with v-. Designed for lang="en" and lang="en-US".
[attr^=v] The value starts with v.
[attr$=v] The value ends with v.
[attr*=v] The value contains v anywhere.
i flag Compare ASCII letters without regard to case.
s flag Force case-sensitive comparison.

Substring matchers (^=, $=, *=) never match when v is an empty string. Specificity is 0,1,0. The value is compared with the attribute text, not the property of the DOM element, so [value="x"] on an input does not follow what the user types.

Usage notes

  • Case rules. Attribute names in HTML are case-insensitive. Values are case-sensitive for most attributes, but HTML defines a list of attributes (such as type, rel, target, lang) whose values compare case-insensitively in HTML documents. The i and s flags override the default.
  • Typed inputs. input[type="checkbox"] is a standard way to style form controls without extra classes.
  • Beware *=. [class*="col"] also matches collapse and color-red. Prefer ~= or a real class selector.
  • Link patterns. a[href^="http"] and a[href$=".pdf"] are common, but they test the written URL, not the resolved one, and ignore query strings.
  • Data attributes. [data-state="open"] is a clean hook for state set by JavaScript or Alpine.
  • Not for dynamic DOM state. Typed text and checked state are properties, not attributes; use pseudo-classes such as :checked and :placeholder-shown instead.
  • Dashes and specials. Values with spaces or special characters need quotes.

Browser support

Supported in every browser. The i flag is universal; the s flag is newer but available in all current browsers.