Sub topic 4
Writing your first @supports rule
This chapter assumes you know basic CSS syntax, how source order and the cascade resolve conflicts, and how an at-rule such as @media wraps a block of rules. The previous chapters covered progressive enhancement and cascade fallbacks. Here you write the @supports rule that lets you apply an enhancement only where the browser can handle it.
What @supports does
@supports is a conditional group rule. It holds a condition and a block of ordinary CSS rules. The browser evaluates the condition once while it parses the stylesheet. If the condition is true, the rules inside behave as if they were written at the top level. If it is false, the browser skips the whole block.
It is the same shape as @media, but it asks a different question. @media asks about the environment, such as viewport width or color scheme. @supports asks about the browser itself: can it parse this declaration?
The basic syntax
The simplest condition is a single declaration in parentheses.
@supports (display: grid) {
.gallery {
display: grid;
}
}Each part has a job:
| Part | Meaning |
|---|---|
| `@supports` | The at-rule keyword. |
| `(display: grid)` | The condition. A property, a colon, and a value, all inside parentheses. |
| `{ ... }` | The block of normal rules. Selectors and declarations work as usual. |
Some details of the condition matter:
- The parentheses are required.
@supports display: grid { ... }is invalid, and the browser ignores the whole rule. - Write the declaration without a trailing semicolon. The parentheses hold one declaration, not a declaration list.
- Put a space between
@supportsand the opening parenthesis. Some browsers do not read@supports(as an at-rule followed by a condition. - The condition is not a selector and does not match elements. It is evaluated once, not per element.
What the browser actually tests
The browser does not render anything to answer the condition. It asks whether it recognizes the property and whether it accepts the value as valid. If both are true, the condition is true.
This has three consequences:
- A property the browser does not know is false.
- A known property with a value the browser cannot parse is also false.
(display: subgrid)is false in a browser that does not know that value, even though it knowsdisplay. - A true result means "I can parse this," not "I implement this without bugs." A browser can parse a feature and still render it badly.
@supportscannot detect that.
Because the test is at the value level, you can ask narrow questions. The condition (grid-template-columns: subgrid) tests for subgrid support specifically, which (display: grid) cannot tell you.
@supports (grid-template-columns: subgrid) {
.card-list > .card {
display: grid;
grid-template-rows: subgrid;
grid-row: span 3;
}
}Check the exact value you plan to use. A feature often arrives in stages, so a test on the property alone can be true while your value is still unsupported.
Baseline first, enhancement second
A condition only becomes useful once it sits on top of a working baseline. The pattern is:
- Write the layout or style that works in every browser you support.
- Add an
@supportsblock after it that upgrades the result.
Here is a complete page you can save as gallery.html and open in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Gallery</title>
<style>
.gallery {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.gallery > * {
flex: 1 1 12rem;
padding: 1rem;
background: #eef;
}
@supports (display: grid) {
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
}
}
</style>
</head>
<body>
<div class="gallery">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
</div>
</body>
</html>In a browser with grid support, the @supports block replaces display: flex with display: grid and adds the column definition. In a browser without it, the flexbox layout stays. Every visitor gets a usable gallery.
Notice what the enhancement does not do. It does not reset the flex properties on the children. In a grid container, flex: 1 1 12rem on the children has no effect, so it can stay. Leaving such leftover declarations in place keeps the enhancement short. When you do need to cancel something, say so explicitly inside the block.
The next diagram shows the decision the browser makes while it parses this stylesheet.
Why the order of rules matters
@supports does not add specificity and does not raise priority. Rules inside the block compete in the cascade exactly as if they were outside it. Two things follow.
First, put the @supports block after the baseline rule it overrides. If it comes first, a later baseline rule with the same selector wins, and your enhancement never shows.
/* Wrong order: the baseline overrides the enhancement */
@supports (display: grid) {
.gallery {
display: grid;
}
}
.gallery {
display: flex;
}In this stylesheet, .gallery always ends up as display: flex, even in browsers that support grid. Both rules have the same specificity, and the later one wins.
Second, a more specific selector outside the block beats a less specific one inside it. If the baseline uses .page .gallery and the enhancement uses .gallery, the baseline wins even though it came first. Keep selectors in the block equal to those in the baseline, or more specific.
Browsers that do not know @supports
@supports itself arrived in browsers long ago, but a few old browsers, notably Internet Explorer 11, do not implement it. Those browsers do not understand the at-rule, so they skip the whole block. The effect is the same as a false condition: the baseline remains.
That makes the baseline-first pattern safe in two ways. The browsers that cannot evaluate your condition and the browsers that evaluate it as false both end up with a working page.
The reverse pattern is not safe. Suppose you write a rule that applies a workaround only inside @supports not (display: grid). A browser that has no @supports support skips that block too, so it never gets the workaround it needs most. The not form appears later in this tutorial, and this limit is a reason to prefer baseline plus enhancement.
Testing a value, not just a property
The condition accepts any declaration the browser could parse, including complex values and functions. Test the form you will actually use.
.card {
width: 300px;
}
@supports (width: min(100%, 300px)) {
.card {
width: min(100%, 300px);
}
}This reads clearly and states the dependency. You will see later that, for this particular case, the cascade alone can do the same job without @supports. Use @supports when the fallback needs several declarations, when the enhancement changes more than one property, or when invalid-declaration fallbacks cannot express the logic.
A few more behaviors are worth knowing:
- Custom properties:
(--anything: value)is true wherever custom properties exist, because custom property values are not validated at parse time. Do not use it to test whether a custom value is valid. !important: you may write!importantin the condition, and the browser ignores it for the test.- Case and whitespace: property names are case-insensitive, and extra whitespace inside the parentheses is fine.
- Vendor prefixes: a prefixed property is tested like any other, so
(-webkit-appearance: none)is true only in browsers that recognize that exact name.
Quick checks in DevTools
When a rule does not apply, check the basics in this order:
- Open the Styles panel and look for your block. If the at-rule shows as struck out or missing, the condition failed or the syntax is invalid.
- Confirm the parentheses and the colon. Missing parentheses are the most common first-time error.
- Copy the declaration into an element's styles by hand. If DevTools marks it invalid, the browser cannot parse it, and the condition is false for that reason.
- Check source order and specificity against the baseline rule.
You can also test a condition from the console. CSS.supports('display: grid') returns true or false. A later chapter covers this API in detail.
Putting it together
A good first @supports rule has this shape:
.component {
/* 1. Baseline: works in every supported browser */
}
@supports (property: value) {
.component {
/* 2. Enhancement: only where the property and value parse */
}
}Keep these rules in mind:
- Write the baseline first, outside any condition.
- Place the
@supportsblock after it, using a selector of the same specificity. - Test the exact value you use.
- Keep the block small. Override only what the enhancement changes.
Exercise
A stylesheet contains the following. In a browser that supports grid, the .gallery is still laid out with flexbox. Give two possible reasons and one fix for each.
.gallery { display: flex; }
@supports display: grid {
.gallery { display: grid; }
}
Show answer
The first reason is syntax. The condition has no parentheses around the declaration, so the rule is invalid and the browser ignores the whole block. The fix is to write @supports (display: grid). The second reason is order and specificity. If a later rule such as .page .gallery { display: flex; } appears after the block, or the baseline uses a more specific selector than the block, the baseline wins in the cascade. The fix is to place the @supports block after the baseline and use a selector with the same or higher specificity. Once the parentheses are added, the @supports block already follows the baseline in this snippet, so the missing parentheses alone explain the failure.
Exercise
Why is @supports (display: grid) not enough to decide whether it is safe to use grid-template-rows: subgrid? Write the condition you should use instead.
Show answer
A browser can support display: grid and still not understand the subgrid value, because the value arrived later than the property. The condition only reports whether the tested declaration parses. Test the exact value you plan to use: @supports (grid-template-rows: subgrid).