CSS Attribute Contains Selector (*=)

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

Definition and Usage

The substring attribute selector [attr*=value] matches when the attribute value contains that string anywhere inside it.

Example: a[href*="example"] matches links whose href includes example (such as http://example.com or https://cdn.example.org/x).

Note Substring, not whole word

*= can match inside a longer token. For a space-separated word match, use ~=.

Note Case with the i flag

Default matching for attributes like href is case-sensitive. Add i for ASCII case-insensitive compares: [href*="pdf" i].

CSS Syntax

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

Optional flags: [attr*=value i] or [attr*=value s]. Quote values that need them.

Selector Patterns

PatternDescriptionTry it
a[href*="example"]Href contains the text exampleTry it
a[href*=".pdf"]Links that mention .pdf in the URLTry it
[href*="example" i]Case-insensitive substring matchTry it
*= vs =Partial match vs exact valueTry it

How to Use *=

Pick the attribute, then the substring you want to find inside its value.

  1. Choose the attribute (href, data-*, …).
  2. Write [attr*="substring"] (or with an element type).
  3. Add i if case should not matter.
  4. Prefer ^= / $= when you only care about start or end.
PieceRequired?Role
Attribute nameYesWhich attribute to inspect
*=YesRequires a substring match
Substring valueYesText that must appear inside the value
i / sNoCase-insensitive or case-sensitive flag

Live Preview

Links whose href contains example get a silver background via a[href*="example"].

href*="example" demo

The internal # link does not contain example, so it stays plain.

Contains preview

Rule used: .tut-atc-stage a[href*="example"]

CSS Examples

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

Example 1 — Href Contains a Domain Fragment

Highlight every link whose URL includes example.

CSS
a[href*="example"] {
  background-color: silver;
}
Try It Yourself

How It Works

  1. Target links. Start with the a element.
  2. Inspect href. [href*=…] reads the URL string.
  3. Look for a substring. example may appear anywhere in the value.
  4. Style matches. Non-matching hrefs stay unchanged.

Example 2 — Mark PDF Links

Style anchors whose href contains .pdf.

CSS
a[href*=".pdf"] {
  color: #b45309;
  font-weight: bold;
}
Try It Yourself

How It Works

  1. Pick a distinctive fragment. .pdf marks document URLs.
  2. Use *=. Match wherever that fragment appears in href.
  3. Style the link. Color and weight call out file downloads.
  4. Watch false positives. A path like pdf-tips.html would also match—tighten with $= if needed.

Example 3 — Case-Insensitive Match

Ignore capitalization with the i flag.

CSS
a[href*="example" i] {
  color: teal;
}
Try It Yourself

How It Works

  1. Write the substring. Use the lowercase form you prefer.
  2. Add i. Place it before the closing ].
  3. Match any casing. Example, EXAMPLE, and example all qualify.
  4. Keep s rare. The case-sensitive flag has narrower support; default is often enough.

Edge Cases & Pitfalls

*= is powerful—and easy to over-match.

broad

Short substrings match too much

[href*="a"] hits almost every URL. Prefer a distinctive fragment.

vs ~=

Not a class-word matcher

[class*="btn"] also matches btnLarge. For whole tokens, use .btn or ~=.

ends

Need start or end only?

Use ^= or $= instead of scanning the whole string.

case

Case can block a match

.PDF does not match [href*=".pdf"] unless you add i.

quotes

Quote when needed

Values with spaces or special characters must be quoted: [title*="hello world"].

Browser Support

*= is a browser CSS attribute selector. Node.js, Deno, and Bun do not evaluate it.

✓ Baseline · Widely available

[attr*=value]

Substring attribute matching is Baseline Widely available. The i flag is widely supported; the s flag is more limited.

99% All modern browsers
Google Chrome All
Full
Mozilla Firefox All
Full
Apple Safari All
Full
Microsoft Edge All
Full
Opera All
Full
Samsung Internet All
Full
Internet Explorer 7+
Full
[attr*=value] Universally supported

Bottom line: Production-ready. Prefer distinctive substrings, and use ^= / $= when position matters.

Key Takeaways

  • Role: match when an attribute value contains a substring.
  • Form: [attr*="text"] (optional element type and i).
  • vs others: = exact, ~= word, ^=/$= ends.
  • Tip: keep substrings specific to avoid accidental matches.

One line: [attr*="x"] means “the value includes x somewhere.”

Frequently Asked Questions

It matches an element when the attribute’s value contains the given substring anywhere—start, middle, or end.
[attr=value] needs an exact full match. [attr*=value] matches a partial string inside the value.
~= matches a whole word in a space-separated list. *= matches any substring, even inside a longer word.
For case-sensitive attributes (like class, id, data-*, href), yes—unless you add the i flag, for example [href*="example" i].
Yes. [data-status*="pend"] matches when the data-status value contains that substring.
No. It is a browser CSS attribute selector operator, not a Node.js feature.

Next: [attribute$=value] Selector

Continue through the CSS tutorial reference.

[attribute$=value] Selector tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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