CSS Attribute Contains Selector (*=)

Definition
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].
Syntax
CSS Syntax
Optional flags: [attr*=value i] or [attr*=value s]. Quote values that need them.
Patterns
Selector Patterns
| Pattern | Description | Try it |
|---|
a[href*="example"] | Href contains the text example | Try it |
a[href*=".pdf"] | Links that mention .pdf in the URL | Try it |
[href*="example" i] | Case-insensitive substring match | Try it |
*= vs = | Partial match vs exact value | Try it |
Approach
How to Use *=
Pick the attribute, then the substring you want to find inside its value.
- Choose the attribute (
href, data-*, …). - Write
[attr*="substring"] (or with an element type). - Add
i if case should not matter. - Prefer
^= / $= when you only care about start or end.
| Piece | Required? | Role |
|---|
| Attribute name | Yes | Which attribute to inspect |
*= | Yes | Requires a substring match |
| Substring value | Yes | Text that must appear inside the value |
i / s | No | Case-insensitive or case-sensitive flag |
Try it
Live Preview
Links whose href contains example get a silver background via a[href*="example"].
Code
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.
a[href*="example"] {
background-color: silver;
}
Only the first link’s href contains the substring.
How It Works
- Target links. Start with the
a element. - Inspect href.
[href*=…] reads the URL string. - Look for a substring.
example may appear anywhere in the value. - Style matches. Non-matching hrefs stay unchanged.
Example 2 — Mark PDF Links
Style anchors whose href contains .pdf.
a[href*=".pdf"] {
color: #b45309;
font-weight: bold;
}
For “ends with .pdf” only, prefer $=.
How It Works
- Pick a distinctive fragment.
.pdf marks document URLs. - Use
*=. Match wherever that fragment appears in href. - Style the link. Color and weight call out file downloads.
- 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.
a[href*="example" i] {
color: teal;
}
Without i, Example would not match example.
How It Works
- Write the substring. Use the lowercase form you prefer.
- Add
i. Place it before the closing ]. - Match any casing.
Example, EXAMPLE, and example all qualify. - Keep
s rare. The case-sensitive flag has narrower support; default is often enough.
Edge Cases & Pitfalls
*= is powerful—and easy to over-match.
broadShort 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 ~=.
endsNeed start or end only?
Use ^= or $= instead of scanning the whole string.
caseCase can block a match
.PDF does not match [href*=".pdf"] unless you add i.
quotesQuote when needed
Values with spaces or special characters must be quoted: [title*="hello world"].
Compatibility
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.
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.
Remember
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: [attr$=value]
Learn how to match attribute values that end with a given suffix.
Ends-with tutorial →About the author
Developer, cloud engineer, and technical writer
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