
How to Target File Extensions in CSS
You can target file extensions in CSS by using a regex selector:
Copied to clipboard!
[href$=".pdf"]:before {
content: "π";
}
This example above uses an attribute selector that selects each href
attribute, that ends with ".pdf
", meaning this will select all anchors where the link is pointing to a PDF file. You can also use the following selectors to select different parts of the attribute:
Copied to clipboard!
// Selects an href with a specific value
[href="https://webtips.dev"]:before {
content: "π¨βπ»";
}
// Selects an href which begins with "https"
[href^="https"]:before {
content: "βοΈ";
}
// Selects an href which contains the word "download"
[href*="download"]:before {
content: "π₯";
}
// Selects an href which contains the word "insensitive", case-insensitively
[href*="insensitive" i]:before { ... }
// Selects an href which contains the word "SeNsItIve", case-sensitively
[href*="SeNsItIve" s]:before { ... }
// Selects an href which begins with "https" and ends with ".dev"
[href^="https"][href$=".dev"]:before {
content: "π¨βπ»";
}
This lets you target either the beginning, part of, the whole, or end of the string. Note that you can also use i
or s
to match a string case-insensitively or case-sensitively. If you combine them you can also filter out specific urls which begins and ends with certain values.


Resources:
π More Webtips
Master the Art of Frontend
Access exclusive interactive lessons
Unlimited access to hundreds of tutorials
Remove ads to learn without distractions
Courses

CSS - The Complete Guide (including Flexbox, Grid and Sass)
Including Flexbox, Grid, and Sass
Whether you're learning CSS for the first time or brushing up on your CSS skills and diving even deeper, this course is for you. Every web developer has to know CSS.

The HTML & CSS Bootcamp
From Zero to Expert!
This course covers flexbox, CSS grid, animations, responsive design, and much more! You will find tons of exercises & projects inside this course.

The Creative HTML5 & CSS3 Course
Build Awesome Websites
Learn HTML5 and CSS3 by creating three amazing, well-designed and animated websites from scratch.