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 acontainerbadge. - A container query,
@container (inline-size >= 30rem), sets the wide layout: the time in its own7remcolumn, 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-subheadinggrows with the window, so the title was 28 pixels in both slots. Container query units measure the container instead:1cqiis 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 incqi: 20 pixels in a 240-pixel slot and 28 in a 640-pixel one. The title grows 8 pixels over 400, so the slope is2cqi. At 240 pixels, that’s 4.8 pixels, and the other 15.2 pixels come fromrem, so the preferred value is0.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
cqipart shrank, but therempart 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 acqislope and arempart.