Container query units: sizing against the container, not the viewport

Kevin Powell's "HTML & CSS Tip of the Week" site flags a useful detail about container queries: once you query a container, its size becomes available as a unit of measurement. Container query units let you size elements relative to that queried container.

The two core units are cqi and cqb. They behave like vw and vh, except they track the container's dimensions rather than the viewport's.

  • 1cqi equals 1% of the container's inline size — typically width in horizontal writing modes.
  • 1cqb equals 1% of the container's block size — typically height.

There are also cqmin and cqmax, which resolve against either the inline or the block size. For example, 50cqmax equals 50% of the container's size, but it compares the container's inline and block sizes, picks the larger one, and uses that for the final computed value.

1200px by 500px rectangle showing that 50cqmax is equal to 50% of the larger size.

That gives you a bit of conditional logic for free, and it helps preserve proportions if the writing mode might change, say from horizontal to vertical.