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 an attribute value ends with a specific string. It is ideal for file extensions like .html, .pdf, and .webp.

01

Ends with

Suffix match.

02

$= operator

Suffix operator.

03

Extensions

.html, .pdf.

04

href / src

URLs & paths.

05

vs *=

End vs contains.

06

:hover

Combine states.

Introduction

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

This is particularly useful for styling links to local pages, downloadable files, or images based on how their URLs or paths end — without adding extra classes to every element.

Definition and Usage

Write the attribute name in brackets, then $= and the suffix in quotes: a[href$=".html"]. The element matches only if the attribute value ends with .html exactly at the final characters.

💡
Beginner Tip

$= means “ends with.” Use [attr^=value] when the match must be at the start, and [attr*=value] when it can appear anywhere.

📝 Syntax

The signature of the ends-with attribute selector is:

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

Basic Example

href-ends-with.css
a[href$=".html"] {
  color: #2563eb;
  font-weight: 600;
}

a[href$=".html"]:hover {
  background-color: #e2e8f0;
}

Syntax Rules

  • $= checks that the attribute value ends with the given string.
  • The match is case-sensitive.
  • Include the dot in extensions: .pdf, not just pdf, for precise matching.
  • Combine with element names: img[src$=".webp"].
  • Works with pseudo-classes: a[href$=".html"]:hover.

Compare Attribute Operators

SelectorMatches when
[attr$="x"]Value ends with x
[attr^="x"]Value starts with x
[attr*="x"]Value contains x anywhere
[attr="x"]Value is exactly x

⚡ Quick Reference

QuestionAnswer
Operator$= (ends with)
Match positionSuffix only — final characters of value
Case sensitive?Yes
Common patterna[href$=".pdf"]
vs contains*= matches anywhere in value
Browser supportAll modern browsers
a[href$=".html"] a[href$=".pdf"] img[src$=".webp"] a[href$=".css"]

When to Use [attribute$=value]

  • Local page links — Style links ending in .html differently from external URLs.
  • Download files — Highlight .pdf, .zip, or .docx links.
  • Image formats — Border or badge images ending in .png or .webp.
  • Asset paths — Theme stylesheets ending in -dark.css or .min.js.
  • Language codes — Match hreflang values ending in -US when structured consistently.

👀 Live Preview

Links ending in .html are underlined; the external blog link does not match:

Examples Gallery

Practice suffix matching with page links, downloads, images, and asset filenames.

📜 Core Patterns

Match file extensions and URL suffixes at the end of attribute values.

Example 1 — Links ending in .html

Style internal page links and add a hover background for feedback.

html-links.css
a[href$=".html"] {
  color: #2563eb;
  font-weight: 600;
}

a[href$=".html"]:hover {
  background-color: #e2e8f0;
}
Try It Yourself

How It Works

about.html and contact.html end with .html. The external blog URL does not, so it keeps default styling.

Example 2 — PDF download links

Target links whose href ends with .pdf for a distinct download style.

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

How It Works

$= is stricter than *=: a path like /pdf/guide would not match .pdf at the end, but guide.pdf would.

📄 Media & Assets

Use suffix matching for images and stylesheet filenames.

Example 3 — WebP images

Style images whose src ends with .webp.

webp-images.css
img[src$=".webp"] {
  border: 3px solid #7c3aed;
  border-radius: 8px;
  padding: 4px;
}
Try It Yourself

How It Works

Only the WebP image path ends with .webp. The PNG file uses a different suffix and is not selected.

Example 4 — Asset filename suffixes

Compare styling for asset links ending in .css versus .js.

asset-suffix.css
a[href$=".css"] {
  background: #eff6ff;
  color: #1d4ed8;
}

a[href$=".js"] {
  background: #fef9c3;
  color: #854d0e;
}
Try It Yourself

How It Works

Each rule targets a different file extension at the end of the href value, making asset types easy to distinguish in documentation or file lists.

⚠️ Common Pitfalls

  • Query stringspage.html?v=2 does not end with .html; the suffix is 2.
  • Case sensitivity.PDF and .pdf are different matches.
  • Missing attribute — The element must have the attribute for any match to occur.
  • Performance — Prefer classes for high-frequency styling on very large pages when possible.

♿ Accessibility

  • Clear link text — Do not rely on file-type color alone; write descriptive link labels.
  • Download cues — Pair visual PDF styling with text like “(PDF)” in the link when helpful.
  • External links — Distinguish internal .html pages from external URLs for clarity.

🧠 How [attribute$=value] Works

1

Read attribute value

The browser gets the full string from the HTML attribute.

DOM
2

Check the suffix

$= compares the end of the value to your quoted string.

Match
3

Apply styles

Elements with a matching suffix receive the CSS rule.

Render
=

Precise suffix targeting

Ideal for extensions and predictable URL endings.

Browser Compatibility

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

Universal · All browsers

Suffix 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 file extensions and URL suffixes in modern projects.

Conclusion

The [attribute$=value] selector is a precise tool for matching attribute values that end with a specific string. It shines when styling file types, local pages, and predictable URL patterns.

Combine it with pseudo-classes like :hover, compare it with *= and ^=, and remember that query strings can change what the browser sees as the suffix.

💡 Best Practices

✅ Do

  • Include the dot in extensions: .pdf
  • Use a[href$=".html"] for local pages
  • Combine with :hover for feedback
  • Prefer $= over *= for exact suffixes
  • Test URLs with query parameters

❌ Don’t

  • Assume page.html?v=1 ends with .html
  • Ignore case in file extensions
  • Style elements missing the attribute
  • Overuse attribute selectors on huge DOMs
  • Confuse $= with *=

Key Takeaways

Knowledge Unlocked

Five things to remember about $=

Use these points when matching suffixes.

5
Core concepts
📄 02

.html / .pdf

Extensions.

Pattern
🖼 03

img[src$=]

Media files.

Uses
🔍 04

Case matters

.pdf ≠ .PDF

Rule
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

It matches elements whose attribute value ends with the specified substring. The match must occur at the very end of the value.
[attr$=val] matches only when val is at the end of the attribute value. [attr*=val] matches when val appears anywhere inside the value.
Yes. The ends-with match is case-sensitive, so .PDF and .pdf are different.
Matching file extensions in href or src attributes, such as a[href$=".pdf"] or img[src$=".webp"].
Yes. All modern browsers support the $= attribute selector operator.

Practice in the Live Editor

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