CSS [attribute|=value] Selector

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The [attr|=value] selector matches when the attribute value is exactly value, or starts with value followed immediately by a hyphen (-).

Classic use: language tags. [lang|=en] matches en, en-US, and en-GB—but not english. Compare with = (exact only) and ^= (any prefix).

Standard-track.

Part of CSS attribute selectors (Selectors Level 3/4). Baseline widely available. Not deprecated or experimental.

Hyphen boundary note

The next character after value must be - (or the value ends). english starts with en but has no hyphen, so [lang|=en] skips it.

CSS Syntax

syntax.css
[attr|= value] { … }

Optional flags before ]: i (case-insensitive) or s (force case-sensitive).

Selector Parts

PartMeaningTry it
attrAttribute name (lang, hreflang, …)Try it
|=Exact value, or value + hyphen + moreTry it
valueBase token, e.g. en or zhTry it
vs ^=Prefix match without requiring a hyphenTry it

How to Use |=

Use it for hyphenated codes where the primary tag matters.

  1. Pick the attribute that stores a language or similar code (lang, hreflang).
  2. Write [attr|=primary] with the primary tag (e.g. en).
  3. Style all matching locales together.
  4. Prefer |= over ^= when you must reject lookalikes like english.
GoalTypical selector
Any English locale[lang|=en]
Any Chinese locale[lang|=zh]
English alternate linksa[hreflang|=en]
Exact language only[lang=en] (no en-US)

Live Preview

Pick a primary language tag. Matching elements highlight with real [lang|=…] rules.

Primary tag

Matches exact tags and hyphenated variants (e.g. en-US), not lookalike words.

Result

en — base English

en-US — American English

en-GB — British English

english — lookalike (no hyphen)

fr — French

fr-CA — Canadian French

zh-Hans — Simplified Chinese

Matching [lang|=en]

CSS Examples

Each snippet focuses on |=. Use Try It Yourself for a live editor.

Example 1 — Style All English Locales

Match en, en-US, and en-GB with one rule.

CSS
<style>
  [lang|="en"] {
    color: #1d4ed8;
    font-weight: 600;
  }
</style>

<p lang="en-US">American English</p>
<p lang="en">Base English</p>
<p lang="fr">Français</p>
Try It Yourself

How It Works

  1. Choose lang. HTML language tags often use primary-region form.
  2. Query the primary tag. |=en means en or en-*.
  3. Style matches. Both English lines turn blue and bold.
  4. Skip others. fr does not start with en-, so it stays normal.

Example 2 — Highlight English Alternate Links

Use hreflang the same way for language switchers.

CSS
<style>
  a[hreflang|="en"] {
    color: #1d4ed8;
    font-weight: 600;
  }
</style>

<a href="/en-us/" hreflang="en-US">English (US)</a>
<a href="/fr/" hreflang="fr-FR">Français</a>
Try It Yourself

How It Works

  1. Target links. Combine a with hreflang.
  2. Match English variants. |=en covers en-US and friends.
  3. Style the switcher. The English link is emphasized.
  4. Leave other languages. French keeps default styling.

Example 3 — Why |= Beats ^= for Languages

Show that ^=en also matches the lookalike word english.

CSS
<style>
  [lang|="en"] {
    color: #1d4ed8;
  }

  [lang^="en"] {
    text-decoration: underline;
  }
</style>

<p lang="en-US">en-US</p>
<p lang="english">english</p>
Try It Yourself

How It Works

  1. |= needs a hyphen boundary. en-US matches; english does not (for color).
  2. ^= is any prefix. Both lines can get underlines.
  3. Pick the safer tool. For BCP 47-style codes, prefer |=.
  4. Remember exact. Use = when only en (not en-US) should match.

Edge Cases & Pitfalls

If language styling looks wrong, check these first.

prefix

Do not confuse with ^=

^= matches any starting string. |= requires an exact match or a hyphen right after the value.

exact

Exact-only needs =

[lang=en] misses en-US. Use |= when regions should share styles.

case

Watch letter case

Values may be case-sensitive depending on the attribute. Use the i flag when casing varies.

space

Not for space-separated lists

For words separated by spaces, use ~=, not |=.

hyphen

Only the first hyphen segment

[lang|=zh] matches zh-Hans-CN because it starts with zh-. Query the primary tag you care about.

Browser Support

[attr|=value] is a browser CSS selector. Node.js, Deno, and Bun do not evaluate it.

✓ Baseline · Widely available

[attr|=value]

Supported in Chrome 4+, Firefox 2+, Safari 3.1+, Edge 12+, Opera 10+, IE 7+. The i case flag needs Chrome 49+, Firefox 47+, Safari 9+, Edge 79+.

98% Global support
Google Chrome 4+
Full support
Mozilla Firefox 2+
Full support
Apple Safari 3.1+
Full support
Microsoft Edge 12+
Full support
Opera 10+
Full support
Samsung Internet 4+
Full support
Internet Explorer 7+
Full support
|= Universally supported

Bottom line: Safe for production. Prefer |= for language-style hyphenated codes.

Key Takeaways

  • Role: match exact value or value-* with a hyphen boundary.
  • Common use: [lang|=en] for all English locales.
  • vs ^=: |= rejects lookalikes like english; ^= does not.
  • Pitfall: space-separated tokens need ~=, not |=.

One line: use [attr|=value] for hyphenated codes where the primary tag defines the group.

Frequently Asked Questions

It matches elements whose attribute value is exactly the given string, or starts with that string followed immediately by a hyphen (-).
[lang|=en] matches en and en-US, but not english. [lang^=en] matches any string that starts with en, including english.
= matches only the exact full value. |= also matches value plus a hyphen and more characters (en-US when querying en).
Matching is typically case-sensitive for custom and data attributes. Use the i flag for ASCII case-insensitive matching when needed.
Language codes: [lang|=en] styles en, en-US, and en-GB with one rule. Also useful with hreflang.
Widely available: Chrome 4+, Firefox 2+, Safari 3.1+, Edge 12+, IE 7+. It is a browser CSS selector, not a Node.js API.

Next: [attribute~=value]

Match one whole word inside a space-separated attribute value.

space selector →

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.

8 people found this page helpful