Sub topic 2
Writing Your First `@supports` Rule
Assume you know CSS selectors, declarations, the cascade, and basic at-rules such as @media. @supports is a conditional group rule. It applies its declarations only when the browser understands the tested feature.
Testing a property and value
Write a working baseline first. Then place the enhancement inside @supports:
.gallery {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
@supports (display: grid) {
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
}
}A browser that supports display: grid uses the second rule. Other browsers keep the flexbox layout. The fallback remains useful instead of being replaced by an unsupported feature.
The condition tests a property and value pair. Keep both inside parentheses:
@supports (backdrop-filter: blur(8px)) {
.panel {
backdrop-filter: blur(8px);
}
}Combining conditions with `not`, `and`, `or`
Use and when every condition must pass. Use or when at least one condition may pass. Use not when a feature must be missing.
@supports ((display: grid) and (gap: 1rem)) or (display: flex) {
.toolbar {
display: flex;
gap: 0.5rem;
}
}
@supports not (display: grid) {
.layout {
display: block;
}
}Parenthesize each condition when mixing operators. Without the inner parentheses, the whole condition can become invalid and the block will not apply.
Testing selector support with `selector()`
A property test does not test whether a selector is understood. Use the selector() function for selectors such as :has():
.card {
border: 1px solid #ccc;
}
@supports selector(:has(img)) {
.card:has(img) {
border-color: rebeccapurple;
}
}The baseline rule still styles every card. The enhanced rule runs only where the browser can parse the selector.
Practical enhancement patterns
Use this structure for most enhancements:
- Create a usable baseline.
- Test the exact feature you need.
- Override or extend the baseline inside
@supports. - Test both branches in browsers or development tools.
.panel {
background: rgb(255 255 255 / 0.95);
}
@supports (backdrop-filter: blur(8px)) or
(-webkit-backdrop-filter: blur(8px)) {
.panel {
background: rgb(255 255 255 / 0.6);
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
}
}Exercise
Write a rule that uses a solid white .modal background by default and a translucent background only when backdrop-filter: blur(6px) is supported.
Show answer
Use the solid background first, then override it in @supports (backdrop-filter: blur(6px)).