The [attribute|=value] selector matches elements when an attribute value is exactly equal to a string, or when it starts with that string followed by a hyphen. It is ideal for language codes like en-US and en-GB.
01
Hyphen match
value or value-…
02
|= operator
Pipe-equals.
03
lang
en, en-US.
04
hreflang
Alt languages.
05
vs ^=
Hyphen vs prefix.
06
Exact en
Also matches en.
Fundamentals
Introduction
The [attribute|=value] selector in CSS targets elements whose attribute value is either exactly equal to a given string, or begins with that string followed immediately by a hyphen (-).
This pattern is especially useful for BCP 47 language tags such as lang="en", lang="en-US", and lang="en-GB" — all matchable with a single [lang|=en] rule.
Definition and Usage
Write the attribute in brackets, then |= and the base value: [lang|=en]. It matches lang="en" and lang="en-US", but not lang="english" or lang="fr".
💡
Beginner Tip
Think of |= as “equals value or value-something.” The hyphen after the base string is required for the extended form. Use [attr^=value] when any prefix match is enough.
Foundation
📝 Syntax
The signature of the hyphen-separated attribute selector is:
data-theme="dark-high-contrast" matches because it starts with dark-. A value like midnight-dark would not match [data-theme|="dark"].
Example 4 — |= vs ^= for lang codes
See why |= is safer than ^= when matching language prefixes.
lang-vs-prefix.css
/* Matches en, en-US, en-GB — not "english" */[lang|="en"]{outline:2px solid #2563eb;}/* Would also match lang="english" */[lang^="en"]{background:#fef9c3;}
[lang^="en"] selects any value starting with the letters en, including accidental matches. [lang|="en"] requires either an exact en or a proper en- locale suffix.
Watch Out
⚠️ Common Pitfalls
Underscores — lang="en_US" does not match [lang|="en"]; the separator must be a hyphen.
Wrong prefix — lang="eng" does not match [lang|="en"] because it is not en or en-….
Case sensitivity — lang="EN-us" and lang="en-US" are different.
Confusing with ^= — Use ^= only when any prefix match is intended, not for structured locale codes.
A11y
♿ Accessibility
Correct lang — Always set valid lang attributes in HTML; CSS styling is supplementary.
Screen readers — Language detection relies on HTML semantics, not CSS selectors.
Color alone — Do not use color as the only way to distinguish languages; labels should remain readable.
🧠 How [attribute|=value] Works
1
Read attribute value
The browser gets the full string from the HTML attribute.
DOM
2
Check exact or hyphen form
|= matches if the value equals value or starts with value-.
Match
3
Apply styles
Elements with a matching value pattern receive the CSS rule.
Render
=
✅
Structured hyphen matching
Ideal for language tags and base-suffix attribute values.
Compatibility
Browser Compatibility
The [attribute|=value] selector is supported in all modern browsers.
✓ Universal · All browsers
Hyphen 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 language codes and hyphen-structured attribute values.
Wrap Up
Conclusion
The [attribute|=value] selector is a precise tool for matching attribute values that are either exactly equal to a base string or extend it with a hyphenated suffix.
It shines with lang and hreflang attributes, and it avoids the false positives that ^= can introduce when matching short language prefixes.