Loading video player…

Turn Fluid Sizes Into a Scale

The video picks up with Rosa’s home page as the previous lesson, Make Fluid Headings That Still Zoom, left it: a main heading that fits her phone and grows when she zooms, and every other size still a fixed rem value.

In this video, you turned the site’s size tokens into a fluid scale. You put semantic tokens between the primitives and the rules that read them, gave each primitive a clamp() worked out from two sizes, and checked every step at 320 pixels, in a wide window and at 200% zoom. Here are the main points and the final tokens.

Key Points

  • The size tokens live on :root in the base layer. Primitives hold values, one step on a scale each. Semantic tokens name a role, such as the header’s gap, and point at a primitive. Components read only the roles.
  • Add the roles first. When the body, the subheading and the components read semantic tokens that point at the same primitives, nothing on the page changes.
  • Then make only the primitives fluid. Each step keeps its fixed value at 320 pixels, which becomes the minimum of its clamp(), and is an eighth bigger at 1280 pixels, so body text goes from 16 pixels to 18. The eighth is a choice: small growth keeps most of each size in rem. Each clamp() comes from those two sizes, as the heading’s did.
  • The step numbers keep their meaning. A spacing step’s number counts quarter rems, so --space-4 is 1rem. A type step’s number counts steps up a 1.25 scale, so --text-3 is three steps up. Both stay true at 320 pixels, where each step keeps its value.
  • The scale gets a new top step, --text-4, one step above --text-3, so on a phone the heading grows from 32 pixels to 39. --space-8 and --space-16 join the spacing steps, for later pages. Two new roles point at new steps: --font-size-heading, which the main heading reads, and --space-stack, for the space between stacked blocks.
  • Some steps have no rule yet, so the home page can’t show them. Make a scratch page, scale.html, that links styles.css and has a line of text per type step and a bar per spacing step, each reading one primitive, such as .text-1 { font-size: var(--text-1); } and .space-8 { inline-size: var(--space-8); }. It’s a check aid, not part of the site, and it lets you check every step.
  • Check the narrow end with the device toolbar at 320 pixels: every step is at its minimum (body text and the header’s gap 16 pixels, the panel’s padding 24, the heading 39), and there’s no sideways scroll. Then turn off the device toolbar and widen the real window to 1280 pixels: body text and the gap reach 18 pixels, and the padding 27.
  • Check at 200% zoom in that wide window. The viewport drops to 640 CSS pixels, and the Computed pane shows body text at 16.7 pixels. Drawn twice as big, that’s 33.3 on screen, against 18 before: 1.85 times. Close to twice is what you want, and every other step comes out 1.85 times too.
  • Watch out: rem bounds alone don’t make a step zoom. Between its bounds, the preferred value decides, and zoom shrinks its vw part. Kept at 56 pixels (3.5rem) in a wide window, the top step’s preferred value had the most vw of any step, and the heading came out only 1.6 times as big.
  • The fix: bring its wide size down to an eighth bigger, 2.746rem. That gives it a gentler slope and mostly rem, and it comes out 1.85 times as big too. The cost: the heading now tops out at 44 pixels in a wide window instead of 56.
  • The more a step grows with the window, the less it grows with zoom. So keep the growth small, and confirm it at 200%.

The Final Code

The :root rule in the base layer of styles.css, with the color tokens left as they were:

Language: CSS
:root {
  /* Primitives */
  --text-0: clamp(1rem, 0.958rem + 0.209vw, 1.125rem);
  --text-1: clamp(1.25rem, 1.198rem + 0.26vw, 1.406rem);
  --text-2: clamp(1.5625rem, 1.497rem + 0.327vw, 1.758rem);
  --text-3: clamp(1.953rem, 1.871rem + 0.408vw, 2.197rem);
  --text-4: clamp(2.441rem, 2.339rem + 0.509vw, 2.746rem);
  --space-2: clamp(0.5rem, 0.479rem + 0.104vw, 0.562rem);
  --space-4: clamp(1rem, 0.958rem + 0.209vw, 1.125rem);
  --space-6: clamp(1.5rem, 1.437rem + 0.314vw, 1.688rem);
  --space-8: clamp(2rem, 1.916rem + 0.418vw, 2.25rem);
  --space-16: clamp(4rem, 3.833rem + 0.834vw, 4.5rem);

  /* Semantic tokens */
  --font-size-body: var(--text-0);
  --font-size-heading: var(--text-4);
  --font-size-subheading: var(--text-2);
  --space-gap: var(--space-4);
  --space-inset: var(--space-6);
  --space-control-block: var(--space-2);
  --space-control-inline: var(--space-4);
  --space-stack: var(--space-8);
}

The rules that read them:

Language: CSS
body {
  font-size: var(--font-size-body);
}

h1 {
  font-size: var(--font-size-heading);
}

h2 {
  font-size: var(--font-size-subheading);
}

.site-header {
  gap: var(--space-gap);
  padding-inline: var(--space-inset);
}

.today-link {
  padding-block: var(--space-control-block);
  padding-inline: var(--space-control-inline);
}

.today-panel {
  padding: var(--space-inset);
}

Each rule keeps its other declarations.

Next, in Exercise: Build the Lido Page’s Fluid Scale, you’ll give Riverside Lido’s page a scale of its own. It’s this course’s first coding exercise, and its Contents tab explains how exercises work.

00:00 Rosa’s main heading now fits her phone and grows when she zooms. Widen the window from 320 pixels to 1280, and the heading grows from 32 pixels to 56. The body text stays at 16, and the space around the Today panel doesn’t move.

00:18 So a wide window gets a big heading over phone-sized text and spacing.

00:23 In this video, you’ll turn the site’s size tokens into a fluid scale, then check it at 320 pixels, in a wide window, and at 200% zoom.

00:36 The sizes already live as tokens on the root element, in the base layer. These primitives each hold a value, one step on a scale. Semantic tokens sit on top.

00:47 Each names a role, such as the header’s gap, and points at a primitive. Components read only the roles.

00:55 This site skips that tier for its sizes, so add it, and switch the rules to roles. The page doesn’t change, since each role points at the same value.

01:06 Now give each primitive two sizes. At 320 pixels, a step keeps its current value, the clamp’s minimum. At 1280, it’s an eighth bigger, so body text goes from 16 pixels to 18.

01:21 That eighth is a choice. Small growth keeps most of each size in rem. A spacing step’s number counts quarter rems, so --space-4 is one rem. A type step’s number counts steps up a 1.25 scale, so --text-3 is three steps up.

01:39 Both stay true at 320 pixels, where each step keeps its value. Each line comes from its two sizes, like the heading’s.

01:51 A new top step, --text-4, sits one step above --text-3, so on a phone the heading grows from 32 pixels to 39. You keep its wide 56 pixels, so it grows far more than an eighth.

02:05 Two spacing steps join it, for later pages. Two new roles follow, the heading font size for the main heading and the stack space between stacked blocks.

02:16 Save, and check the narrow end. Some steps have no rule yet, so make a scratch page, scale.html, that links the stylesheet and shows a line of text per type step, a bar per spacing step.

02:31 In the device toolbar at 320 pixels, every step is at its minimum. Body text and the gap are 16 pixels, the padding 24, and the heading 39. There’s no sideways scroll.

02:45 Turn off the device toolbar and widen the real window to 1280. Body text and the gap reach 18 pixels, the padding 27, and the heading 56.

02:58 Last, zoom that window to 200%. The viewport drops to 640 CSS pixels, and the Computed pane shows body text at 16.7. Drawn twice as big, that’s 33.3, or 1.85 times 18.

03:15 Close to twice is what you want. Every other step comes out 1.85 times too, but the heading only gets 1.6. Watch out: rem bounds alone don’t make a step zoom.

03:28 Between them, the preferred value decides. The top step has the most vw, which zoom shrinks. Bring its wide size down to an eighth bigger. Its slope gets gentler, and rem makes up most of its size.

03:43 At 100%, the heading tops out at 44 pixels instead of 56. That’s the cost. At 200%, it comes out 1.85 times as big.

03:56 Here’s the rule: change the primitives, point roles at them, and check every step at 320 pixels, in a wide window, and at 200% zoom.

04:07 The more a step grows with the window, the less it grows with zoom. So keep the growth small, and confirm it at 200%.

04:16 Rosa’s home page now scales from her phone to a wide screen. Next, in Exercise: Build the Lido Page’s Fluid Scale, you’ll give Riverside Lido’s page a scale of its own.

04:27 It’s this course’s first coding exercise, and its Contents tab explains how exercises work.

Become a Member to join the conversation.