Sub topic 9
Fluid sizing with clamp(), min(), and max()
This chapter assumes you know the box model, the difference between px, rem, and em, and how viewport units such as vw work. Here you will combine those units with three math functions so that sizes grow and shrink with the screen, within limits you choose.
The problem with pure viewport units
A size such as font-size: 5vw scales smoothly, but it has no limits. On a 320px phone the text is 16px. On a 2000px monitor it is 100px. A viewport unit also ignores the user's browser font setting, so people who raise their default text size see no change.
The functions min(), max(), and clamp() fix both problems. They pick a value from a list, and they accept any length unit, including mixed ones.
min() and max()
min() returns the smallest value from its arguments. max() returns the largest. Both are resolved by the browser when the page is laid out, so they respond to resizing.
.container {
width: min(90%, 60rem);
}
.sidebar {
width: max(12rem, 25%);
}Read min(90%, 60rem) as "90% wide, but never wider than 60rem". It acts as a ceiling. Read max(12rem, 25%) as "25% wide, but never narrower than 12rem". It acts as a floor.
clamp(): floor, preferred value, ceiling
clamp(minimum, preferred, maximum) combines both ideas. The browser uses the preferred value unless it falls below the minimum or above the maximum.
h1 {
font-size: clamp(1.75rem, 1rem + 3vw, 3rem);
}For this heading:
| Viewport width | Preferred value (1rem + 3vw) | Result |
|---|---|---|
| 320px | 16px + 9.6px = 25.6px | 28px (minimum, 1.75rem) |
| 600px | 16px + 18px = 34px | 34px |
| 1200px | 16px + 36px = 52px | 48px (maximum, 3rem) |
These figures assume the default root font size of 16px.
Why the preferred value mixes rem and vw
The preferred value 1rem + 3vw is deliberate. The 3vw part supplies the growth. The 1rem part keeps a connection to the user's font setting. If a user raises their browser default to 24px, the base grows with it, and the text stays larger than it would with vw alone. Using calc() is not needed inside these functions. You can write the addition directly.
You can pick the numbers by deciding two points. For example, you want 28px at 320px wide and 48px at 1200px wide. The slope is (48 − 28) / (1200 − 320), about 0.0227 px per px of width, which is roughly 2.27vw. Tune from there by eye.
Common errors
- Mixing up the order.
clamp(3rem, 2vw, 1.75rem)has a minimum above the maximum. The minimum wins, so the size is always 3rem. - Using only
vwin the preferred value. This ignores user font settings. Add aremterm. - Missing spaces around
+and-. Inside math functions,1rem+3vwis invalid. Write1rem + 3vw. - Using fluid type for everything. Body text usually works best at a fixed
1rem. Reserveclamp()for headings and large spacing values. - Percentages in font-size. A percentage in
font-sizerefers to the parent's font size, not the container width. Usevwfor width-based scaling.
Using clamp() beyond fonts
Any length property accepts these functions. Fluid padding is a common use.
.section {
padding-block: clamp(1.5rem, 5vw, 4rem);
padding-inline: max(1rem, 4vw);
}Exercise
Write a declaration that makes a paragraph's width 100% of its container but never wider than 40rem and never narrower than 15rem. Use one function.
Show answer
width: clamp(15rem, 100%, 40rem); An equivalent form is width: max(15rem, min(100%, 40rem)). Both give a floor of 15rem, a ceiling of 40rem, and 100% in between.
Exercise
At a viewport width of 800px, what font size does font-size: clamp(1rem, 2rem + 1vw, 2.5rem) produce, assuming a 16px root size?
Show answer
The preferred value is 32px + 8px = 40px. The maximum is 2.5rem, which is 40px, so the result is 40px. At this width the preferred value equals the maximum.