Sub topic 15
Pitfalls, debugging, and fallbacks
This chapter assumes you know min(), max(), and clamp(), the length units px, rem, em, %, and vw, and basic CSS Grid. It covers the mistakes that show up most often and how to find and handle them.
Common pitfalls
Missing units and operator spacing
Inside comparison functions, every number that is a length needs a unit. A bare 0 also fails in some contexts, so write 0px when in doubt. Operators + and - need whitespace on both sides. * and / do not, but spacing them keeps the code readable.
.bad {
width: min(100%-2rem, 70rem); /* invalid: "100%-2rem" is not parsed as subtraction */
}
.good {
width: min(100% - 2rem, 70rem); /* valid */
}When a declaration is invalid, the browser drops it. The property then falls back to its inherited or initial value, with no error message.
Swapped arguments in clamp()
The order is clamp(MIN, PREFERRED, MAX). If MIN is larger than MAX, the MIN value wins. A common slip is swapping the preferred value and the maximum, which makes the font size stuck at one value.
h1 {
font-size: clamp(1.5rem, 3rem, 1rem + 2vw); /* wrong: the preferred value is a fixed 3rem */
}Percentages that resolve against the wrong box
A percentage in width refers to the container's width. In font-size, it refers to the parent's font size. In padding, both vertical and horizontal percentages refer to the container's width. Check which property you are in before you trust a % term.
Debugging
- Open the browser's developer tools and select the element.
- In the Styles panel, look for a declaration that is struck through or marked with a warning icon. That means the browser rejected it.
- Open the Computed panel to see the final value in pixels. Resize the window and watch it change.
- Compare the computed value with the three arguments by hand. If the result equals MIN or MAX over a wide range of widths, the preferred value is not doing any work.
/* Temporary debugging aid: make the three values visible */
h1 {
--min: 1.5rem;
--pref: 1rem + 2vw;
--max: 3rem;
font-size: clamp(var(--min), var(--pref), var(--max));
outline: 1px solid red;
}Note that var(--pref) here would not be valid inside clamp() on its own, because the custom property holds the text 1rem + 2vw. Custom properties are substituted as text, so this particular case does work in modern browsers. Keep the pieces simple, though, and test in the Computed panel.
Fallbacks for older browsers
min(), max(), and clamp() are supported in all current browsers. If you must support an older one, declare a plain value first. A browser that does not understand the function drops the second declaration and keeps the first.
h1 {
font-size: 2rem; /* fallback */
font-size: clamp(1.5rem, 1rem + 2vw, 3rem);
}For a whole block of rules, use @supports:
.container {
width: 90%;
max-width: 70rem;
margin-inline: auto;
}
@supports (width: min(100%, 10px)) {
.container {
width: min(100% - 2rem, 70rem);
max-width: none;
}
}Quick checklist
| Symptom | Likely cause |
|---|---|
| Declaration ignored | Missing unit or missing spaces around `+` or `-` |
| Size never changes | Preferred value is a fixed length, or MIN and MAX are swapped |
| Text does not scale with zoom | Preferred value uses only `vw` |
| Grid overflows on narrow screens | `minmax()` minimum is not guarded with `min(100%, ...)` |
Exercise
This rule has no effect on the heading's size: h1 { font-size: clamp(2rem, 1rem+2vw, 4rem); }. What is wrong, and how do you fix it?
Show answer
The + has no whitespace around it, so 1rem+2vw is not parsed as addition and the whole declaration is invalid. Fix it with spaces: clamp(2rem, 1rem + 2vw, 4rem).