CSS Feature Queries, introduced in CSS3, enable developers to apply styles based on the presence of certain CSS features in the browser. This allows for targeted styling for browsers that support specific features, enhancing compatibility and providing fallbacks for older browsers.
Feature queries start with '@supports'
followed by the condition inside parentheses.
@supports (display: grid) {
/* CSS code for browsers that support CSS Grid */
}
Use feature queries to check if a browser supports a particular CSS property or value before applying styles.
@supports (display: flex) {
/* CSS code for browsers that support Flexbox */
}
Combine multiple conditions within a feature query to target browsers that support a combination of CSS features.
@supports (display: grid) and (grid-template-columns: auto) {
/* CSS code for browsers that support CSS Grid with auto column sizing */
}
Use the 'not'
keyword to apply styles when a certain feature is not supported.
@supports not (display: grid) {
/* CSS code for browsers that do not support CSS Grid */
}
Feature queries can be nested within other feature queries to create more complex conditions.
@supports (display: grid) {
@supports (grid-template-rows: 1fr) {
/* CSS code for browsers that support CSS Grid with flexible row sizing */
}
}
Use logical operators (and
, or
, not
) to create more sophisticated conditions within feature queries.
@supports (display: grid) and (not (grid-auto-flow: dense)) {
/* CSS code for browsers that support CSS Grid without dense auto-placement */
}
/* Apply styles for browsers that support CSS Grid */
@supports (display: grid) {
.container {
display: grid;
grid-template-columns: 1fr 1fr;
}
}
/* Fallback for browsers that do not support CSS Grid */
@supports not (display: grid) {
.container {
float: left;
width: 50%;
}
}
CSS Feature Queries empower developers to create robust and adaptable stylesheets by selectively applying styles based on browser support for specific CSS features. By leveraging advanced techniques, developers can ensure graceful degradation in older browsers while embracing the full potential of modern CSS. Embracing CSS Feature Queries enables developers to craft more resilient and future-proof web experiences that cater to a diverse range of devices and browsers. Happy Coding! ❤️