Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Make Type and Spacing Fluid

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.5rem in the tokens layer, 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.
  • vw follows the window. One vw is 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.
  • vw alone ignores zoom. With device mode off and window.innerWidth reading 1280, zoom the page to 200%: it’s now 640 CSS pixels wide, so 4vw is 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. Only vw shrank, with the narrower page. A visitor who zooms gets no help, so vw on 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. With clamp(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 bare 1rem + 2.5vw isn’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, 1rem is 16 and 2.5vw is 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 1rem still 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.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The events page’s cards now fit every screen with no media query. The headings haven’t caught up. At 1280 pixels, the home page’s main heading is 40 pixels, on two lines.

00:12 At 320, it’s still 40 pixels: five lines, 300 pixels tall, pushing the photo far down.

00:20 That size comes from one token, –size-h1: 2.5rem at every width. A media query could switch to a bigger size above one width, but then the size would jump at that width.

00:33 You want it to grow a little at a time.

00:37 CSS has a unit for that. One vw is a hundredth of the viewport’s width. Set the token to 4vw, and save. At 320, the heading drops to 12.8 pixels, smaller than the body text’s 18.

00:52 Now drag wider. It grows with the window, smoothly. At 1280, it’s 51.2 pixels.

01:02 There’s a second problem. Turn off device mode, and check the Console: the page is 1280 CSS pixels wide. Then zoom the page to 200%. It’s now 640 CSS pixels wide, so 4vw is only 25.6 pixels.

01:19 Each CSS pixel is drawn twice as big, so on screen, the heading is 51.2 pixels, the same as before. The paragraph’s 18 pixels stay 18 CSS pixels, so they double on screen.

01:33 Only vw shrank, with the narrower page. A visitor who zooms gets no help.

01:39 So vw on its own was a detour.

01:45 CSS has a clamp function, like clamping a number in code. It takes a floor, a preferred size, and a ceiling. Back at 100% zoom, in device mode, set the token to a clamp.

01:58 The floor is 1.75rem. The middle, 1rem plus 2.5vw, has a rem part and a vw part. Clamp turns each into pixels and adds them. The ceiling is 2.5rem. Save. Drag from 320.

02:15 The heading sits on its floor, 28 pixels, on four lines instead of five. At 480, 1rem is 16 and 2.5vw is 12: 28, the floor. It grows until 960, where 16 plus 24 reaches the ceiling, 40.

02:32 The numbers are a choice. Pick a floor and ceiling, then tune the rem and vw parts until the ramp spans the widths you want. Clamp is Baseline Widely available.

02:43 Every current browser supports it.

02:47 Turn off device mode again, and zoom the page to 200% on the same window. It’s 640 CSS pixels wide again. Your turn: pause the video and predict. How big is the heading on screen now?

03:03 At 640 CSS pixels, the vw part is 16. The 1rem stays 16, so the heading is 32 CSS pixels. The rem floor and ceiling stay 28 and 40 CSS pixels, and 32 fits between them.

03:18 Drawn twice as big, that’s 64 on screen, up from 40. Not quite double, but the rem part keeps it growing with zoom, and the vw part makes it follow the window.

03:33 The same move works one level down. Reset the zoom, turn device mode back on, and set –size-h2 to a clamp: 1.25rem, 1rem plus 1vw, and 1.5rem. Every heading that reads the token follows.

03:50 Next Work Day and Volunteer of the Month are 20 pixels at 320 and 24 from 800 up.

03:58 The course project makes the third-level headings and the spacing tokens fluid the same way.

04:05 So when a size should grow a little at a time, use clamp: a rem floor and ceiling, which grow with zoom, and a preferred size with a rem part and a vw part. No media query needed.

04:18 Keep a media query when the layout changes shape: the header becoming a row, or the sidebar coming in. A font size can grow by degrees, but a row can’t be half a row.

04:30 Next, in Respond to the Container’s Width, a card follows the width of its own slot instead of the window.

Become a Member to join the conversation.