πŸŽ„ Get 20% off from our JavaScript course for the holidays! πŸŽ„
πŸ’‘ This page contain affiliate links. By making a purchase through them, we may earn a commission at no extra cost to you.
How to Target File Extensions in CSS

How to Target File Extensions in CSS

Ferenc Almasi β€’ 2020 December 01 β€’ Read time 1 min read
  • twitter
  • facebook
CSS

You can target file extensions in CSS by using a regex selector:

Copied to clipboard!
[href$=".pdf"]:before {
  content: "πŸ“œ";
}
extension.css

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: "πŸ‘¨β€πŸ’»";
}
atrributes.css

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.

How to target file extensions in CSS
If you would like to see more Webtips, follow @flowforfrank

10 Best Practices for Quickly Improving Your CSS

Resources:

Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access exclusive interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Remove ads to learn without distractions
Become a Pro

Courses

Recommended