How work @supports role in CSS?

How work @supports role in CSS?

Published on Updated on

CSS @supports is a conditional rule that allows developers to check if a particular CSS feature is supported by the browser before applying the related CSS styles. This is useful for creating fallbacks or alternate styles when a browser does not support a particular feature.

Here's an example of how to use `@supports` to check for the support of the CSS `display: grid` property:

@supports (display: grid) {
  /* Styles for browsers that support CSS Grid */
  .container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-gap: 20px;
  }
}

/* Fallback styles for browsers that don't support CSS Grid */
.container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

The `@supports` rule can also be used with more complex expressions, such as logical operators and feature queries. For example, you can use the following syntax to check if a browser supports both the `display: grid` property and the `gap` property, which sets the gap between grid items:

In this example, we're using `@supports` to check if the `display: grid` property is supported by the browser. If it is, we apply some grid-specific styles to a `.container` class. If it's not supported, we apply some fallback styles using the `display: flex` property.

@supports (display: grid) and (gap: 20px) {
  /* Styles for browsers that support CSS Grid with the gap property */
  .container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
  }
}

By using `@supports` to conditionally apply CSS styles, you can create more robust and resilient web designs that work across a range of browsers and devices.

Updated on