Feature Queries are used in CSS for precise feature detection. And theyâre now supported by all the modern browsers!
We use the @supports
keyword. For example:
@supports (height: 100vh) {
.container {
height: 100vh;
}
}
Here weâre checking if the vh
unit is supported. If so, the height
value is set accordingly.
As you can see theyâre structured much like a media query.
Letâs expand upon our example:
.container {
height: 100%;
}
@supports (height: 100vh) {
.container {
height: 100vh;
}
}
Hereâs weâve provided a fallback by default giving the container a height
value of 100%. If the browser supports the vh
value, 100vh will apply instead.
Older browsers will simply ignore the @supports
block.
We can use @supports
for any CSS property, to check any value.
We can also use the logical operators and
, or
and not
to build more complex feature queries.
Letâs use and
to check if the browser supports CSS Grid and Flexbox:
@supports (display: grid) and (display: flex) {
/* ... */
}
Another example, asking if CSS Grid is supported:
.element {
float: left;
...
}
@supports (display: grid) {
.element {
float: none;
display: grid;
/* ... */
}
}
Simple and effective!
Related Posts:
A little about me..
Hey, Iâm Tim! đ
Iâm a freelance business owner, web developer & author. I teach both new and experienced freelancers how to build a sustainable and successful freelancing business. Check out my Complete Guide to Freelancing if you'd like to find out more.
While you're here, you can browse through my blogs where I post freelancing tips, code tutorials, design inspiration, useful tools & resources, and much more! You can also join the newsletter, or find me on X.
Thanks for reading! đ