CSS feature queries are part of the CSS conditional rules module, which also defines the media query @media
at-rule. Feature queries behave similarly to media queries. The difference is that with a media query, you are testing something about the environment in which the web page is running, whereas with feature queries you are testing browser support for CSS features.
A feature query consists of the @supports
at-rule followed by the support condition or a supports()
function and declaration parameter within an @import
at-rule declaration:
/* `@supports` at-rule */
@supports <support-condition> {
CSS rules to apply
}
/* `supports()` function */
@import url_to_import supports(<declaration>);
For example, we can apply a set of styles or import an entire stylesheet if the user-agent supports red
as a valid value for the CSS color
property:
/* `@supports` at-rule */
@supports (color: red) {
CSS rules to apply
}
/* `supports()` function */
@import `/css/styles.css` supports(color: red);
As another example, if you want to check if a browser supports the row-gap
property you would write the following feature query. It doesn't matter which value you use in a lot of cases: if all you want is to check that the browser supports this property, then any valid value will do.
<div class="box">
If your browser supports the row-gap property, the border will be dashed and
text will be red.
</div>
body {
font: 1.2em / 1.5 sans-serif;
}
.box {
border: 4px solid blue;
color: blue;
padding: 1em;
}
@supports (row-gap: 10px) {
.box {
border: 4px dashed darkgreen;
color: red;
}
}
The value part of the property-value pair matters more if you are testing for new values of a particular property. All browsers support color: red
: this dates back to CSS1. However, there are often additional values added to properties in CSS, like relative colors, which may not be supported. Feature queries enable testing property and value pairs, meaning we can detect support for values.
Expanding on the color
property example above, here we check if the browser supports the color: AccentColor
declaration:
/* `@supports` at-rule */
@supports (color: AccentColor) {
CSS rules to apply
}
/* `supports()` function */
@import `/css/styles.css` supports(color: AccentColor);
In these examples, we've used feature queries to check if the user-agent supports a specific value of a CSS property, listing the single declaration within parenthesis. You can test for multiple property values or the lack of support.