Sub topic 9
Container Query Units
This chapter assumes you can already mark an element with @container and style children with variants such as @md:. It covers the length units that measure a container instead of the viewport, and how to use them in Tailwind.
What container query units are
Viewport units such as vw and vh are percentages of the browser window. Container query units work the same way, but they are percentages of the nearest ancestor that is a query container.
| Unit | Meaning |
|---|---|
| `cqw` | 1% of the container's width |
| `cqh` | 1% of the container's height |
| `cqi` | 1% of the container's inline size (width in horizontal writing modes) |
| `cqb` | 1% of the container's block size (height in horizontal writing modes) |
| `cqmin` | The smaller of `cqi` and `cqb` |
| `cqmax` | The larger of `cqi` and `cqb` |
So 50cqw means half of the container's width. A container that is 600px wide makes 50cqw equal to 300px. If the container is resized, the value follows.
Using units in Tailwind
Tailwind has no dedicated utilities for these units. You write them as arbitrary values in square brackets.
<div class="@container">
<div class="flex flex-col @min-[475px]:flex-row">
<div class="w-[50cqw]">Half of the container width</div>
<div>Content</div>
</div>
</div>The outer div is the container. The child with w-[50cqw] is always half as wide as that container, whether the container sits in a narrow sidebar or a wide main column. The @min-[475px] variant is an arbitrary container breakpoint, and it switches the layout to a row once the container is at least 475px wide.
Any utility that accepts a length can take a container unit. For example, text-[5cqw] scales font size with the container, and p-[4cqi] scales padding.
Units are not the same as variants
Variants and units solve different problems.
- A variant such as
@md:is a switch. Below the size it does nothing, and at or above it the styles apply. - A unit such as
cqwis a continuous scale. The value changes smoothly as the container grows.
Use variants when the layout changes shape, for example column to row. Use units when something should grow in proportion, for example a heading that fills a fixed share of its container.
You can combine them. A common pattern is to switch the layout with a variant and size an element with a unit.
Which container is measured
A container query unit resolves against the nearest ancestor that has @container. Nested containers therefore change the result. Tailwind's named containers (@container/main) apply to variants such as @md/main:. The unit syntax has no way to name a container, so cqw always uses the nearest one.
If no ancestor is a query container, the units fall back to the small viewport size. 50cqw then behaves like 50svw. This is easy to miss, because the page still renders and the value looks plausible.
Things to watch
- The container's own size. An element cannot use
cqwto size the container it is itself. Units resolve against ancestors, so put the unit on a child. - Circular sizing. If the container's width depends on its children, and a child's width is set in
cqw, the browser has nothing stable to measure.@containerapplies inline-size containment, so the container's width must come from its parent, not from its content. Give it a block-level width or a grid or flex slot. - Readability. Text in
cqwshrinks with the container and can become too small. Pair it with a lower bound, for exampletext-[max(1rem,5cqw)].
Putting it together
This card uses a variant for layout and units for proportion. The image takes 40% of the container's width in row layout, and the title scales within limits.
<div class="@container">
<div class="flex flex-col gap-4 @min-[475px]:flex-row">
<img class="w-full @min-[475px]:w-[40cqw]" src="/photo.jpg" alt="Product photo" />
<div>
<h2 class="text-[clamp(1rem,5cqw,2rem)] font-semibold">Product name</h2>
<p class="text-sm text-gray-600">Short description of the product.</p>
</div>
</div>
</div>The clamp() function keeps the title between 1rem and 2rem, so it never becomes unreadable at either extreme.
Exercise
A child has w-[25cqw]. Its nearest @container ancestor is 800px wide, and that ancestor sits inside another @container that is 1200px wide. How wide is the child, and what happens if you remove @container from both ancestors?
Show answer
The child is 200px wide, because the unit resolves against the nearest container (800px × 25%). If both @container classes are removed, there is no query container, so the unit falls back to the small viewport size and the child becomes 25% of the viewport width.