CSS [attribute^=value] Selector

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Attribute Selectors

What You’ll Learn

The [attribute^=value] selector matches elements when an attribute value begins with a specific string. It is ideal for URL schemes, path prefixes, and class naming patterns.

01

Starts with

Prefix match.

02

^= operator

Caret-equals.

03

href URLs

https, /.

04

Class prefix

btn-, nav-.

05

vs $=

Start vs end.

06

vs |=

Prefix vs hyphen.

Introduction

The [attribute^=value] selector in CSS is an attribute selector that targets elements whose specified attribute value starts with a given substring.

This is particularly useful for styling links by protocol, internal paths, form field name prefixes, or utility classes that share a common beginning — without adding extra classes to every element.

Definition and Usage

Write the attribute name in brackets, then ^= and the prefix in quotes: a[href^="https"]. The element matches only if the attribute value begins with those exact characters at the first position.

💡
Beginner Tip

^= means “starts with.” Use [attr$=value] when the match must be at the end, and [attr|=value] when you need an exact value or a hyphen-separated suffix like en-US.

📝 Syntax

The signature of the starts-with attribute selector is:

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

Basic Example

href-https.css
a[href^="https"] {
  color: #16a34a;
  font-weight: 600;
}

a[href^="/"] {
  color: #7c3aed;
}

Syntax Rules

  • ^= checks that the attribute value begins with the given string.
  • The match is case-sensitive.
  • The prefix must be at the very start — not in the middle of the value.
  • Combine with element names: a[href^="https"].
  • Works with pseudo-classes: a[href^="https"]:hover.

Compare Attribute Operators

SelectorMatches when
[attr^="x"]Value starts with x
[attr$="x"]Value ends with x
[attr*="x"]Value contains x anywhere
[attr|="x"]Value is x or starts with x- (hyphen rule)

⚡ Quick Reference

QuestionAnswer
Operator^= (starts with)
Match positionPrefix only — first characters of value
Case sensitive?Yes
Common patterna[href^="https"]
vs ends with$= matches the suffix at the end
Browser supportAll modern browsers
a[href^="https"] a[href^="/"] [class^="btn-"] input[name^="user_"]

When to Use [attribute^=value]

  • External links — Style links starting with https:// or mailto:.
  • Internal paths — Highlight root-relative links like href="/about".
  • Class prefixes — Target utility classes such as btn-primary via [class^="btn-"].
  • Form fields — Style inputs whose name starts with a shared prefix.
  • CDN assets — Match src values starting with a CDN domain.

👀 Live Preview

Links starting with https are green; links starting with / are purple:

Examples Gallery

Practice prefix matching with external URLs, internal paths, class prefixes, and form field names.

🔗 URL Prefixes

Match links by how their href value begins.

Example 1 — Links starting with https

Style secure external links that begin with the https protocol.

href-https.css
a[href^="https"] {
  color: #16a34a;
  font-weight: 600;
}

a[href^="https"]:hover {
  text-decoration: underline;
}
Try It Yourself

How It Works

Only URLs beginning with https match. A relative path like /contact does not start with those letters.

Example 2 — Internal root-relative links

Highlight same-site links whose href starts with /.

href-internal.css
a[href^="/"] {
  color: #7c3aed;
  font-weight: 600;
}

a {
  color: #2563eb;
  text-decoration: none;
}
Try It Yourself

How It Works

Root-relative paths start with /. Full external URLs start with a scheme like https and do not match.

🎨 Naming Patterns

Use prefix matching for class and form field naming conventions.

Example 3 — Class name prefixes

Style elements whose class attribute starts with btn-.

class-prefix.css
[class^="btn-"] {
  display: inline-block;
  padding: 0.45rem 1rem;
  border-radius: 6px;
  font-weight: 600;
  border: none;
  cursor: pointer;
}

.btn-primary { background: #2563eb; color: #fff; }
.btn-danger { background: #dc2626; color: #fff; }
Try It Yourself

How It Works

btn-primary and btn-danger both start with btn-. A class like card or my-btn would not match because the prefix is not at the start.

Example 4 — Form field name prefixes

Style inputs whose name attribute starts with user_.

name-prefix.css
input[name^="user_"] {
  border: 1px solid #93c5fd;
  background: #eff6ff;
  padding: 0.5rem;
  border-radius: 6px;
}

label {
  display: block;
  margin-bottom: 0.35rem;
  font-weight: 600;
}

.field { margin-bottom: 1rem; }
Try It Yourself

How It Works

user_name and user_email share the user_ prefix. Fields like order_id use a different naming pattern and are not selected.

⚠️ Common Pitfalls

  • http vs https[href^="http"] also matches https:// URLs because https starts with http. Put the more specific rule first or use https alone.
  • Case sensitivityHTTPS and https are different prefixes.
  • Not substring — The match must be at the start of the value, not anywhere inside it.
  • vs |= — Use |= for hyphen-separated locale codes; ^= is a plain character prefix match.

♿ Accessibility

  • External link cues — Do not rely only on color to mark external links; add clear link text or icons.
  • Form labels — Prefix-based input styling is visual only; always pair inputs with <label> elements.
  • Contrast — Ensure styled link and button colors meet WCAG contrast requirements.

🧠 How [attribute^=value] Works

1

Read attribute value

The browser gets the full string from the HTML attribute.

DOM
2

Check the prefix

^= compares the beginning of the value to your quoted string.

Match
3

Apply styles

Elements with a matching prefix receive the CSS rule.

Render
^

Precise prefix targeting

Ideal for URLs, paths, and naming conventions.

Browser Compatibility

The [attribute^=value] selector is supported in all modern browsers.

Universal · All browsers

Prefix 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 URL schemes, paths, and attribute prefixes in modern projects.

Conclusion

The [attribute^=value] selector is a precise tool for matching attribute values that begin with a specific string. It shines when styling links by protocol, internal paths, and shared naming prefixes.

Combine it with pseudo-classes like :hover, compare it with $= and |=, and watch out for overlapping prefixes like http and https.

💡 Best Practices

✅ Do

  • Use a[href^="https"] for secure external links
  • Match internal paths with a[href^="/"]
  • Put more specific prefixes before general ones
  • Combine with :hover for feedback
  • Use |= for lang codes, not ^=

❌ Don’t

  • Assume http and https rules are mutually exclusive
  • Ignore case in URL prefixes
  • Expect matches in the middle of a value
  • Use ^= for space-separated class tokens
  • Confuse ^= with *=

Key Takeaways

Knowledge Unlocked

Five things to remember about ^=

Use these points when matching attribute prefixes.

5
Core concepts
🔗 02

href^=https

External URLs.

Pattern
📁 03

href^=/

Internal paths.

Uses
🔍 04

http trap

https matches http

Rule
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

It matches elements whose attribute value begins with the specified substring. The match must occur at the very start of the value.
[attr^=val] matches when val is at the start of the value. [attr$=val] matches when val is at the end of the value.
Yes. The starts-with match is case-sensitive, so HTTPS and https are different.
Matching URL schemes like a[href^="https"], internal paths like a[href^="/"], or class prefixes like [class^="btn-"].
Yes. All modern browsers support the ^= attribute selector operator.

Practice in the Live Editor

Open the HTML editor and experiment with a[href^="https"] and other prefix 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