Sub topic 13
Calculating the preferred value between two viewport widths
This chapter assumes you can read clamp(MIN, PREFERRED, MAX) and know that 1vw is 1% of the viewport width. You also need the rem unit, where 1rem is 16px unless the user changes it.
The problem
In clamp(), the minimum and maximum are easy to choose. The preferred value is harder. If you want a heading to be exactly 1rem at a 320px viewport and exactly 2rem at a 960px viewport, a guess like 2vw will not hit those points. You need a straight line through two points, and the preferred value is that line.
The line formula
A line has a slope and an intercept. In CSS terms:
- The slope is how much the size grows per unit of viewport width.
- The intercept is the size the line would have at a viewport width of zero.
| Step | Formula |
|---|---|
| Slope | (maxSize - minSize) / (maxViewport - minViewport) |
| Slope in `vw` | slope × 100 |
| Intercept | minSize - slope × minViewport |
| Preferred value | intercept + slope in `vw` |
Use the same unit, such as rem, for all sizes and viewports. Multiply the slope by 100 because 1vw equals 1% of the width, so a slope of 0.025 becomes 2.5vw.
Worked example
Scale an h2 from 1rem at a 20rem (320px) viewport to 2rem at a 60rem (960px) viewport.
- Slope: (2 - 1) / (60 - 20) = 0.025, which is
2.5vw. - Intercept: 1 - 0.025 × 20 = 0.5rem.
- Preferred value:
0.5rem + 2.5vw.
h2 {
font-size: clamp(1rem, 0.5rem + 2.5vw, 2rem);
}Check both ends. At 320px, 2.5vw is 8px, which is 0.5rem, so the total is 1rem. At 960px, 2.5vw is 24px, which is 1.5rem, so the total is 2rem. Both match the targets.
Why the clamp bounds matter
The line keeps rising past 960px and keeps falling below 320px. The clamp() minimum and maximum stop it at your two chosen sizes. Set them to the same two sizes you used to build the line.
A second example
Scale body copy from 1.25rem at 30rem (480px) to 3rem at 80rem (1280px).
- Slope: (3 - 1.25) / (80 - 30) = 0.035, which is
3.5vw. - Intercept: 1.25 - 0.035 × 30 = 0.2rem.
.hero-title {
font-size: clamp(1.25rem, 0.2rem + 3.5vw, 3rem);
}At 1280px, 3.5vw is 44.8px, which is 2.8rem. Adding 0.2rem gives 3rem.
Common mistakes
- Mixing units. If you compute the slope with
pxsizes andremviewports, the result is wrong. Convert everything toremfirst. - Forgetting the ×100. A slope of 0.025 written as
0.025vwis almost no growth. - Dropping the
remterm. A preferred value with onlyvwcannot be both exact at two points and zoom-friendly. Keeping the intercept inremlets the size respond to user font settings. - Assuming a 16px root. The
remand pixel values above hold only when the root font size is 16px. Thevwpart never changes with font settings, so the endpoints shift if a user changes the root size. That is a reasonable trade for accessibility.
Exercise
Write a clamp() for padding that is 1rem at a 24rem viewport and 3rem at a 64rem viewport.
Show answer
Slope = (3 - 1) / (64 - 24) = 0.05, which is 5vw. Intercept = 1 - 0.05 × 24 = -0.2rem. So use padding-block: clamp(1rem, 5vw - 0.2rem, 3rem). Inside clamp(), write it as calc-free math: 5vw - 0.2rem.