← Back to HTML & CSS, page 10

html-css-0184 · Easy fresher

What are attribute selectors, and when do you use the forms with ^=, $=, *=, ~= and |=?

Attribute selectors match by presence or by value: exact, starts with (^=), ends with ($=), contains (*=), word in a list (~=) and language prefix (|=). They have class specificity. An i flag ignores case, and the matching works on the attribute text, so relative links differ from absolute ones.

Full answer

An attribute selector matches elements by an attribute or by its value. It has the same specificity as a class, which is (0,1,0).

The forms:

  • [disabled] matches when the attribute is present, whatever the value.
  • [type="email"] matches when the value is exactly email.
  • [href^="https"] matches when the value starts with the text. Use it for secure links, or for links that start with /docs.
  • [href$=".pdf"] matches when the value ends with the text. Use it for file types.
  • [href*="@"] matches when the value contains the text anywhere.
  • [class~="btn-lg"] matches when the value is a space-separated list that contains the word. This is how a class selector works inside.
  • [lang|="en"] matches en or any value that starts with en-, such as en-GB.
  • Add a space and i before the closing bracket, for example [href$=".pdf" i], to ignore case. The attribute name is not case-sensitive in HTML, and values of some attributes, such as type, are already case-insensitive.

Good uses: styling form inputs by type, marking external or file links, styling ARIA states such as [aria-expanded="true"], and styling data attributes. Be careful with these:

  • Quotes are needed when the value has special characters or spaces. Using them every time is a good habit.
  • *= can match more than you expect. [class*="btn"] also matches my-btn-group and btnx.
  • A selector on href sees the attribute text, not the full resolved address. A relative link /files/a.pdf does not start with https.
  • Attribute selectors are not faster or slower in a way that matters on normal pages, but a very broad one, such as [class], can match many elements.

The secure link is green, the PDF link is red, and the mail link is purple. The upper-case file is italic, because the rule with the i flag matches it, but it stays the default blue, because the first PDF rule is case-sensitive and does not match. The disabled input is half transparent, the British English paragraph is teal, and the big button has a 24 pixel font.

Pick the simplest selector that is clear. A class is often better than a long attribute selector.

html
<style>
  a[href^="https"] { color: green; }
  a[href$=".pdf"] { color: red; }
  a[href$=".pdf" i] { font-style: italic; }
  a[href*="@"] { color: purple; }
  input[disabled] { opacity: 0.5; }
  [lang|="en"] { color: teal; }
  [class~="btn-lg"] { font-size: 24px; }
</style>
<a id="a1" href="https://example.org">Secure</a>
<a id="a2" href="/files/a.pdf">PDF</a>
<a id="a3" href="mailto:me@example.com">Email</a>
<a id="a4" href="/files/B.PDF">Upper case file</a>
<input id="i1" type="text" disabled>
<p id="p1" lang="en-GB">Hello</p>
<div id="d1" class="btn btn-lg">Big</div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • [attr] checks presence; [attr="v"] checks the exact value.
  • ^=, $= and *= match start, end and contains.
  • ~= matches a word in a list; |= matches a language prefix.
  • Specificity is the same as a class.
  • Add i to ignore case.

Common mistakes

  • Using *= when ^= or $= is exact enough, and matching too much.
  • Expecting [href^="https"] to match a relative link.
  • Forgetting the i flag for upper-case file extensions.

Follow-ups

  • What does [lang|="en"] match?
  • How do you match a value case-insensitively?

Tips

  • Name the operators in a table-like list with one use each.
  • Say that the specificity equals a class.