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 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.

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.

📝 Syntax

The signature of the hyphen-separated attribute selector is:

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

Basic Example

lang-en.css
[lang|="en"] {
  color: #1d4ed8;
  font-weight: 600;
}

[lang|="fr"] {
  color: #7c3aed;
  font-style: italic;
}

Syntax Rules

  • |= matches the exact value or value- followed by more text.
  • The separator after the base value must be a hyphen (-), not an underscore or space.
  • The match is case-sensitive.
  • Combine with element names: p[lang|="en"].
  • Designed for hyphenated value lists like language and locale codes.

Compare Attribute Operators

SelectorMatches when
[attr|="en"]Value is en or starts with en-
[attr="en"]Value is exactly en only
[attr^="en"]Value starts with en (including english)
[attr~="en"]en appears as a space-separated word in the value

⚡ Quick Reference

QuestionAnswer
Operator|= (hyphen-separated match)
Match typeExact value or value-suffix
Case sensitive?Yes
Common pattern[lang|="en"]
vs ^=^= does not require a hyphen after the prefix
Browser supportAll modern browsers
[lang|="en"] [lang|="fr"] [hreflang|="en"] [data-theme|="dark"]

When to Use [attribute|=value]

  • Language tags — Style all English variants with [lang|="en"].
  • Alternate links — Target hreflang values like en-US and en-GB.
  • Locale prefixes — Match base locale codes before regional suffixes.
  • Theme tokens — Match data-theme="dark" and data-theme="dark-high-contrast".
  • Hyphenated enums — Any attribute where values follow a base-suffix pattern.

👀 Live Preview

Paragraphs with lang starting with en or exactly en are highlighted; French text is not:

This paragraph is in American English.

This paragraph is in British English.

Ce paragraphe est en français.

Examples Gallery

Practice hyphen-separated matching with language tags, alternate links, themes, and operator comparisons.

🌐 Language Codes

The classic use case for hyphen-separated attribute matching.

Example 1 — English language paragraphs

Style all English variants with one rule using [lang|="en"].

lang-en.css
[lang|="en"] {
  color: #1d4ed8;
  font-weight: 600;
  border-left: 3px solid #2563eb;
  padding-left: 0.65rem;
}
Try It Yourself

How It Works

en-US and en-GB both start with en-. The French paragraph has a different base language code and is excluded.

Example 2 — Alternate language links

Highlight alternate links for English locales using [hreflang|="en"].

hreflang-en.css
a[hreflang|="en"] {
  color: #1d4ed8;
  font-weight: 600;
}

a[hreflang|="fr"] {
  color: #7c3aed;
  font-weight: 600;
}
Try It Yourself

How It Works

hreflang="en-US" and hreflang="en-GB" both match [hreflang|="en"] because the value equals en or continues with en-.

🎨 Custom Attributes

Apply the same hyphen logic to data attributes and theme tokens.

Example 3 — Theme variants

Match a base theme and its hyphenated variants with [data-theme|="dark"].

data-theme.css
[data-theme|="dark"] {
  background: #1e293b;
  color: #f8fafc;
  padding: 1rem;
  border-radius: 8px;
}

[data-theme|="light"] {
  background: #f8fafc;
  color: #0f172a;
  padding: 1rem;
  border-radius: 8px;
}
Try It Yourself

How It Works

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;
}
Try It Yourself

How It Works

[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.

⚠️ Common Pitfalls

  • Underscoreslang="en_US" does not match [lang|="en"]; the separator must be a hyphen.
  • Wrong prefixlang="eng" does not match [lang|="en"] because it is not en or en-….
  • Case sensitivitylang="EN-us" and lang="en-US" are different.
  • Confusing with ^= — Use ^= only when any prefix match is intended, not for structured locale codes.

♿ 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.

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 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 language codes and hyphen-structured attribute values.

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.

💡 Best Practices

✅ Do

  • Use [lang|="en"] for all English variants
  • Prefer |= over ^= for locale codes
  • Set valid BCP 47 language tags in HTML
  • Combine with element selectors for clarity
  • Test underscore vs hyphen formats

❌ Don’t

  • Assume en_US matches [lang|="en"]
  • Use ^= when hyphen structure matters
  • Rely on CSS alone for language accessibility
  • Ignore case in language codes
  • Confuse |= with ~= (space-separated)

Key Takeaways

Knowledge Unlocked

Five things to remember about |=

Use these points when matching hyphen-separated values.

5
Core concepts
🌐 02

en, en-US

Language tags.

Pattern
🔗 03

hreflang

Alt links.

Uses
🔍 04

vs ^=

Safer prefix.

Rule
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

It matches elements whose attribute value is exactly equal to the given string, or starts with that string followed immediately by a hyphen (-).
[lang|=en] matches lang="en" and lang="en-US", but not lang="english". [lang^=en] matches anything starting with "en", including "english".
Yes. The match is case-sensitive, so lang="EN-us" and lang="en-US" are different.
Matching language codes like lang="en", lang="en-US", and lang="en-GB" with a single [lang|=en] rule.
Yes. All modern browsers support the |= attribute selector operator.

Practice in the Live Editor

Open the HTML editor and experiment with [lang|="en"] and other hyphen-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