Sub topic 13
Calculating the preferred value between two viewport widths
This chapter covers how to derive the middle argument of clamp() so that a value hits an exact minimum at one viewport width and an exact maximum at another. It assumes you know clamp(), calc() rules for mixing units, and the relationship between rem, px, and vw. It also assumes a 16px root font size unless stated otherwise.
The problem with guessing
In earlier chapters you wrote values such as clamp(1rem, 0.5rem + 2.5vw, 2rem). The numbers in the middle argument look arbitrary. If you pick them by feel, the type stops growing at some unpredictable viewport width. The size also reaches its maximum at a width you did not choose.
A better approach is to decide two things first:
- The size you want at a narrow viewport, and the width of that viewport.
- The size you want at a wide viewport, and the width of that viewport.
The preferred value is then a straight line through those two points. You calculate its slope and intercept, and write them as a vw term plus a rem term.
The model: a line between two points
Between the two viewport widths, the size changes at a constant rate. That is linear interpolation. The preferred value has the form:
preferred = intercept + slope * viewport-widthIn CSS, 1vw is 1% of the viewport width. So a slope expressed per unit of width becomes a vw coefficient after multiplying by 100.
Define these values, all in the same unit (use rem throughout):
minSizeandmaxSize: the sizes you want at the two ends.minVwandmaxVw: the viewport widths at the two ends.
The formulas are:
slope = (maxSize - minSize) / (maxVw - minVw)
intercept = minSize - slope * minVwThe slope is dimensionless, because the size and width are both lengths in rem. To write it in CSS, multiply it by 100 to get the vw coefficient.
preferred = intercept(rem) + (slope * 100)vwThe clamp() then holds the result between the two sizes outside the interpolation range.
A worked example
Suppose you want an h2 that is 1rem at a 20rem (320px) viewport and 2rem at a 60rem (960px) viewport.
- Slope:
(2 - 1) / (60 - 20) = 1 / 40 = 0.025. - Coefficient in
vw:0.025 * 100 = 2.5vw. - Intercept:
1 - 0.025 * 20 = 0.5rem. - Preferred value:
0.5rem + 2.5vw.
/* Scale from 1rem at a 20rem (320px) viewport to 2rem at a 60rem (960px) viewport.
slope = (2 - 1) / (60 - 20) = 0.025 -> 2.5vw
intercept = 1 - 0.025 * 20 = 0.5rem */
h2 {
font-size: clamp(1rem, 0.5rem + 2.5vw, 2rem);
}Check the result by substituting both widths:
| Viewport | `2.5vw` | Plus `0.5rem` | Result |
|---|---|---|---|
| 320px | 8px | 8px | 16px = `1rem` |
| 640px | 16px | 8px | 24px = `1.5rem` |
| 960px | 24px | 8px | 32px = `2rem` |
At 640px the viewport is halfway between the two endpoints, and the size is halfway between 1rem and 2rem. That is what a line should give you.
Outside the range, the clamp takes over. At 200px the preferred value is 5px + 8px = 13px, which is below 1rem, so the minimum wins. At 1200px the preferred value is 30px + 8px = 38px, which is above 2rem (32px), so the maximum wins.
Another example with different units
You do not have to work in rem only. Say you think in pixels: 18px at 375px and 24px at 1200px. Convert everything to rem by dividing by 16, or work in pixels and convert at the end.
Working in pixels:
- Slope:
(24 - 18) / (1200 - 375) = 6 / 825 ≈ 0.007273. - Coefficient:
0.007273 * 100 ≈ 0.7273vw. - Intercept:
18 - 0.007273 * 375 ≈ 15.273px, which is15.273 / 16 ≈ 0.9545rem.
p.lead {
font-size: clamp(1.125rem, 0.9545rem + 0.7273vw, 1.5rem);
}The minimum is 18 / 16 = 1.125rem and the maximum is 24 / 16 = 1.5rem. The rounded numbers mean the curve will be off by a fraction of a pixel at the ends. That is not visible, and it is normal to round to three or four decimal places.
Checking your work
Interpolation errors are easy to make. These checks catch most of them.
- Substitute
minVwandmaxVwinto your preferred value. The results should equalminSizeandmaxSize. - Confirm the
remterm is the intercept and not a copy ofminSize. They are only equal whenminVwis zero. - Confirm that the
vwcoefficient is the slope multiplied by 100. If your type grows far too fast or too slowly, you probably forgot the factor of 100. - Confirm that the first argument of
clamp()is smaller than the last. If you invert them,clamp()returns the minimum.
Calculating in the stylesheet itself
CSS can do the arithmetic for you. Because calc() supports division by a number, you can express the formula with custom properties. The unit rules matter here. Dividing by a length is not allowed in older browsers, so the expression divides by a unitless number.
:root {
--min-size: 1; /* rem */
--max-size: 2; /* rem */
--min-vw: 20; /* rem */
--max-vw: 60; /* rem */
}
h2 {
font-size: clamp(
calc(var(--min-size) * 1rem),
calc(
(var(--min-size) - (var(--max-size) - var(--min-size)) / (var(--max-vw) - var(--min-vw)) * var(--min-vw)) * 1rem
+ (var(--max-size) - var(--min-size)) / (var(--max-vw) - var(--min-vw)) * 100vw
),
calc(var(--max-size) * 1rem)
);
}This keeps the inputs readable, and it stays correct when you change them. The cost is a long expression that is harder to scan. Many teams calculate the numbers once with a script or a small calculator and paste the result, which also makes the final CSS easier to read.
Edge cases
- Decreasing sizes. If
maxSizeis smaller thanminSize, the slope is negative. This works, but the first and last arguments ofclamp()must still be ordered with the smaller value first. Writeclamp(smaller, preferred, larger)and let the negativevwcoefficient do the work. - Negative intercept. A steep slope can give a negative
remterm, for example-1rem + 5vw. Write it ascalc(5vw - 1rem)or-1rem + 5vw. Both are valid. Keep the spaces around+and-, becausecalc()requires whitespace around those two operators. - The scrollbar.
100vwincludes the width of a classic scrollbar. On a desktop with a visible scrollbar, the content area is narrower than100vw, so the endpoints are reached a little later than calculated. For type this is usually unnoticeable. - Container-relative widths. The same formula works with container query units such as
cqiwhen you want a component to scale with its container instead of the viewport. Replacevwwithcqiand use container widths forminVwandmaxVw. - Mixed ranges. One interpolation covers one range. If you need different slopes in different ranges, you need either media queries or nested
min()andmax()calls that approximate a piecewise line. In practice, most designs use one range.
Keeping the result accessible
The rem term is the reason this form is preferred over a viewport-only value. When the user zooms the page or raises the default font size, the rem part and the clamp limits grow, so the text responds. A value like clamp(1rem, 4vw, 2rem) has a preferred value that ignores the font setting entirely.
Also check the maximum against the minimum. A common guideline is that the largest size should be no more than about 2.5 times the smallest. A larger ratio can make zoomed text fail to scale as users expect.
Exercises
Exercise
You want a font-size of 1.25rem at a 24rem viewport and 2.25rem at a 64rem viewport. Write the full clamp() declaration.
Show answer
Slope = (2.25 - 1.25) / (64 - 24) = 1 / 40 = 0.025, so the vw coefficient is 2.5vw. Intercept = 1.25 - 0.025 * 24 = 0.65rem. The declaration is font-size: clamp(1.25rem, 0.65rem + 2.5vw, 2.25rem);. Check: at 24rem (384px), 2.5vw is 9.6px and 0.65rem is 10.4px, which totals 20px = 1.25rem.
Exercise
A teammate wrote font-size: clamp(1rem, 1rem + 2.5vw, 2rem); intending 1rem at 20rem and 2rem at 60rem. At a 20rem (320px) viewport, what size does it produce, and what is wrong?
Show answer
At 320px, 2.5vw is 8px, so the preferred value is 16px + 8px = 24px, which is 1.5rem. It should be 1rem. The rem term was set to the minimum size instead of the intercept (0.5rem). The correct value is clamp(1rem, 0.5rem + 2.5vw, 2rem). With the wrong intercept the text reaches the 2rem maximum at 40rem instead of 60rem.