πŸ’‘ 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:

  • twitter
  • facebook
CSS
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.