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;
}
}
.box { background: red; }is outside@supports, so it applies in every browser.@supportsstarts the question.(display: grid)is what you ask about, called the condition: one full declaration, property and value, in parentheses (round brackets). The check is whether the browser understands that property and value together.- The
{ ... }after it is only used when the answer is yes. What goes inside is ordinary CSS.
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
- A declaration:
@supports (aspect-ratio: 1). The declaration must include its value. A typo matters too: the browser doesn't know(dispaly: grid), so the condition is false and what is inside@supportsis skipped. - A selector:
@supports selector(:has(*))asks whether the browser understands the:has()selector. not: what is inside@supports not (display: grid) { ... }is used only when the browser doesn't understanddisplay: grid.andandor:@supports (display: grid) and (gap: 1rem)means the browser must understand both. Withor, one of them is enough.
@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
- Writing only the property name. The answer to
@supports (gap)is always no, so what is inside is never used. Write it with a value:@supports (gap: 1rem). - Mixing
andandorwithout parentheses.(a) and (b) or (c)isn't valid. Group them first:(a) and ((b) or (c)). - Understood doesn't mean bug-free.
@supportsonly checks that the browser can read the declaration, not that the result is perfect. Still check the page in the browsers you want to support.
💡 JavaScript can ask too.
CSS.supports("display", "grid")asks the same question, and the answer istrue(understood) orfalse(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.