CSS [attribute~=value] Selector

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Attribute Selectors

What You’ll Learn

The [attribute~=value] selector matches elements when a given value appears as a whole word in a space-separated attribute list. It is perfect for targeting specific classes or rel tokens.

01

Word match

Whole token only.

02

~= operator

Tilde-equals.

03

class

Multi-class.

04

rel

Link tokens.

05

vs *=

Word vs substring.

06

Spaces

Separator rule.

Introduction

The [attribute~=value] selector in CSS targets elements whose attribute contains a specific value as one of the space-separated words in that attribute.

This is especially useful for the class attribute, where multiple class names are listed with spaces, and for rel attributes that list link relationship tokens.

Definition and Usage

Write the attribute in brackets, then ~= and the word to find: [class~="lead"]. It matches class="intro lead" because lead is a complete word in the list — but not class="leads".

💡
Beginner Tip

~= means “contains this word in a space-separated list.” For substring matching anywhere in the value, use [attr*=value]. For hyphen-separated values like lang="en-US", use [attr|=value].

📝 Syntax

The signature of the space-separated attribute selector is:

syntax.css
[attribute~="value"] {
  /* CSS properties */
}

Basic Example

class-lead.css
[class~="lead"] {
  background-color: #dbeafe;
  font-weight: 600;
}

a[rel~="noopener"] {
  color: #16a34a;
}

Syntax Rules

  • ~= matches when the value is a complete word separated by spaces.
  • The match is case-sensitive.
  • Works with any space-separated attribute, not only class.
  • Does not match partial words: [class~="le"] does not match class="lead".
  • Prefer the class selector .lead for simple class matching when possible.

Compare Attribute Operators

SelectorMatches when
[attr~="x"]x is a space-separated word in the value
[attr*="x"]Value contains x anywhere (substring)
[attr|="x"]Value is x or starts with x- (hyphen-separated)
.xElement has class x (same as [class~="x"] for class)

⚡ Quick Reference

QuestionAnswer
Operator~= (space-separated word match)
Match typeWhole word in a space-delimited list
Case sensitive?Yes
Common pattern[class~="lead"]
vs contains*= matches substrings; ~= requires a full word
Browser supportAll modern browsers
[class~="lead"] [class~="highlight"] a[rel~="noopener"] [class~="btn"]

When to Use [attribute~=value]

  • Multi-class elements — Target elements that include a specific class token among others.
  • rel tokens — Style links with rel="noopener" or rel="noreferrer" in a list.
  • Legacy markup — Match class names when you cannot add a dedicated class selector.
  • Attribute-driven styling — Any custom attribute that stores space-separated keywords.
  • Specificity control — Combine with other selectors when you need attribute-based matching.

👀 Live Preview

Paragraphs whose class list includes the word lead get a blue background:

Welcome to the site!

This paragraph has the lead class.

Another lead paragraph.

Highlighted only — no lead class.

Examples Gallery

Practice space-separated matching with class lists, rel attributes, highlight modifiers, and operator comparisons.

📜 Class Lists

The most common use case for space-separated attribute matching.

Example 1 — Paragraphs with the lead class

Style any element whose class attribute includes lead as a separate word.

class-lead.css
[class~="lead"] {
  background-color: #dbeafe;
  font-weight: 600;
  padding: 0.5rem 0.65rem;
  border-radius: 6px;
}
Try It Yourself

How It Works

class="intro lead" and class="lead" both contain lead as a whole word. class="intro" does not match.

Example 2 — Highlight modifier class

Apply emphasis styles when highlight appears anywhere in the class list.

class-highlight.css
[class~="highlight"] {
  background: #fef9c3;
  border-left: 4px solid #eab308;
  padding: 0.5rem 0.65rem;
}
Try It Yourself

How It Works

The selector finds highlight as its own token. A class name like highlighter would not match because it is a different word.

Example 3 — Links with noopener

Highlight external links that include noopener in their rel attribute.

rel-noopener.css
a[rel~="noopener"] {
  color: #16a34a;
  font-weight: 600;
}

a {
  color: #2563eb;
  text-decoration: none;
}
Try It Yourself

How It Works

rel="noopener noreferrer" is a space-separated list. [rel~="noopener"] matches because noopener is one of the tokens.

Example 4 — ~= vs *= for class names

See why whole-word matching matters when targeting class tokens.

tilde-vs-contains.css
/* Matches class="btn" or class="btn primary" — not "btn-large" */
[class~="btn"] {
  outline: 2px solid #2563eb;
}

/* Also matches class="btn-large" */
[class*="btn"] {
  background: #fef9c3;
}
Try It Yourself

How It Works

[class~="btn"] requires btn as its own word. [class*="btn"] matches any class string containing those three letters, including btn-large.

⚠️ Common Pitfalls

  • Partial words[class~="le"] does not match class="lead"; only complete tokens count.
  • Case sensitivity[class~=Lead] does not match class="lead".
  • Wrong separator — Works for space-separated lists only, not commas or hyphens.
  • Prefer .class — For simple class targeting, .lead is shorter and equally effective.

♿ Accessibility

  • Semantic HTML — Use proper elements and ARIA roles; attribute selectors are for styling, not structure.
  • rel attributes — Include noopener for security on target="_blank" links regardless of CSS styling.
  • Color alone — Do not rely only on background color from class-based styling to convey meaning.

🧠 How [attribute~=value] Works

1

Read attribute value

The browser reads the full attribute string from the DOM.

DOM
2

Split by spaces

~= checks whether your value matches one of the space-separated words.

Match
3

Apply styles

Elements with a matching word receive the CSS rule.

Render
~

Whole-word token matching

Ideal for class lists and rel attribute tokens.

Browser Compatibility

The [attribute~=value] selector is supported in all modern browsers.

Universal · All browsers

Space-separated matching everywhere

Chrome, Firefox, Safari, Edge, and Opera all support the ~= operator.

99% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All versions
Full support
[attribute~=value] selector 99% supported

Bottom line: Safe to use ~= for class lists and space-separated attribute tokens.

Conclusion

The [attribute~=value] selector is a practical tool for matching whole words in space-separated attribute values. It is especially useful for class and rel attributes.

Remember that it matches complete tokens only — not substrings — and compare it with *= and |= when choosing the right operator for your markup.

💡 Best Practices

✅ Do

  • Use [class~="lead"] for multi-class lists
  • Match whole words in rel attributes
  • Prefer .classname when styling by class
  • Combine with element selectors for specificity
  • Use ~= when you need exact token matching

❌ Don’t

  • Expect partial word matches with ~=
  • Use ~= for hyphen-separated lang codes
  • Ignore case in class token names
  • Apply to comma-separated attribute values
  • Confuse ~= with *=

Key Takeaways

Knowledge Unlocked

Five things to remember about ~=

Use these points when matching space-separated words.

5
Core concepts
📄 02

class~=lead

Multi-class.

Pattern
🔗 03

rel~=noopener

Link tokens.

Uses
🔍 04

Not partial

lead ≠ leads

Rule
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

It matches elements whose attribute contains the given value as a whole word in a space-separated list. The value must be a complete token, not a partial substring.
[class~=btn] matches class="btn primary" but not class="btn-large". [class*="btn"] matches any value containing the letters btn anywhere.
Yes. The word match is case-sensitive, so [class~=Lead] does not match class="intro lead".
The class and rel attributes are the most common. class holds multiple class names; rel can list values like noopener noreferrer.
Yes. All modern browsers support the ~= attribute selector operator.

Practice in the Live Editor

Open the HTML editor and experiment with [class~="lead"] and other space-separated attribute selectors.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful