TExcelTAD
Home › CSS › CSS Attribute Selectors

CSS Attribute Selectors

Attribute selectors match elements by their HTML attributes and values — inputs of a certain type, links pointing to PDFs, elements with a data-* attribute. They're written in square brackets: [type="email"].

The full list

Selector Matches elements whose attribute… Example
[attr] exists (any value) [disabled]
[attr="v"] equals exactly v [type="checkbox"]
[attr~="v"] is a space-separated list containing the word v [class~="note"]
`[attr ="v"]` equals v or starts with v-
[attr^="v"] starts with v [href^="https"]
[attr$="v"] ends with v [href$=".pdf"]
[attr*="v"] contains v anywhere [href*="example"]
[attr="v" i] equals v, case-insensitive [href$=".PDF" i]

Presence and exact match

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  input { display: block; margin: 6px 0; padding: 8px; font-size: 15px; border: 2px solid #ccc; border-radius: 6px; width: 220px; }
  input[type="email"]    { border-color: #1e88e5; }
  input[type="password"] { border-color: #8e24aa; }
  input[required]        { background: #fffde7; }
  input[disabled]        { background: #eee; color: #999; border-style: dashed; }
</style>
</head>
<body>
  <input type="text" placeholder="text">
  <input type="email" placeholder="email (blue)" required>
  <input type="password" placeholder="password (purple)" required>
  <input type="text" placeholder="disabled" disabled>
</body>
</html>

Tip: Quotes around the value are optional for simple words ([type=email]) but required when the value contains spaces or special characters. Using them always is a good habit.

Matching parts of a value

The substring selectors ^=, $= and *= are perfect for links: mark external links, downloads or email links automatically.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; line-height: 2; }
  a { color: #263238; }
  a[href^="http"]::after   { content: " ↗"; color: #1e88e5; }
  a[href$=".pdf" i]::before { content: "📄 "; }
  a[href^="mailto:"]::before { content: "✉️ "; }
  a[href*="shop"] { background: #fff3e0; padding: 2px 6px; border-radius: 4px; }
</style>
</head>
<body>
  <a href="https://site.test/docs">External docs</a><br>
  <a href="/files/report.PDF">Annual report</a><br>
  <a href="mailto:hi@site.test">Email us</a><br>
  <a href="/shop/shoes">Shop shoes</a>
</body>
</html>

Word and language matching

~= treats the attribute as a list of space-separated words; |= is designed for language codes.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  [data-tags~="urgent"] { border-left: 6px solid #e53935; padding-left: 8px; }
  [data-tags*="ur"] { font-style: italic; }
  [lang|="fr"] { color: #1565c0; }
  [lang|="fr"]::before { content: "🇫🇷 "; }
</style>
</head>
<body>
  <p data-tags="work urgent">tags="work urgent" — has the word urgent (red bar) and contains "ur" (italic)</p>
  <p data-tags="leisure">tags="leisure" — contains "ur" (italic) but not the word urgent</p>
  <p lang="fr">Bonjour (lang="fr")</p>
  <p lang="fr-CA">Allô (lang="fr-CA")</p>
  <p lang="en">Hello (lang="en")</p>
</body>
</html>

Styling with data attributes

Custom data-* attributes plus attribute selectors make a clean way to express state or variants in HTML — no extra classes needed.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; gap: 8px; flex-wrap: wrap; }
  .badge { padding: 6px 12px; border-radius: 999px; font-size: 14px; font-weight: 600; }
  .badge[data-status="ok"]      { background: #c8e6c9; color: #1b5e20; }
  .badge[data-status="warning"] { background: #fff9c4; color: #f57f17; }
  .badge[data-status="error"]   { background: #ffcdd2; color: #b71c1c; }
  .badge[data-status]::before   { content: "● "; }
</style>
</head>
<body>
  <span class="badge" data-status="ok">Online</span>
  <span class="badge" data-status="warning">Degraded</span>
  <span class="badge" data-status="error">Offline</span>
  <span class="badge">No status</span>
</body>
</html>

An attribute selector has the same specificity as a class: [type="text"] and .text weigh exactly the same.

Test yourself

Which selector matches links whose href ends with .zip?

  1. a[href^=".zip"]
  2. a[href$=".zip"]
  3. a[href*=".zip"]
  4. a[href~=".zip"]

What does [lang|="en"] match?

  1. Only lang="en"
  2. lang="en" or values starting with "en-"
  3. Any lang containing en
  4. lang attributes that are empty

How does the specificity of [type="text"] compare to .text?

  1. Lower
  2. Equal
  3. Higher
  4. It has no specificity