Write CSS rules for specific browsers
Written byPhuoc Nguyen
Created
07 Sep, 2022
There are cases where we want styles to target a particular browser. We can use CSS feature queries with a declaration that is only available on the target browser.
For example, Firefox is only browser supporting
`-moz-appearance: none`
, the following snippet can be used to target Firefox only:css
@support (-moz-appearance: none) {
/* Styles for Firefox only */
}
The
`not`
operator is useful to exclude the target browser:css
@support not (-moz-appearance: none) {
/* Styles for browser except Firefox */
}
There is another, less popular hack for targeting Firefox only:
css
@-moz-document url-prefix() {
/* Styles for Firefox only */
}
#Target Safari
css
@supports (background: -webkit-named-image(i)) {
/* Styles for Safari only */
}
#Target Chromium
Since only Chromium based and Firefox browsers support the
`contain: paint`
declaration, we can combine `and`
and `not`
operators to exclude Firefox from the target browsers:css
@support (contain: paint) and (not (-moz-appearance: none)) {
/* Styles for Chromium only */
}
Be aware that targeting given browsers with feature queries is still a hack, because the browsers improve their engines continuously, and these properties can be supported or not in the future.
Questions? 🙋
Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
Recent posts ⚡
Newsletter 🔔
If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn