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.
Fundamentals
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].
Foundation
📝 Syntax
The signature of the space-separated attribute selector is:
[class~="btn"] requires btn as its own word. [class*="btn"] matches any class string containing those three letters, including btn-large.
Watch Out
⚠️ 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.
A11y
♿ 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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll versions
Full support
[attribute~=value] selector99% supported
Bottom line: Safe to use ~= for class lists and space-separated attribute tokens.
Wrap Up
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.
Use these points when matching space-separated words.
5
Core concepts
🔢01
Whole word
Token match.
Operator
📄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.