← 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 `@`:
css
@support (property: value) {
/* CSS declarations */
}
We can replace the fallback properties used in the previous technique:
css
.list {
display: flex;
display: grid;
}
with CSS feature queries:
css
.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:
css
@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:
css
.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:
js
if (!CSS || !CSS.supports('display', 'grid')) {
/* CSS grid isn't supported */
}
If you found this post helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

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 🥷.
Ask me questions

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