Sub topic 7
Mixing units and nesting functions
This chapter assumes you know min(), max() and clamp() from the previous chapters, and that you are comfortable with px, rem, % and vw. Here you will combine units inside one function and place functions inside each other.
Math without calc()
Inside min(), max() and clamp(), each argument is a math expression. You do not need to wrap it in calc().
.container {
width: min(100% - 2rem, 70rem);
}The browser evaluates 100% - 2rem first. It then picks the smaller of that result and 70rem. The same rules as calc() apply to the operators:
+and-must have whitespace on both sides.100%-2remis invalid, because the parser reads-2remas a negative number.*and/do not need whitespace, but spaces make the code easier to read.- In a multiplication, at least one side must be a plain number.
- In a division, the right side must be a plain number.
- You can add or subtract only values of the same type.
1rem + 2is invalid because it adds a length to a number.
Mixing units
Each argument can combine different units. The browser converts every term to a length once it knows the context, then compares the results. Percentages resolve against whatever the property uses as a reference. For width, that is the container's width. For font-size, it is the parent's font size.
.sidebar {
/* 25% of the container, but never under 12rem or over 20rem */
width: clamp(12rem, 25%, 20rem);
}
.hero {
/* Fixed header height subtracted from the viewport height */
min-height: max(20rem, 100vh - 4rem);
}The comparison happens at layout time, so one function can mix rem, % and vh without any conversion on your part.
Nesting functions
You can put a comparison function inside another one, or inside calc(). Parentheses group terms as they do in arithmetic.
.panel {
/* Equivalent to clamp(20rem, 50%, 40rem) */
width: max(20rem, min(50%, 40rem));
}
.gutter {
/* Side padding grows once the viewport is wider than 70rem */
padding-inline: max(1rem, (100% - 70rem) / 2);
}
h1 {
/* The maximum is itself a comparison */
font-size: clamp(1.5rem, 1rem + 2vw, min(3rem, 6vw));
}Evaluation goes from the innermost function outward. The diagram shows .panel in a 600px container with a 16px root font size.
The floor of 20rem wins here. In a 1000px container, the inner min() returns 500px, and the outer max() keeps it.
Common errors
A declaration with an invalid expression is dropped as a whole. The property then falls back to an earlier declaration or to its default value. Browsers do not report it as a partial error.
.broken {
width: min(100%-2rem, 70rem); /* invalid: missing whitespace around - */
padding: max(1rem + 2, 3rem); /* invalid: length plus number */
}Two habits keep nested expressions readable:
- Write one
clamp()for the common case. Use nestedmin()andmax()only when a bound itself needs to be a comparison. - Put a comment next to each nested expression saying what it does in plain words.
Exercise
A page has a 16px root font size and a 600px-wide viewport. What font size does font-size: clamp(16px, 2vw + 8px, 24px) produce? Also, why is width: min(100%-2rem, 70rem) invalid?
Show answer
2vw is 12px, so the preferred value is 12px + 8px = 20px. That lies between 16px and 24px, so the font size is 20px. The width declaration is invalid because + and - need whitespace on both sides. Without it, -2rem is parsed as a negative number, and the browser discards the declaration. The correct form is min(100% - 2rem, 70rem).