Find the Container a Unit Measures

Rosa’s session card now fits its slot. In the narrow sidebar, its title is 20 pixels, and in the wide Today panel, it’s 28, all from one rule: clamp(1.25rem, 0.95rem + 2cqi, 1.75rem). The last video left one question open, though. What exactly is cqi measured against?

That matters as soon as the card moves. If you place it somewhere new, nest one container inside another, or forget a container-type, the card’s sizes change without a warning. In this lesson, you’ll learn the rule the browser follows, so you can predict the length a container unit gives before you save and check it in DevTools afterward. Start with the unit you already use, cqi.

Find the Container Behind cqi

A container unit needs a box to measure, and the browser has to pick one. For cqi, it walks up from the element through its ancestors and stops at the first one that’s a query container, an element with a container-type that answers for the inline axis. One cqi is one percent of that container’s inline size, measured across its content box, excluding padding.

You can watch the lookup with a temporary rule. Open styles.css and, in the components layer, add this rule below .session-card h3. It sets each title’s width in cqi and draws a dashed outline around it, so the title’s box becomes a ruler:

Language: CSS Filename: styles.css
.session-card h3 {
  inline-size: 50cqi;
  outline: 2px dashed;
}

Container sizes depend on the window, so fix the window’s size first. Open DevTools with F12 or Cmd+Option+I on macOS. Turn on the device toolbar with Ctrl+Shift+M or Cmd+Shift+M on macOS, and set the size to 1280 by 800 pixels. Keep it there for the rest of the lesson.

Save, then select the family swim card’s <h3> in the Elements panel and open the Computed pane. The title is 117 pixels wide:

The Computed pane for the family swim card's title shows a width of 117px. On the page, a dashed outline marks the title's box, about half the sidebar's width.
Half of the Sidebar's Content Box

Where does 117 come from? The title’s parent is the card, but the card isn’t a container, so the browser skips it. The next ancestor is the sidebar, with container-type: inline-size. It’s 288 pixels wide with 27 pixels of padding on each side, so its content box is 234 pixels, and half of that is 117.

Watch out: cqi isn’t a percentage of the parent the way % is. Inside the card, 50% would be half the card’s content box, 99 pixels, not 117.

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

Become a Member to join the conversation.