← Back toCross Browser

CSS feature queries

Written byPhuoc Nguyen
Created
06 Sep, 2022
Modern browsers provide a convenient way to detect if a CSS property is supported. The syntax is similar to the media query which starts with `@`:
@support (property: value) {
/* CSS declarations */
}
We can replace the fallback properties used in the previous technique:
.list {
display: flex;
display: grid;
}
with CSS feature queries:
.list {
display: flex;
}

/* In general speaking, use grid if it is supported by the browser */
@support (display: grid) {
.list {
display: grid;
}
}
It's good to know that we can use the `and`, `or` and `not` operators to build a complex query:
@support (...) and (...) {
/* CSS declarations */
}
@support (...) or (...) {
/* CSS declarations */
}

Avoid using the not operator

The `not` operator tells the browser to apply styles declared inside the `@support` block if it doesn't not support the input declaration. Let's revise the example above with the opposite approach:
.list {
display: grid;
}

@supports not (display: grid) {
.list {
display: flex;
}
}
By default, the CSS grid is used for the `list` class. It will fallback to the CSS flexbox if the browser doesn't support CSS grid. It seems to be good and works in modern browsers.
What could happen if the browser doesn't understand both CSS grid syntax and `@supports`? IE 11 is one of such browsers. In that scenario, all of our styles aren't effective. It's recommended to avoid using the `not` operator of CSS feature queries.

Check for feature queries programmatically

In addition to the declarative way of using `@support`, it's possible to check if a particular CSS declaration is supported programmatically:
if (!CSS || !CSS.supports('display', 'grid')) {
/* CSS grid isn't supported */
}

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