← Back toCross Browser

Write CSS rules for specific browsers

Written byPhuoc Nguyen
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:
@support (-moz-appearance: none) {
/* Styles for Firefox only */
The `not` operator is useful to exclude the target browser:
@support not (-moz-appearance: none) {
/* Styles for browser except Firefox */
There is another, less popular hack for targeting Firefox only:
@-moz-document url-prefix() {
/* Styles for Firefox only */

Target Safari

@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:
@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? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.

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