How to Target File Extensions in CSS

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

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

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:

// 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
Copied to clipboard!

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:

Remove ads
Remove ads

πŸ“š Get access to exclusive content

Want to get access to exclusive content? Support webtips with the price of a coffee to get access to tips, checklists, cheatsheets, and much more. β˜•

Get access Support us
Remove ads Read more on
Remove ads
Remove ads
πŸŽ‰ Thank you for subscribing to our newsletter. x 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.