CSS [attribute^=value] Selector

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

Definition and Usage

The [attr^=value] selector matches elements whose attribute value starts with value (a prefix match).

Classic uses: secure links with a[href^="https"], site paths with a[href^="/"], or in-page anchors with a[href^="#"]. Compare with $= (suffix) and |= (hyphenated codes).

Standard-track.

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

Prefix, not language codes note

[lang^=en] also matches english. For language tags, prefer [lang|=en].

CSS Syntax

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

Optional flags before ]: i (case-insensitive) or s (force case-sensitive). Quote values with special characters.

Selector Parts

PartMeaningTry it
attrAttribute name (href, src, class, …)Try it
^=Starts-with (prefix) operatorTry it
valueRequired prefix, e.g. https, /, or #Try it
vs |=Hyphen-aware match for language-style codesTry it

How to Use ^=

Pick the attribute, choose a clear prefix, then style only the matches.

  1. Choose the attribute that holds the string (href, src, or data-*).
  2. Write [attr^="prefix"] with the exact starting text.
  3. Apply styles for those matches.
  4. Use |= instead when you need a hyphen boundary (language codes).
GoalTypical selector
HTTPS linksa[href^="https"]
Root-relative pathsa[href^="/"]
In-page anchorsa[href^="#"]
Email linksa[href^="mailto:"]

Live Preview

Pick a prefix. Matching links highlight with real [href^=…] rules.

Prefix

Only links whose href starts with the chosen prefix light up.

CSS Examples

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

Example 1 — Style HTTPS Links

Emphasize anchors whose href starts with https.

CSS
<style>
  a[href^="https"] {
    color: #15803d;
    font-weight: 600;
  }
</style>

<a href="https://example.com">Secure</a>
<a href="/about">Internal</a>
Try It Yourself

How It Works

  1. Target href. Link destinations live in that attribute.
  2. Require the prefix. ^="https" matches only values that start that way.
  3. Style matches. The secure link turns green and bold.
  4. Skip others. Paths like /about do not start with https.

Example 2 — Style Root-Relative Paths

Highlight site links whose href starts with /.

CSS
<style>
  a[href^="/"] {
    color: #6d28d9;
    font-weight: 600;
  }
</style>

<a href="/about">About</a>
<a href="https://example.com">External</a>
Try It Yourself

How It Works

  1. Use a short prefix. / marks root-relative URLs.
  2. Write the selector. a[href^="/"] targets those links.
  3. Apply a cue. About is emphasized.
  4. Leave absolute URLs. https://… does not start with /.

Example 3 — Why Languages Prefer |=

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

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

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

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

How It Works

  1. ^= is any prefix. Both lines can be underlined.
  2. |= needs a hyphen boundary. Only en-US gets the blue color.
  3. Pick by intent. URL schemes → ^=; language tags → |=.
  4. Chain when needed. MDN pattern: a[href^="https://"][href$=".org"].

Edge Cases & Pitfalls

If a prefix rule surprises you, check these first.

case

Watch letter case

^="https" misses HTTPS://… unless you add the i flag.

scheme

https also matches https: variants

^="https" matches https:// and would also match a made-up httpsfoo. Prefer ^="https://" when you need the scheme boundary.

lang

Do not use ^= for language tags

Use |= so english does not slip through.

slash

Protocol-relative URLs

//cdn.example.com starts with /, so [href^="/"] matches it too.

end

Need the ending instead?

Use $= for file extensions and other suffixes.

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 3.5+, 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 3.5+
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 URL prefixes; use |= for language-style codes.

Key Takeaways

  • Role: ^= matches an attribute value prefix.
  • Common use: a[href^="https"], a[href^="/"], a[href^="#"].
  • vs |= / $=: any prefix vs hyphen codes vs suffix.
  • Pitfall: loose prefixes can over-match (prefer https:// over bare https when needed).

One line: use [attr^="prefix"] when the beginning of an attribute value is what matters.

Frequently Asked Questions

It matches elements whose attribute value begins with the given substring. The match must be at the very start of the value.
^= matches a prefix at the start. $= matches a suffix at the end.
^= matches any starting string (including english for en). |= matches exact value or value followed by a hyphen (en, en-US — not english).
For attributes like href, class, id, and data-*, matching is usually case-sensitive. Use the i flag for ASCII case-insensitive matching: [href^="https" i].
Styling links by scheme or path: a[href^="https"], a[href^="/"], a[href^="#"], or a[href^="mailto:"].
Widely available: Chrome 4+, Firefox 3.5+, Safari 3.1+, Edge 12+, IE 7+. It is a browser CSS selector, not a Node.js API.

Next: ::before

Insert generated content before an element’s content with the ::before pseudo-element.

::before tutorial →

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