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 a specific substring appears anywhere inside an attribute value. It is perfect for URLs, file types, and naming patterns.

01

Substring

Partial match.

02

*= operator

Contains value.

03

URLs

href patterns.

04

Files

.png, .pdf.

05

Classes

btn- prefix.

06

vs =

Exact vs partial.

Introduction

The CSS [attribute*=value] selector targets elements whose attribute value contains a specific substring. The match can occur at the start, middle, or end of the value.

This is especially useful when attribute values vary but share a common pattern — such as links containing https, images ending in .png, or class names that include btn-.

Definition and Usage

Write the attribute name in brackets, followed by *= and the substring in quotes: a[href*="about"]. The element matches if about appears anywhere in the href value.

💡
Beginner Tip

Think of *= as “contains.” If you need the value to start with a string, use [attribute^=value] instead. For ending with, use [attribute$=value].

📝 Syntax

The signature of the substring attribute selector is:

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

Basic Example

href-contains.css
a[href*="https"] {
  color: #2563eb;
  font-weight: 600;
}

a[href*="about"] {
  text-decoration: underline;
}

Syntax Rules

  • *= means “attribute value contains this substring.”
  • The substring match is case-sensitive.
  • Combine with element names: img[src*=".png"].
  • Quotes around the value are required when it contains special characters.
  • Multiple rules can target different substrings on the same attribute.

Compare Attribute Operators

SelectorMatches when
[attr]Attribute is present
[attr="x"]Exact full value is x
[attr*="x"]Value contains x anywhere
[attr^="x"]Value starts with x
[attr$="x"]Value ends with x
[attr~="x"]Value has word x in a space-separated list

⚡ Quick Reference

QuestionAnswer
Operator*= (contains)
Match typeSubstring anywhere in value
Case sensitive?Yes
Common patterna[href*="pdf"]
vs exact match[attr="val"] needs full value
Browser supportAll modern browsers
a[href*="https"] img[src*=".png"] a[href*="pdf"] [class*="btn-"]

When to Use [attribute*=value]

  • URL segments — Style links pointing to certain pages (about, blog).
  • File types — Highlight .pdf, .zip, or image extensions in href or src.
  • Protocol checks — Target https links for security indicators.
  • Class prefixes — Theme all elements whose class contains btn- or card-.
  • Data attributes — Match [data-theme*="dark"] for partial state values.

👀 Live Preview

Links with https are styled; the About link also matches about:

Examples Gallery

Practice substring matching with URLs, images, downloads, and class name patterns.

📜 Core Patterns

Match partial values inside href and src attributes.

Example 1 — href substring matching

Style links containing https and add an underline when the URL includes about.

href-substring.css
a[href*="https"] {
  color: #2563eb;
  font-weight: 600;
}

a[href*="about"] {
  text-decoration: underline;
}
Try It Yourself

How It Works

Every link with https in the URL gets blue bold text. The About link also matches about, so it receives an underline too.

Example 2 — Image file extension

Target PNG images by matching .png inside the src attribute.

img-png.css
img[src*=".png"] {
  border: 3px solid #16a34a;
  border-radius: 8px;
  padding: 4px;
}
Try It Yourself

How It Works

.png can appear anywhere in the file path. A .webp image without that substring is not selected.

📄 Real-World Patterns

Spot downloads and theme related class name groups.

Example 3 — PDF download links

Highlight links whose href contains pdf so downloads stand out.

pdf-links.css
a[href*="pdf"] {
  color: #dc2626;
  font-weight: 600;
}
Try It Yourself

How It Works

Both PDF links match because pdf appears in their paths. The changelog link does not contain that substring.

Example 4 — Class name contains pattern

Apply shared button styles to any class that includes btn-.

class-contains.css
[class*="btn-"] {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
}
Try It Yourself

How It Works

[class*="btn-"] matches btn-primary, btn-danger, and btn-ghost because each class string contains btn-.

⚠️ Common Pitfalls

  • Case sensitivity[href*="PDF"] will not match file.pdf.
  • Over-matching[href*="a"] matches almost every URL; keep substrings specific.
  • Performance — Broad *= rules on large pages can be slower; prefer classes when possible.
  • Specificity — Combine with element names (a[href*="pdf"]) to avoid unintended matches.

♿ Accessibility

  • Do not rely on color alone — PDF indicators should include clear link text, not just red styling.
  • Generated labels — If using ::after with PDF markers, ensure the link text is still descriptive.
  • External links — Pair visual cues with accessible names and rel attributes where needed.

🧠 How [attribute*=value] Works

1

Browser reads attribute values

Each element’s attribute string is evaluated against the selector.

DOM
2

Substring search runs

*= checks whether the value contains the quoted substring.

Match
3

Matching elements styled

All elements with a containing match receive the CSS declarations.

Render
=

Flexible partial matching

Style dynamic attribute values without exact strings.

Browser Compatibility

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

Universal · All browsers

Substring matching everywhere

Chrome, Firefox, Safari, Edge, and Opera all support the *= attribute 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 partial attribute matching in modern projects.

Conclusion

The [attribute*=value] selector gives you flexible control when attribute values share patterns but are not identical. It is ideal for URLs, file extensions, and naming conventions.

Use specific substrings, combine with element selectors, and reach for exact (=), starts-with (^=), or ends-with ($=) operators when they fit better.

💡 Best Practices

✅ Do

  • Use specific substrings like pdf or .png
  • Combine with element types: a[href*="..."]
  • Compare with ^= and $= for tighter matches
  • Use for data-attribute theming hooks
  • Test case sensitivity in your values

❌ Don’t

  • Use overly short substrings like a or e
  • Rely on *= when exact = is enough
  • Forget performance on very large DOM trees
  • Hide essential meaning in styling alone
  • Mix up *= with ~= (word list)

Key Takeaways

Knowledge Unlocked

Five things to remember about *=

Use these points when matching partial attribute values.

5
Core concepts
🔗 02

href / src

URL patterns.

Uses
📄 03

File types

.png, .pdf.

Pattern
🔢 04

Case matters

Sensitive match.

Rule
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

It matches elements whose attribute value contains the specified substring anywhere within the value.
[attr=val] requires an exact full match. [attr*=val] matches when val appears anywhere inside the attribute value.
Yes. The substring match is case-sensitive, so pdf and PDF are treated differently in the selector value.
Yes. Selectors like [data-status*=pending] work when the data attribute value contains that substring.
Yes. All modern browsers support the *= attribute selector operator.

Practice in the Live Editor

Open the HTML editor and experiment with a[href*="..."] and other substring 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