In this lesson, you made the garden’s two heading sizes fluid with clamp(), so they grow with the window between a floor and a ceiling and still grow when a visitor zooms the page.
Key Points
- The need. The home page’s main heading reads
--size-h1,2.5remin thetokenslayer, so it’s 40 pixels at every width. At 320 pixels, that’s five lines, 300 pixels tall, pushing the photo far down. A media query could switch to a bigger size above one width, but the size would jump at that width. vwfollows the window. Onevwis a hundredth of the viewport’s width. With--size-h1: 4vw, the heading drops to 12.8 pixels at 320, smaller than the body text’s 18 pixels, and grows smoothly as you drag wider, to 51.2 pixels at 1280.vwalone ignores zoom. With device mode off andwindow.innerWidthreading 1280, zoom the page to 200%: it’s now 640 CSS pixels wide, so4vwis 25.6 pixels. Each CSS pixel is drawn twice as big, so the heading is 51.2 pixels on screen, the same as before. The paragraph’s 18 pixels stay 18 CSS pixels, so they double on screen. Onlyvwshrank, with the narrower page. A visitor who zooms gets no help, sovwon its own was a detour. (On-screen pixel counts assume a display at 100% scaling.)clamp()keeps a size between a floor and a ceiling. It takes three sizes: a floor, a preferred size and a ceiling. Withclamp(1.75rem, 1rem + 2.5vw, 2.5rem), the middle value is a sum of a rem part and a vw part.clamp()turns each part into pixels and adds them. A bare1rem + 2.5vwisn’t valid CSS. The heading sits on its floor, 28 pixels, up to 480 pixels wide (four lines instead of five at 320). At 480,1remis 16 and2.5vwis 12, so the middle reaches the floor (16 + 12 = 28). It grows with the window until 960, where 16 + 24 reaches the ceiling, 40 pixels. These numbers are a choice. Pick a floor and a ceiling, then tune the rem and vw parts until the ramp spans the widths you want.clamp()is Baseline Widely available, so every current browser supports it.- The rem part keeps zoom working. With device mode off again, at 200% zoom on the same window, the page is 640 CSS pixels wide, so the vw part is 16. With
1remstill 16, the heading is 32 CSS pixels. The floor and ceiling are in rem too, so they stay 28 and 40 CSS pixels, and 32 fits between them. Drawn twice as big, that’s 64 on screen, up from 40. That’s not quite double, but the rem part keeps the heading growing with zoom, and the vw part makes it follow the window. - One token, every heading. Back at 100% zoom in device mode,
--size-h2: clamp(1.25rem, 1rem + 1vw, 1.5rem)does the same one level down. Next Work Day and Volunteer of the Month are 20 pixels at 320 and 24 from 800 up. The course project makes the third-level headings and the spacing tokens fluid the same way.
The Rule
- When a size should grow a little at a time, use
clamp(): a floor and a ceiling in rem, which grow with zoom, and a preferred size with a rem part and a vw part. No media query needed. - Keep a media query when the layout changes shape, such as the header becoming a row or the sidebar coming in beside the welcome. A font size can grow by degrees, but a row can’t be half a row.