@supports (Feature Queries) — CSS

@supports (property: value) { ... } asks whether the browser understands a declaration, and what is inside is used only when it does. First write the fallback…

Not every browser understands every CSS feature, especially the new ones. With @supports, you ask the browser: "do you understand this?" If the answer is yes, the CSS inside @supports is used. If not, everything inside is skipped. It is like asking "is there a microwave?" before you decide how to heat your food. The microwave is the CSS feature, and the way you heat the food is the CSS you prepare for each answer.

Ask the browser before you use it

This code makes a box red, then green if the browser understands grid:

.box {
  background: red;
}

@supports (display: grid) {
  .box {
    background: green;
  }
}

The result: a browser that understands grid shows a green box. The rule inside @supports comes later, so it overrides the red rule. A browser that doesn't understand grid still shows a red box.

Fallback first, upgrade after

The most common pattern: first write the version that works in every browser. This version is called the fallback. Then write the nicer version (the upgrade) inside @supports. This gallery is an example:

.gallery {
  display: flex;
  flex-wrap: wrap;
}

@supports (display: grid) {
  .gallery {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
  }
}

Every visitor gets a gallery that works, with flexbox. Browsers that understand grid get the neater version: three columns of equal width. The order matters: fallback on top, @supports below, so the upgrade rule overrides the fallback.

Today every browser still in use understands grid, so this gallery is only a practice example. A more real example is a feature that is not in every browser yet, such as anchor positioning (attaching one element to another, like a small note to its button): @supports (anchor-name: --a). For one property, you don't need @supports: two lines one after the other are enough, like height: 100vh; then height: 100svh;. A browser that doesn't know 100svh skips the second line and keeps the first. @supports is useful when several rules must switch together.

What you can ask about

@supports or @media?

@media asks about the device: screen width, orientation, dark mode. @supports asks about the browser: which CSS features it understands. A wide screen doesn't mean the browser on it understands grid. So neither question can replace the other.

⚠️ Questions that get the wrong answer

💡 JavaScript can ask too. CSS.supports("display", "grid") asks the same question, and the answer is true (understood) or false (not understood).

TL;DR: @supports (property: value) { ... } asks whether the browser understands a declaration, and what is inside is used only when it does. First write the fallback (the version that works in every browser), then the upgrade (the nicer version) inside @supports. You can also ask about a selector with selector(...), and combine conditions with and, or and not. @media asks about the device (for example the screen width), while @supports asks about the CSS features the browser understands.

⚡ Ask the browser first

Write:

.box {
  background: red;
}

@supports (display: grid) {
  .box {
    background: green;
  }
}
Guess first: the CSS inside @supports is used when the browser understands the declaration in the parentheses. If you write @supports (gap), with no value, is the CSS inside used?

Never. With no value, what is in the parentheses is not a full declaration, so the answer is always no. Write it with a value: @supports (gap: 1rem).

Meaning: @supports asks the browser: "do you understand this?" What you ask about is one full declaration (property and value) in parentheses. A browser that understands display: grid uses the rule inside @supports, so the box is green. A browser that doesn't understand it skips what is inside, so the box stays red.

⚡ Fallback first, upgrade after

Write:

.gallery {
  display: flex;
  flex-wrap: wrap;
}

@supports (display: grid) {
  .gallery {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
  }
}

Meaning: First write the fallback: the version that works in every browser, here a gallery with flexbox. Then write the upgrade inside @supports: the neater version for browsers that understand grid. Fallback on top and @supports below, so the upgrade rule overrides the fallback.

⚡ Device or feature

Guess first: the screen is wide. Does that mean the browser understands grid?

Not necessarily. @media asks about the device, for example the screen width. @supports asks about the CSS features the browser understands.

Meaning: @media and @supports answer two different questions, so neither can replace the other.

What you will learn