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

Place One Card in Two Containers

This section’s starting files add to Rosa’s home page: a two-column layout with a “Next at Riverside” sidebar, and one session card component in two slots. Open them from the course download before you watch.

The video picks up with that page: the early lane swim in the Today panel, in the wide main column, and the family swim in the narrow “Next at Riverside” sidebar. Both cards looked the same: stacked, even in the Today panel, which had room to spare, and with a title and padding that followed the window, so the sidebar’s card got the same big title as the wide one.

In this video, you made the card fit the space it’s given. A container query switched its layout, and cqi inside clamp() scaled its title and padding with its slot. Here are the main points and the final code.

Key Points

  • Give each slot container-type: inline-size. In the Elements panel, the Today panel and the sidebar then carry a container badge.
  • A container query, @container (inline-size >= 30rem), sets the wide layout: the time in its own 7rem column, beside the title, the badges and the note. At 1280 pixels, the Today panel’s slot is 920 pixels wide, so its card goes side by side. The sidebar’s slot is 234 pixels wide, so its card stays stacked.
  • A media query couldn’t do this. It sees only the window, so both cards would switch together.
  • A size token such as --font-size-subheading grows with the window, so the title was 28 pixels in both slots. Container query units measure the container instead: 1cqi is one percent of the container’s inline size, 2.34 pixels in the sidebar and 9.2 in the Today panel.
  • Work out the title’s clamp() from two sizes, as for the headings, with the slot’s width in place of the window’s, so the slope is in cqi: 20 pixels in a 240-pixel slot and 28 in a 640-pixel one. The title grows 8 pixels over 400, so the slope is 2cqi. At 240 pixels, that’s 4.8 pixels, and the other 15.2 pixels come from rem, so the preferred value is 0.95rem + 2cqi. The padding goes from 16 pixels to 24 the same way.
  • At 1280 pixels, the sidebar’s title is 20 pixels and its padding 16. In the Today card, the title stays at 28 pixels, and the padding settles at 24, down from 27: the maximums of the two clamp() values.
  • Narrow the window to 800 pixels, and the Today panel’s slot is 444 pixels wide, under 30rem: its card stacks again and its title shrinks to 24 pixels. The sidebar’s slot barely changes, at 237 pixels, so its card doesn’t either.
  • At 700 pixels, the page’s layout puts the sidebar under the main column at full width, and its slot grows to 650 pixels. Its card goes side by side, its time in its own column, and its title grows to 28 pixels. The window got narrower, but the card’s slot got wider, and the card answered its slot.
  • Zoom the 1280-pixel window to 200%, and the page lays out 640 CSS pixels wide, with the sidebar under the main column. The Today card’s title is 27 CSS pixels, drawn twice as big: 54 on screen, 1.93 times its 28. Its slot shrank from 920 to 590 pixels, so its cqi part shrank, but the rem part keeps the text growing.
  • Reset the zoom and narrow the window to 320 pixels: both cards stack, and nothing scrolls sideways.
  • The rule: make the slot the container, switch the card’s layout with a container query, and size its parts with a clamp() that has a cqi slope and a rem part.

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 Rosa’s home page now shows a session card in two places. In the wide main column, the Today panel shows the early lane swim. In the narrow sidebar, Next at Riverside shows the family swim.

00:13 It’s one component, and right now it looks the same in both slots. Each card is stacked, even in the Today panel, which has room to spare. The titles are 28 pixels in both, and the padding is 27.

00:26 Those sizes follow the window, so the sidebar’s card gets the same big title as the wide one. In this video, you’ll make the card fit the space it’s given, first its layout, then its sizes.

00:41 First, the layout. Give each slot, the Today panel and the sidebar, an inline-size container type. In the Elements panel, both now carry a container badge.

00:52 Then add a container query for slots at least 30 rems wide. Inside it, the time moves into its own column, beside the title, the badges and the note. Save.

01:04 The main column’s card goes side by side. The sidebar is 234 pixels wide, so its card stays stacked. A media query couldn’t do this. It sees only the window, so both cards would switch together.

01:20 The layout adapts, but the title is still 28 pixels in both slots. Its size comes from a token that grows with the window. Container query units measure the container instead.

01:32 One cqi is one percent of the container’s inline size. In the sidebar, that’s 2.34 pixels. In the Today panel, it’s 9.2.

01:44 Pick two sizes for the title, as you did for the heading: 20 pixels in a 240 pixel slot, and 28 in a 640 pixel one. It’s the same method, with the slot’s width in place of the window’s, so the slope is in cqi.

01:59 The title grows 8 pixels over 400, so the slope is 2 cqi. At 240 pixels, that’s 4.8 pixels, and the other 15.2 come from rem. So the preferred value is 0.95 rem plus 2 cqi.

02:16 Do the same for the padding, from 16 pixels to 24. Save. In the sidebar, the title drops to 20 pixels and the padding to 16. In the main column, the title stays at 28, and the padding settles at 24, the clamps’ maximums.

02:35 Now narrow the window, and watch both cards. At 800 pixels, the main column’s slot is 444 pixels wide, under 30 rems. Its card stacks again, and its title shrinks to 24.

02:47 The sidebar’s slot barely changes, at 237 pixels, so its card doesn’t either.

02:56 At 700 pixels, the page’s layout puts the sidebar under the main column, at full width. Its slot is now 650 pixels wide. Your turn: pause the video and predict how the family swim card looks there.

03:11 It goes side by side, its time in its own column, and its title grows to 28 pixels. The window got narrower, but the card’s slot got wider, and the card answered its slot.

03:26 Last, zoom the 1280 pixel window to 200%. The page lays out 640 CSS pixels wide, so the sidebar drops under the main column. The main card’s title is 27 CSS pixels, drawn twice as big: 54 on screen, 1.93 times its 28.

03:46 Its slot shrank from 920 to 590 pixels, so its cqi part shrank, but the rem part keeps the text growing.

03:56 Then reset the zoom and narrow the window to 320 pixels. Both cards stack, and nothing scrolls sideways.

04:05 Here’s the rule. Make the slot the container, switch the card’s layout with a container query, and size its parts with a clamp that has a cqi slope and a rem part.

04:17 Rosa’s session card now fits wherever it’s placed. But which box does a container unit measure when containers nest, or when there isn’t one? That’s next, in Find the Container a Unit Measures.

Become a Member to join the conversation.