Sub topic 5
Combining conditions with not, and, or
This chapter assumes you can write a basic @supports (property: value) rule and know how the cascade resolves conflicting declarations. Here you will combine several tests in one @supports condition.
One test is often not enough
A single declaration test answers one question: does this browser understand this property and value? Real enhancements often depend on more than one thing. You may need two features together, either of two spellings of one feature, or a workaround for a missing feature. The keywords not, and and or let you express these cases.
Each declaration test goes in its own parentheses. The keywords join those groups.
| Keyword | Meaning | Shape |
|---|---|---|
| `not` | The condition is false | `not (display: grid)` |
| `and` | Every condition is true | `(display: grid) and (gap: 1rem)` |
| `or` | At least one condition is true | `(a: b) or (c: d)` |
or: accept more than one spelling
Use or when a feature exists under several names. Browsers that shipped backdrop-filter with a vendor prefix are the usual example.
.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);
-webkit-backdrop-filter: blur(8px);
backdrop-filter: blur(8px);
}
}The panel is nearly opaque by default, so text stays readable everywhere. It becomes translucent only where the blur is available. The enhancement never makes the baseline worse.
and: require several features together
Use and when the enhancement needs every feature to be present. For example, a layout that relies on grid and on the gap property in grid containers should check both.
@supports (display: grid) and (gap: 1rem) {
.toolbar {
display: grid;
grid-auto-flow: column;
gap: 0.5rem;
}
}If either test fails, the whole condition is false and the block is skipped.
not: target a missing feature
not applies a rule only when a feature is absent. It is useful for a targeted workaround.
@supports not (display: grid) {
.layout {
float: left;
width: 50%;
}
}This works, but it describes the old browser rather than the new one. In most cases a baseline plus an enhancement is clearer. The baseline is the plain styling that works everywhere. The enhancement is the @supports block that improves it. Reserve not for cases where the workaround must not apply to browsers that have the feature.
Mixing operators needs parentheses
You can chain several and tests, or several or tests, without extra parentheses. You cannot mix and with or at the same level. CSS does not define a precedence between them, so it requires you to say which grouping you mean.
@supports ((display: grid) and (gap: 1rem)) or (display: flex) {
.toolbar {
display: flex;
gap: 0.5rem;
}
}The inner group (display: grid) and (gap: 1rem) is wrapped in its own parentheses. The or then joins that group with (display: flex).
not follows the same rule. It takes exactly one parenthesized item, so combining it with and or or also needs parentheses. Write (not (display: grid)) and (display: flex). Do not write not (display: grid) and (display: flex).
What goes wrong when the condition is malformed
The browser does not report an error for a bad condition. It evaluates the condition as false and skips the block. The page looks as if the browser lacked the feature, even when it supports it.
/* Invalid: and and or are mixed without grouping */
@supports (display: grid) and (gap: 1rem) or (display: flex) {
.toolbar {
display: flex;
}
}Check these items first when a block never applies:
- Every declaration test is wrapped in its own parentheses.
andandorare not mixed at the same level.notis followed by a space and one parenthesized item.- The value is valid.
(gap: banana)is false everywhere.
Choosing between the three
- Use
orfor alternative spellings of one feature. - Use
andwhen the enhancement needs several features at once. - Use
notfor a workaround that must not reach browsers with the feature. - Prefer a baseline plus an enhancement over
notwhen both would work.
Exercise
A browser must support both display: grid and gap, or else support display: flex, before a rule applies. Why does @supports (display: grid) and (gap: 1rem) or (display: flex) fail, and how do you fix it?
Show answer
It mixes and with or at one level without parentheses, so the condition is invalid and the block is never applied. Group the and part: @supports ((display: grid) and (gap: 1rem)) or (display: flex).
Exercise
Write a condition that is true only when display: grid is unsupported and display: flex is supported.
Show answer
@supports (not (display: grid)) and (display: flex). The not group is wrapped in parentheses before it is joined with and.