Sub topic 5
Combining Conditions with not, and, or
This chapter assumes you can write a basic @supports (property: value) rule and know how source order and the cascade resolve conflicts. You should also be comfortable with @media conditions. Here you will learn how to combine several feature tests into one condition and how to avoid the syntax mistakes that make a condition silently fail.
Why one test is often not enough
A single @supports (display: grid) test answers one question. Real enhancements often depend on several things at once. A layout might need grid and gap. A frosted-glass panel needs either the unprefixed or the -webkit- prefixed form of a property. A workaround might apply only where something is missing.
CSS gives you three keywords for this:
| Keyword | Meaning | Operands |
|---|---|---|
| `and` | True only if every operand is true | Two or more |
| `or` | True if at least one operand is true | Two or more |
| `not` | True if the single operand is false | Exactly one |
Each operand is either a declaration test in parentheses, such as (display: grid), or a parenthesized group of other conditions.
Every test needs its own parentheses
The declaration inside @supports must always be wrapped in parentheses. This is true even when there is only one test.
/* Valid */
@supports (display: grid) {
.layout { display: grid; }
}
/* Invalid: the block is ignored */
@supports display: grid {
.layout { display: grid; }
}The keywords and, or and not must be separated from their operands by whitespace. not (display: grid) is valid. not(display: grid) is not, because the browser reads not( as a function name.
Using and
Use and when an enhancement relies on several features at the same time. Every operand must pass.
.toolbar {
display: block;
}
@supports (display: grid) and (gap: 1rem) {
.toolbar {
display: grid;
grid-auto-flow: column;
gap: 1rem;
}
}You can chain as many and operands as you like, as long as you do not mix in or at the same level:
@supports (display: grid) and (gap: 1rem) and (aspect-ratio: 1 / 1) {
.thumbnail-grid {
display: grid;
gap: 1rem;
}
.thumbnail-grid > img {
aspect-ratio: 1 / 1;
}
}Using or
Use or when there is more than one way to get the same result. The most common case is a vendor prefix. The panel example below uses the unprefixed and the -webkit- forms of backdrop-filter:
.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 baseline is a nearly opaque background that stays legible with no blur. The enhancement makes the panel translucent only where the blur can be drawn. If you made the panel translucent without the blur, text over a busy background would be hard to read. That is why the background change sits inside the @supports block next to the blur.
Inside the block, both property declarations are written. A browser that knows only one of them ignores the other as an invalid declaration, which is the cascade fallback behavior from earlier in this tutorial.
Using not
not inverts a single condition. It is useful when you need a workaround for the browsers that lack a feature:
@supports not (display: grid) {
.layout {
float: left;
width: 50%;
}
}not takes exactly one operand. That operand can be a group, which is how you negate a combination:
@supports not ((display: grid) and (gap: 1rem)) {
.layout {
display: block;
}
}The outer parentheses after not belong to the group. The inner ones belong to the individual tests. Without the group, not would apply to only the first test and the rest would be a syntax error, as the next section shows.
The parentheses rule for mixing operators
You can chain several and operands or several or operands without extra parentheses. You cannot mix and with or at the same level, and you cannot follow not with and or or. The browser will not guess which operator binds tighter, so you must group explicitly.
This condition is invalid:
/* Invalid: and and or mixed at one level */
@supports (display: grid) and (gap: 1rem) or (display: flex) {
.toolbar { display: flex; }
}Add parentheses to say what you mean. The following example groups the and pair first, then applies or:
@supports ((display: grid) and (gap: 1rem)) or (display: flex) {
.toolbar {
display: flex;
gap: 0.5rem;
}
}The other grouping gives a different meaning. This one requires grid, plus either gap or flex:
@supports (display: grid) and ((gap: 1rem) or (display: flex)) {
.toolbar {
display: grid;
}
}not follows the same rule. This condition is invalid:
/* Invalid: not followed by and */
@supports not (display: grid) and (gap: 1rem) {
.layout { float: left; }
}Both of the following are valid, and they mean different things:
/* Grid is missing, and gap is supported */
@supports (not (display: grid)) and (gap: 1rem) {
.layout { float: left; }
}
/* It is not true that both grid and gap are supported */
@supports not ((display: grid) and (gap: 1rem)) {
.layout { float: left; }
}How a condition is evaluated
The browser evaluates the nested groups from the inside out. Each declaration test asks whether the browser can parse the property and value. The operators then combine the true or false results.
Two details follow from this model. A condition that cannot be parsed counts as false, so a typo and a missing feature look the same from the outside. A test for something the browser has never heard of, such as (display: made-up), is also simply false, so it does no harm.
Tests check parsing, not behavior in context
A declaration test asks one question: does this browser accept this property with this value? It does not check whether the feature works in every situation.
The gap property shows the problem. Browsers supported gap for grid before they supported it for flex layouts. A test like @supports (gap: 1rem) was true in browsers where gap worked in grid but not yet in flexbox. A condition that combines (display: flex) and (gap: 1rem) does not fix that, because each test is evaluated on its own and not as a pair on one element.
When you rely on a feature only in a specific context, choose one of these approaches:
- Test a different property that exists only when the context works.
- Make the baseline acceptable without the feature, such as using margins instead of
gap. - Accept that a few older browsers get the baseline layout.
The third option fits the progressive enhancement idea best. The baseline should already be usable, so a missed enhancement costs polish and not function.
Choosing between not and a baseline plus enhancement
You can often write the same outcome two ways. The first writes a baseline for old browsers and a @supports block for new ones. The second writes a modern default and a @supports not block that patches old browsers.
/* Pattern 1: baseline, then enhance */
.layout { float: left; width: 50%; }
@supports (display: grid) {
.layout { float: none; width: auto; display: grid; }
}
/* Pattern 2: modern default, then patch */
.layout { display: grid; }
@supports not (display: grid) {
.layout { float: left; width: 50%; }
}Prefer pattern 1 when you can. Its default styles work everywhere, and a browser that does not understand @supports at all still gets a working page. Pattern 2 puts the modern code first. That makes it easier to remove the patch later, but the patch depends on the browser understanding @supports not. Browsers old enough to lack grid, such as Internet Explorer 11, do not understand @supports either, so the patch would never run for them. Pattern 2 is therefore weakest in exactly the browsers it is meant to help.
Debugging a condition that does not apply
Work through these steps in order:
- Check the parentheses. Every declaration test needs its own pair. Any mix of
andandor, or anynotfollowed by another operator, needs explicit grouping. - Check the spacing.
not (needs a space.andandorneed whitespace on both sides. - Test the condition in the console. Paste the condition into
CSS.supports()with the@supportskeyword and braces removed. A result offalsetells you the condition is invalid or unsupported. - Test each declaration alone. Break the condition into individual tests and find which one is false.
- Look at the cascade. If the condition is true but the style does not change, a more specific selector or a later rule may override it. The
@supportsblock adds no specificity of its own.
A console check looks like this:
const combined = '((display: grid) and (gap: 1rem)) or (display: flex)';
console.log(CSS.supports(combined));
// Break it apart to find the failing test
console.log(CSS.supports('display', 'grid'));
console.log(CSS.supports('gap', '1rem'));
console.log(CSS.supports('display', 'flex'));CSS.supports() accepts either a property and value pair or a full condition string. The condition string uses the same grammar as @supports, so a condition that is false there will be false in your stylesheet too. A later chapter covers this function in more detail.
Nesting and combining with other at-rules
@supports blocks can sit inside @media and the reverse, so you do not need to cram everything into one condition. The viewport test is best left to @media, and the feature test to @supports:
@media (min-width: 40rem) {
@supports (display: grid) {
.page {
display: grid;
grid-template-columns: 16rem 1fr;
}
}
}Keeping them separate lets each condition stay short and readable. It also avoids confusion, because @media and @supports have different syntax for their conditions even though both use and, or and not.
Summary of the rules
- Each declaration test is wrapped in its own parentheses.
andandorcan each be chained, but never mixed at the same level.nottakes one operand, which may be a parenthesized group.- Whitespace is required around the keywords.
- An invalid condition is false, and the block is skipped without any warning.
- A test checks that the browser can parse a declaration. It does not check that the feature works in every context.
Exercise
The following rule never applies in any browser, including ones that fully support grid and flexbox. Explain why, and rewrite it so it applies when the browser supports grid and either gap or column-gap.
@supports (display: grid) and (gap: 1rem) or (column-gap: 1rem) {
.list { display: grid; gap: 1rem; }
}
Show answer
The condition mixes and and or at one level without parentheses. The grammar does not define a precedence between them, so the condition is invalid and evaluates to false, and the block is skipped. Group the or operands: @supports (display: grid) and ((gap: 1rem) or (column-gap: 1rem)) { ... }.
Exercise
What is the difference between @supports (not (display: grid)) and (gap: 1rem) and @supports not ((display: grid) and (gap: 1rem))? Name one browser situation where the first is false and the second is true.
Show answer
The first is true only when grid is unsupported and gap is supported. The second is true whenever at least one of the two is unsupported. In a browser that supports grid but not gap, the first is false, because grid is supported, and the second is true, because gap is missing.
Exercise
Write a single @supports condition that is true when a browser supports backdrop-filter in either its unprefixed or -webkit- form, and also supports display: grid.
Show answer
@supports ((backdrop-filter: blur(8px)) or (-webkit-backdrop-filter: blur(8px))) and (display: grid) { ... }. The or pair is grouped in its own parentheses so that it can be combined with and.