Sub topic 3
Fallbacks you already get from the cascade
What this chapter assumes
This chapter assumes you can read basic CSS (selectors, properties, values) and that you know how source order and specificity decide between two conflicting declarations. It also assumes you have used at-rules such as @media. The previous chapters introduced progressive enhancement and the reasons to detect features instead of browsers. This chapter shows the cheapest form of feature detection, which needs no special syntax.
The browser drops what it cannot parse
When a browser reads a stylesheet, it checks each declaration against the grammar it knows. If a declaration uses a property it has never heard of, or a value it cannot parse for that property, the browser discards that one declaration. It then carries on with the next declaration. It does not report an error, and it does not discard the rest of the rule.
This error handling is part of the CSS specification, and it is the reason CSS is forgiving. A stylesheet written today can be served to a browser from several years ago, and the old browser will skip the lines it does not understand.
The cascade does the rest of the work. If two declarations for the same property are in the same rule, the later one wins. If the later one was discarded at parse time, only the earlier one is left. That earlier declaration is your fallback.
The two-declaration pattern
The pattern is to write the older, widely supported declaration first and the newer one directly after it.
.card {
width: 300px;
width: min(100%, 300px);
}A browser that understands min() uses the second line, so the card never grows wider than its container. A browser that does not understand min() treats the second line as invalid and keeps width: 300px. Both browsers show a usable card, and you wrote no detection code.
The same pattern works for many other newer values.
.hero {
height: 100vh;
height: 100dvh;
}
.title {
font-size: 1.25rem;
font-size: clamp(1rem, 2.5vw, 1.5rem);
}
.badge {
color: #6b21a8;
color: oklch(45% 0.2 310);
}The dvh unit (dynamic viewport height) accounts for mobile browser toolbars that appear and disappear. A browser without dvh keeps the vh line. A browser without clamp() keeps the fixed size. A browser without oklch() keeps the hex color.
Order is the whole mechanism
The fallback must come first. Both declarations have the same specificity, so source order decides, and the later valid declaration wins. If you reverse the order, every browser reaches the older declaration last and uses it.
/* Wrong: modern browsers end up with 300px too */
.card {
width: min(100%, 300px);
width: 300px;
}This is the most common mistake with the pattern. Nothing breaks visibly in an old browser, and the modern browser quietly loses the enhancement. The result looks like a bug in the new feature, when the cause is the order of the lines.
When the fallback fails to apply
The cascade fallback only works when the browser can tell at parse time that the declaration is invalid. Several situations do not meet that condition.
The value is valid but not understood in context
A browser that knows display: grid but has a bug in one grid feature will parse the declaration successfully. The cascade cannot help, because nothing was dropped. For this kind of problem you need an explicit test, which the next chapter covers.
The declaration uses var()
A declaration that contains var() is accepted at parse time, because the browser cannot know what the variable holds until later. If the variable turns out to be undefined or holds an unusable value, the declaration becomes invalid at computed-value time. At that point the earlier declaration is no longer consulted. The property falls back to its inherited or initial value.
.box {
width: 300px;
width: var(--box-width);
}If --box-width is not defined, this .box does not get 300px. It gets width: auto, the initial value. To keep a fallback with variables, give var() its own second argument.
.box {
width: var(--box-width, 300px);
}The fallback needs several declarations to change together
Suppose a layout needs display: grid on the container and grid-column on each child. In a browser without grid, the invalid display: grid is dropped, but grid-column is also unknown and dropped. That case happens to work. A worse case is a legacy float layout on the children. A grid container ignores float on its children, but a browser that understands display: grid only partly may mix the two models. When a feature touches several properties, putting the fallback in one declaration is not enough. You want one block of rules that applies as a unit, and @supports provides it.
A selector is not understood
The cascade handles unknown declarations, but unknown selectors behave differently. If any selector in a comma-separated list is invalid, the browser discards the entire rule.
.card:has(img),
.card.has-image {
border-color: rebeccapurple;
}A browser without :has() drops this whole rule, including the .card.has-image part that it could have understood. Keep selectors that need a newer feature in their own rule, so they cannot take working selectors down with them. The selector() function, covered later in this tutorial, gives you a direct test.
A shorthand contains one bad part
A shorthand property is one declaration. If any part is invalid, the browser drops the whole declaration.
.banner {
background: url("banner.png") center / cover;
background: image-set("banner.avif" type("image/avif"), "banner.png" type("image/png")) center / cover;
}Here the second line may be dropped as a whole in an older browser, and the first line takes over. That is the behavior you want, but it also means a single unsupported keyword costs you the entire shorthand. If you only need to enhance one part, write the longhand property instead.
A worked example: a fluid card
The following stylesheet uses only cascade fallbacks. It runs as shown in any browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cascade fallbacks</title>
<style>
body {
margin: 0;
font-family: system-ui, sans-serif;
}
.hero {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
height: 100dvh;
background: #e9d5ff;
}
.card {
box-sizing: border-box;
width: 300px;
width: min(100%, 300px);
padding: 1.5rem;
background: #ffffff;
border: 1px solid #6b21a8;
border-color: oklch(45% 0.2 310);
}
.card h1 {
font-size: 1.25rem;
font-size: clamp(1rem, 2.5vw, 1.5rem);
margin: 0 0 0.5rem;
}
</style>
</head>
<body>
<div class="hero">
<article class="card">
<h1>Fluid card</h1>
<p>This card keeps a fixed width in older browsers and shrinks to fit narrow screens in newer ones.</p>
</article>
</div>
</body>
</html>To see the fallback at work, open the page in DevTools. Browsers that cannot parse a declaration show it struck through, often with a warning icon beside it. In a current browser you will see the opposite: the older declaration is struck through because the later one overrode it. Both outcomes are the cascade working as designed.
Choosing between a fallback pair and @supports
| Situation | Cascade fallback is enough | Use @supports |
|---|---|---|
| One property gets a newer value | Yes | Not needed |
| A newer unit or function such as `dvh` or `clamp()` | Yes | Not needed |
| Several properties must change together | No | Yes |
| The feature is a new selector such as `:has()` | No | Yes, with `selector()` |
| The old and new layouts need different rules, such as flex and grid | No | Yes |
| The value depends on `var()` | No | Use the `var()` fallback argument |
Prefer the cascade when it works. It has no extra syntax, nothing to maintain, and no way to disagree with itself. Move to @supports when you need a group of rules to apply together, or when the browser would parse your enhancement without being able to render it correctly.
Checking your work
- Write the fallback first, then the enhancement, in the same rule.
- Open DevTools in a current browser and confirm the enhancement is the active declaration.
- Temporarily misspell the enhanced value (for example
clampasclampp) and reload. The fallback should become the active declaration, and the page should still look acceptable. - Search for
var()in any declaration you meant as an enhancement, and check whether it needs its own fallback argument.
Exercise
A stylesheet contains .sidebar { width: min(100%, 20rem); width: 20rem; }. Users on current browsers report that the sidebar overflows narrow screens. What is wrong, and how do you fix it?
Show answer
The declarations are in the wrong order. Both have equal specificity, so the later one, width: 20rem, wins in every browser that parses it, which is all of them. The min() line is never used. Swap the lines so the fallback comes first: .sidebar { width: 20rem; width: min(100%, 20rem); }. Browsers without min() drop the second line and keep 20rem, and browsers with min() use it.
Exercise
You write .panel { margin: 1rem; margin: var(--gap); } and --gap is never defined anywhere. A teammate expects the panel to have a 1rem margin. What margin does it get, and why? Rewrite the rule so it works as the teammate expects.
Show answer
The panel gets the initial value, a margin of 0. The declaration with var() is valid at parse time, so it wins the cascade and the 1rem line is discarded. It becomes invalid at computed-value time, and the property then falls back to its initial value instead of the earlier declaration. The fix is to supply the fallback inside var(): .panel { margin: var(--gap, 1rem); }.
Exercise
The rule .tag:has(svg), .tag.with-icon { padding-left: 1.5rem; } styles icon tags. In a browser that does not support :has(), the .tag.with-icon tags are also unstyled. Why, and how would you restructure the CSS?
Show answer
An invalid selector in a comma-separated list invalidates the whole rule, so the browser drops the .tag.with-icon selector together with .tag:has(svg). Split them into two rules: .tag.with-icon { padding-left: 1.5rem; } and .tag:has(svg) { padding-left: 1.5rem; }. The first rule applies everywhere, and the second one is dropped only where :has() is unsupported.