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.
Fundamentals
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.
Foundation
📝 Syntax
The signature of the ends-with attribute selector is:
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.
Watch Out
⚠️ Common Pitfalls
Query strings — page.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.
A11y
♿ 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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll versions
Full support
[attribute$=value] selector99% supported
Bottom line: Safe to use $= for file extensions and URL suffixes in modern projects.
Wrap Up
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.