Sub topic 15
Pitfalls, debugging, and fallbacks
This chapter covers the mistakes that most often break min(), max(), and clamp() in real stylesheets, how to find them, and how to keep older or unusual environments working. It assumes you have read the earlier chapters. You should be comfortable with calc(), with clamp(MIN, PREFERRED, MAX), and with the fluid container, type, and grid patterns built so far.
Syntax errors that silently drop a declaration
CSS does not report errors. When a declaration fails to parse, the browser discards it and the cascade continues as if you had never written it. Most comparison-function bugs show up as "my rule has no effect."
Whitespace around + and -
Inside min(), max(), clamp(), and calc(), the + and - operators must have whitespace on both sides. Without it, the parser reads -2rem as a single negative number.
.bad { width: min(100%-2rem, 70rem); } /* invalid: declaration is dropped */
.good { width: min(100% - 2rem, 70rem); } /* valid */The * and / operators do not need spaces, but adding them keeps expressions readable.
Multiplying and dividing units
At least one side of * must be a plain number, and the right side of / must be a number. Multiplying two lengths is meaningless and invalid.
.ok { padding: calc(1rem * 1.5); }
.bad { padding: calc(1rem * 2vw); } /* invalid: length times length */Unitless zero inside math
Outside math, 0 can stand in for a length. Inside a math function, a bare 0 is a number, not a length, so it cannot be added to a length. Write 0px or 0rem when you need a zero length in an expression.
Logic errors that parse fine
The minimum wins when MIN is larger than MAX
clamp(MIN, PREFERRED, MAX) is defined as max(MIN, min(PREFERRED, MAX)). If you swap the bounds by accident, the outer max() picks the larger value, which is your "minimum."
h1 {
/* MIN (3rem) is larger than MAX (2rem): always resolves to 3rem */
font-size: clamp(3rem, 1rem + 2vw, 2rem);
}There is no error and no warning. If a fluid value never changes, check the order of the arguments first.
min() and max() feel backwards
min() is a ceiling and max() is a floor. People often reach for max() when they want a maximum width. Say the goal aloud: "never wider than 40rem" needs the smaller of the two values, so use min(100%, 40rem).
Percentages resolve against different things
A percentage inside a comparison function resolves exactly as it would outside it. The reference changes by property:
| Property | Percentage is relative to |
|---|---|
| `width`, `max-width`, `margin`, `padding` | The width of the containing block |
| `font-size` | The parent element's font size |
| `height` | The containing block's height, which is ignored if that height is `auto` |
So padding-block: min(5%, 3rem) scales with the container's width, not its height. If a height-based percentage does nothing, the parent has no definite height.
Viewport units and scrollbars
100vw includes the width of a vertical scrollbar. On systems with classic scrollbars, width: 100vw is wider than the space available and causes horizontal scrolling. Prefer 100% for widths that should fill the parent. Keep vw for the preferred term inside clamp(), where a few pixels of error do not matter.
Accessibility regressions
Fluid type fails accessibility checks when the preferred value uses only vw. Browser zoom changes the viewport width in CSS pixels, so a vw-only size can fail to grow when the user zooms. Always include a rem term, as in clamp(1rem, 0.5rem + 3vw, 2rem).
Also keep the ratio between the maximum and minimum modest. As a rule of thumb, a maximum of about 2.5 times the minimum is more likely to pass the WCAG requirement that text can be enlarged to 200 percent. Test these cases:
- Zoom the page to 200 percent and confirm that the text enlarges.
- Raise the browser's default font size (not only page zoom) and confirm that the layout follows.
- Check that nothing overlaps or scrolls sideways at a 320px-wide viewport.
Debugging workflow
The browser's developer tools show the resolved result. Open the Computed pane and read the final pixel value of width, font-size, or padding. Resize the window slowly. A value that never changes points to a clamp at its bound or to swapped arguments. A rule that is missing from the Styles pane, or shown with a strike-through or warning icon, points to a parse error.
A quick trick for isolating a term is to temporarily replace a complex expression with its parts. If clamp(1rem, 0.5rem + 3vw, 2rem) misbehaves, set font-size: calc(0.5rem + 3vw) alone and watch the computed value. Then add the bounds back.
Fallbacks for unsupported browsers
min(), max(), and clamp() are supported in all current major browsers. A fallback matters only if you must support very old engines or embedded browsers.
The two-declaration pattern
Write a plain value first, then the function value. A browser that cannot parse the second declaration drops it and keeps the first.
.title {
font-size: 1.5rem; /* fallback */
font-size: clamp(1.5rem, 1rem + 2vw, 3rem); /* used if supported */
}Why this fails with custom properties
When a declaration uses var(), the browser cannot check the final value at parse time. If the variable then holds something the browser does not understand, the declaration becomes invalid at computed-value time. The earlier fallback declaration is not used. For an inherited property such as font-size, the value is inherited instead.
:root { --size: clamp(1.5rem, 1rem + 2vw, 3rem); }
.title {
font-size: 1.5rem;
font-size: var(--size); /* fallback is NOT used if clamp() is unsupported */
}Gate the variable itself with @supports instead:
:root {
--size: 1.5rem;
}
@supports (font-size: clamp(1rem, 1vw, 2rem)) {
:root {
--size: clamp(1.5rem, 1rem + 2vw, 3rem);
}
}
h1 { font-size: var(--size); }Summary of common failures
| Symptom | Likely cause | Fix |
|---|---|---|
| Rule has no effect | Missing spaces around `+` or `-`, or length times length | Add spaces, multiply by a plain number |
| Value never changes | `MIN` larger than `MAX`, or the viewport range never reaches the preferred value | Reorder the arguments, recompute the slope |
| Horizontal scrollbar | `100vw` width, or a fixed minimum wider than the container | Use `100%`, or `min(100%, 16rem)` |
| Text ignores zoom | Preferred value uses only `vw` | Add a `rem` term |
| Fallback ignored | Function hidden behind `var()` | Define the variable inside `@supports` |
Exercise
The following rule has no effect in any browser. Give the two problems and a corrected version. .card { width: min(100%-3rem, 2rem * 20rem); }
Show answer
There are two parse errors. First, 100%-3rem has no spaces around the minus sign, so it is read as the percentage 100% followed by the dimension -3rem rather than a subtraction. Second, 2rem * 20rem multiplies two lengths, which is invalid. A corrected version is width: min(100% - 3rem, 40rem);.
Exercise
A heading uses font-size: clamp(2.5rem, 1rem + 4vw, 2rem); and stays at one size at every viewport width. Why, and what is the value?
Show answer
The minimum (2.5rem) is larger than the maximum (2rem). Because clamp() equals max(MIN, min(PREFERRED, MAX)), the inner min() never exceeds 2rem and the outer max() then returns 2.5rem. The size is always 2.5rem. Swap the bounds to clamp(2rem, 1rem + 4vw, 2.5rem).
Exercise
You store --gap: clamp(1rem, 2vw, 2rem); in :root and write gap: 1rem; gap: var(--gap); as a fallback. Why is the fallback unreliable, and how do you fix it?
Show answer
A declaration containing var() is accepted at parse time, so the earlier gap: 1rem is overridden even if clamp() turns out to be unsupported. In that case the declaration becomes invalid at computed-value time and the fallback is lost. Define --gap: 1rem; in :root, then redefine it with clamp() inside @supports (width: clamp(1px, 1vw, 2px)).