CSS at-ruleAt-rules

@supports

Applies styles only in browsers that support a given property, value, or selector. It lets you use new features safely.

On this page

Example

#
CSS
.header {
  background: rgb(24 24 27 / 0.95);
}

@supports (backdrop-filter: blur(8px)) {
  .header {
    background: rgb(24 24 27 / 0.55);
    backdrop-filter: blur(8px);
  }
}
Preview

The bar is semi-transparent and blurs the tiles beneath it only where backdrop-filter works. A browser without this feature will show an almost opaque bar, so the text stays readable anyway.

Definition and usage

#

The @supports rule asks the browser whether it understands a given syntax. In parentheses you provide a declaration, e.g. (display: grid), and the browser checks whether it knows both the property and that value. If so, the styles in the block apply. If not, the whole block is skipped.

A typical pattern is progressive enhancement. Outside the rule you write a simple style that works everywhere, and in @supports you add the newer version. The opposite form, @supports not (…), holds a fallback only for browsers that do not know the feature.

You combine conditions with and and or, and each must be in its own parentheses. The selector() function checks selector support, e.g. selector(:has(img)). This matters because a single unknown selector in a list invalidates the whole rule, and in @supports you can prepare a replacement for it.

Remember that @supports only checks whether the browser recognizes the syntax, not whether the feature works without errors in every detail. You also do not need it for every new feature: the browser silently skips an unknown declaration anyway. The rule is useful only when other styles depend on the support.

Syntax

#
Syntax
@supports (<property>: <value>) {
  selector { property: value; }
}

@supports not (<condition>) { … }
@supports (<condition>) and (<condition>) { … }
@supports (<condition>) or (<condition>) { … }
@supports selector(<selector>) { … }

Kinds of conditions

#
  • (display: grid)

    a property and value

    Every modern browser knows grid, so the block applies and the tiles line up in three columns. Without it they would stay one below the other.
  • not (display: grid)

    no support

    The block applies only where grid is missing, that is, in very old browsers. Here it does not apply, so the tiles stay one below the other, as in the base style.
  • selector(:has(*))

    selector support

    Checks a selector instead of a declaration. Hover over a tile: the others fade. The rule with :has() works only in browsers that know this selector.
  • (…) and (…)

    both conditions at once

    The block applies only when the browser supports grid and aspect-ratio. The tiles line up in a row and become square.
  • (…) or (…)

    at least one condition

    One satisfied condition is enough. This is how you check prefixed properties: the glass blurs the tiles beneath it if the browser knows backdrop-filter in either version.

Kinds of conditions in practice

#

(display: grid)

Every modern browser knows grid, so the block applies and the tiles line up in three columns. Without it they would stay one below the other.
@supports (display: grid) {
  .demo {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
  }
}

not (display: grid)

The block applies only where grid is missing, that is, in very old browsers. Here it does not apply, so the tiles stay one below the other, as in the base style.
@supports not (display: grid) {
  .demo { flex-direction: row; }
}

selector(:has(*))

Checks a selector instead of a declaration. Hover over a tile: the others fade. The rule with :has() works only in browsers that know this selector.
@supports selector(:has(*)) {
  .demo:has(.box:hover) .box:not(:hover) {
    opacity: 0.3;
  }
}

(…) and (…)

The block applies only when the browser supports grid and aspect-ratio. The tiles line up in a row and become square.
@supports (display: grid) and (aspect-ratio: 1) {
  .demo {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
  }
  .demo .box { aspect-ratio: 1; }
}

(…) or (…)

One satisfied condition is enough. This is how you check prefixed properties: the glass blurs the tiles beneath it if the browser knows backdrop-filter in either version.
@supports (backdrop-filter: blur(6px))
  or (-webkit-backdrop-filter: blur(6px)) {
  .glass {
    background: rgb(255 255 255 / 0.12);
    -webkit-backdrop-filter: blur(6px);
    backdrop-filter: blur(6px);
  }
}

More examples

#
Container queries with a fallback
CSS
.card {
  display: grid;
  gap: 10px;
}

@supports (container-type: inline-size) {
  .slot {
    container-type: inline-size;
  }

  @container (width >= 300px) {
    .card {
      grid-template-columns: 80px 1fr;
      align-items: center;
    }
  }
}
Preview

The simplest way to check for container query support is through the container-type property, which arrived together with them. A browser without this feature will simply show a vertical card.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Write the base style outside the rule, and put in @supports only what requires the new feature. An older browser will get a simpler but working look.
  • You can check support for an at-rule through the property that came with it, e.g. @supports (container-type: inline-size) before using @container.
  • For prefixed properties, check both versions with or, e.g. (backdrop-filter: blur(4px)) or (-webkit-backdrop-filter: blur(4px)).

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.