Sub topic 7
Mixing Units and Nesting Functions
This chapter assumes you know the common length units (px, rem, em, %, vw), the box model, and the basic behavior of calc(), min(), max(), and clamp(). Here you will combine them. You will learn how the browser evaluates an expression that mixes units, how nested functions resolve, and which mistakes cause a declaration to be dropped.
Why mixing units is the point
Each length unit answers a different question:
| Unit | Resolves against | Typical use in a comparison function |
|---|---|---|
| `rem` | Root font size | Limits that respect user font settings |
| `em` | The element's own font size (or the parent's, for `font-size`) | Limits tied to local text size |
| `%` | A property-specific reference, often the containing block's width | Sizes relative to the parent |
| `vw` / `vh` | Viewport width / height | Growth that tracks the window |
| `px` | CSS pixels | Hairlines and fixed offsets |
A comparison function accepts arguments in different units. The browser does not need to convert them when you write the CSS. It resolves each argument to an absolute length at layout time, once it knows the root font size, the viewport, and the containing block. Then it compares the results.
This is why min(100%, 40rem) works. Neither value is known to be larger until layout. A percentage depends on the parent, and rem depends on the root font size.
Math inside comparison functions
Inside min(), max(), and clamp(), each argument is a math expression. You can add, subtract, multiply, and divide without wrapping the argument in calc().
.container {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}The first argument is 100% - 2rem. It is a complete expression, and no calc() is needed. 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 require whitespace, but adding it improves readability.- For
*, at least one side must be a plain number.2rem * 3is valid.2rem * 3remis not. - For
/, the right side must be a plain number.10rem / 2is valid.10rem / 2remis not, in most browsers. - Addition and subtraction require compatible types. You can add
remandvw, since both are lengths. You cannot add a length and a bare number:1rem + 2is invalid.
.valid {
/* Length + length, number multiplier */
width: min(50% + 2rem, 60rem * 0.9);
}
.invalid {
/* A length plus a unitless number makes the declaration invalid */
width: min(50% + 2, 60rem);
}A declaration that fails to parse is dropped at parse time. The browser then uses the inherited or initial value, or an earlier valid declaration for the same property, and it does not warn you.
Nesting functions
Comparison functions nest freely. You can place min(), max(), clamp(), and calc() inside each other, and parentheses group sub-expressions.
.panel {
/* Inner max() sets a floor on the gutter; outer min() caps the width */
width: min(100% - max(1rem, 4vw) * 2, 64rem);
}Read nested expressions from the inside out:
- Resolve
max(1rem, 4vw). Say the viewport is 500px wide, so4vwis 20px, and1remis 16px. The result is 20px. - Multiply by 2 to get 40px.
- Compute
100% - 40px. If the parent is 500px wide, the result is 460px. - Compare 460px with
64rem(1024px). The result is 460px.
At a viewport width of 1600px, 4vw is 64px. The gutter is 128px in total, and the first argument is 100% - 128px. The width is still capped at 1024px if the parent is wide enough.
flowchart TDThe diagram below shows the same evaluation order as a flow.
clamp() is shorthand for nesting
clamp(MIN, PREFERRED, MAX) equals max(MIN, min(PREFERRED, MAX)). Knowing this helps in two ways. You can write the long form when you want to see what is happening. You can also see why clamp() behaves oddly when MIN is larger than MAX. The outer max() wins, so the result is MIN.
h1 {
/* The MIN (3rem) is larger than the MAX (2rem), so the result is always 3rem */
font-size: clamp(3rem, 2.5vw, 2rem);
}Nesting with calc() is optional
Inside a comparison function, calc() is unnecessary. Outside one, you still need it. These two declarations are equivalent:
.a { width: min(100% - 2rem, 70rem); }
.b { width: min(calc(100% - 2rem), 70rem); }A bare parenthesis group also works inside a math function, for example min((100% - 2rem) / 2, 30rem).
Percentages and context
Percentages are the unit most affected by where you use them. The same expression can mean different things on different properties.
width: min(100%, 40rem)resolves100%against the containing block's width.padding-inline: max(1rem, 5%)also resolves against the containing block's width, even for vertical padding. Percentage padding always uses width.height: min(50%, 20rem)resolves against the containing block's height, and only if that height is definite. Otherwise the percentage behaves asauto, and the declaration may not do what you expect.font-size: clamp(1rem, 2%, 2rem)resolves2%against the parent's font size, which is rarely what you want for fluid type.
If an expression resolves to auto or none behavior because of an indefinite percentage, test it in the browser's inspector. The computed value shows what the browser used.
Units that need care
em inside font-size
On the font-size property, em refers to the parent's font size, not the element's own. On other properties, em refers to the element's own font size. Mixing em in a clamp on font-size can therefore compound through nested elements. Prefer rem for the limits and the preferred value of fluid type.
Viewport units on mobile
vh and vw are measured against the large viewport on some mobile browsers, so a dynamic toolbar can make vh-based values taller than the visible area. Newer units (svh, lvh, dvh) address this. For width-based fluid values, vw is usually fine. For height-based ones, check on a real device.
Unitless zero
In calc() and the comparison functions, a bare 0 is a number, not a length. max(0, 2vw - 1rem) is invalid because the arguments mix a number and a length. Write 0px instead. This is the most common reason for a dropped declaration in nested expressions.
.nudge {
/* Valid: 0px is a length */
margin-inline-start: max(0px, 2vw - 1rem);
}A worked example
Suppose you want a section whose horizontal padding is at least 1rem and grows with the viewport, and whose content never exceeds 60rem. You can combine all of the earlier ideas:
.section {
--gutter: clamp(1rem, 0.5rem + 2.5vw, 3rem);
padding-inline: var(--gutter);
max-width: calc(60rem + var(--gutter) * 2);
margin-inline: auto;
}Here clamp() supplies the gutter, and calc() with a custom property adds twice the gutter to the maximum width. The content box is therefore 60rem wide on large screens when the box-sizing is content-box. If you use box-sizing: border-box, max-width: 60rem already includes the padding, and the extra term is not needed. Choose based on which box model your stylesheet uses.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The declaration has no effect | Missing whitespace around `+` or `-` | Write `100% - 2rem` |
| The declaration has no effect | Unitless `0` mixed with lengths | Use `0px` or `0rem` |
| The declaration has no effect | Dividing by a length, or multiplying two lengths | Keep one side of `*` and the right side of `/` a number |
| `clamp()` returns the minimum at every width | `MIN` is larger than `MAX`, or the preferred value is too small | Check the argument order |
| Text size compounds in nested elements | `em` used inside `font-size` | Use `rem` |
| Percentage height does nothing | Parent height is not definite | Give the parent a height, or use another unit |
Exercise
What is wrong with width: min(100%-2rem, 70rem);, and what happens when the browser reads it? Rewrite it correctly.
Show answer
The - has no whitespace around it, so the parser reads 100%-2rem as an invalid token sequence instead of a subtraction. The browser drops the whole declaration, and the element uses its inherited or earlier value for width. The fixed version is width: min(100% - 2rem, 70rem);.
Exercise
Expand clamp(1rem, 3vw, 2rem) into max() and min(). What is the result at a viewport width of 1000px with a 16px root font size?
Show answer
It equals max(1rem, min(3vw, 2rem)). At 1000px, 3vw is 30px and 2rem is 32px, so min() gives 30px. Then max(16px, 30px) gives 30px.
Exercise
Why is margin-inline-start: max(0, 2vw - 1rem); invalid, and how do you fix it?
Show answer
A bare 0 is a number, and 2vw - 1rem is a length. Comparison functions need arguments of compatible types here, so the declaration is dropped. Write max(0px, 2vw - 1rem).