Sub topic 9
Container query units
This chapter assumes you can already mark an element with @container, style children with variants such as @md:, and write arbitrary values like w-[50cqw]. You should know what a viewport unit such as vw means. The chapter covers the CSS container query length units, how Tailwind exposes them, and where they behave differently from what you might expect.
What container query units are
Container query variants switch styles on and off at a size threshold. Container query units do something different: they give you a length that scales continuously with the container. If a container is 400px wide, 1cqw is 4px. At 800px wide, 1cqw is 8px. No breakpoint is involved.
Viewport units work the same way, except they measure the browser window. A heading set to 5vw grows with the window even when it sits in a narrow sidebar. A heading set to 5cqw grows with the sidebar.
Tailwind has no dedicated utilities for these units. You write them as arbitrary values, such as w-[50cqw] or p-[4cqi]. The browser does the rest.
The six units
| Unit | Meaning | Relative to |
|---|---|---|
| `cqw` | 1% of the container's width | Width |
| `cqh` | 1% of the container's height | 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` | Smaller axis |
| `cqmax` | The larger of `cqi` and `cqb` | Larger axis |
In left-to-right languages such as English, cqi equals cqw and cqb equals cqh. They differ in vertical writing modes, such as some Japanese or Chinese layouts. There the inline axis runs top to bottom, so cqi follows the height. Prefer cqi and cqb when your component might appear in more than one writing mode. Use cqw and cqh when you mean physical width and height.
Each unit measures the container's content box. Padding and borders on the container are not counted. A container with p-8 and a 400px width has a content width of 336px, so 1cqw is 3.36px.
How the browser picks the container
An element using a container unit does not name the container it wants. The browser walks up the ancestor chain and uses the nearest ancestor that can answer for that axis. Figure 1 shows the decision.
Two consequences follow from this.
- Named containers do not help. Unlike the
@md/sidebar:variant syntax, units have no way to say which container they mean. To target a particular ancestor, you must make sure it is the nearest eligible one, or use a variant instead. - An element's units never refer to itself. If the container element itself uses
w-[50cqw], the value resolves against the next ancestor container up, not against itself.
The axis trap: `@container` only gives you width
Tailwind's @container class applies container-type: inline-size. This lets the browser track the container's width, but not its height. A container whose height depends on its content cannot also serve as a reference for height without creating a circular dependency.
So inside a plain @container:
cqwandcqiwork as expected.cqhandcqbfind no eligible container. They silently fall back to the small viewport height (svh).
This fallback is the most common surprise with these units. The page does not break. A height that you expected to follow the container instead follows the window.
If you need height-based units, the container must opt into container-type: size. Tailwind's @container class doesn't do that, so use an arbitrary property:
<div class="h-64 w-96 [container-type:size]">
<div class="h-[50cqh] w-[50cqw] bg-indigo-500"></div>
</div>Size containment has a cost. The container can no longer size itself to its content in either direction, so it needs an explicit height from you, such as h-64 or a flex or grid parent that assigns one. Without that, the container collapses to zero height and everything measured in cqh becomes zero.
Example: a fluid heading and spacing
A common use is text that scales with its card. The next page is complete and runs from a single file. The dashed box is resizable, so you can drag its bottom-right corner and watch the heading and padding follow.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Container query units</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="p-8">
<div class="@container w-96 max-w-full resize-x overflow-auto border border-dashed border-gray-400 p-0">
<article class="bg-white p-[4cqi] shadow">
<h2 class="text-[length:clamp(1rem,7cqi,2.25rem)] font-bold leading-tight">
Scales with the card
</h2>
<p class="mt-[2cqi] text-[length:clamp(0.875rem,3.5cqi,1.125rem)] text-gray-600">
Drag the lower-right corner of the dashed box. The heading, the padding,
and this text change size, while the browser window stays the same.
</p>
</article>
</div>
</body>
</html>Several details matter here.
- The
@containerelement hasp-0, so its content box equals its full width. The padding lives on thearticle, which usesp-[4cqi]. Padding specified incqion the container itself would resolve against the wrong element. text-[length:...]is a type hint. Tailwind uses the sametext-prefix for font size and color, and aclamp()expression can be ambiguous. Thelength:prefix tells Tailwind to emitfont-size.clamp(min, preferred, max)keeps the fluid value inside readable limits. Pure7cqiwould make a 200px container produce 14px headings and a 1200px container produce 84px headings, both poor outcomes.- There are no spaces inside the arbitrary value. Tailwind splits class names on whitespace, so use commas, or underscores where you need a space, as in
[calc(100cqw_-_2rem)]. Incalc(), Tailwind v4 also adds the required spaces around+and-for you, but underscores make the intent explicit.
Using units inside calc and layout
Container units combine well with other lengths. Here is the example from the plan, with its reasoning:
<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 variant @min-[475px] decides the direction of the layout. The unit 50cqw sizes the first child as half of the container. Below 475px the children stack, and the first child is still 50% of the container width. If you want it to fill the row when stacked, add a variant such as w-full @min-[475px]:w-[50cqw].
For this particular case, w-1/2 would be equivalent and simpler, because the parent is the container. Reach for cqw when the percentage is relative to a container that is not the element's direct parent. A percentage width resolves against the parent's width, but cqw resolves against the nearest container, however many wrappers lie between.
Other patterns that suit container units:
- A square thumbnail whose side is a share of the container:
size-[30cqi]. - A gap that scales with the layout:
gap-[2cqi]. - A subtraction such as
max-w-[calc(100cqw-4rem)]for content that leaves a fixed margin. - A responsive aspect-aware size using
cqmin, such assize-[40cqmin]. This requires acontainer-type: sizeancestor to be meaningful on both axes. In a width-only container, the height half falls back tosvh, andcqmintakes whichever is smaller.
Units versus variants: choosing
| Need | Better tool |
|---|---|
| Switch layout at a threshold (column to row) | Variants such as `@md:flex-row` |
| Smooth scaling with no steps | Container units |
| Target a specific ancestor | Named container variants (`@md/main:`) |
| Height-based sizing | Units, with a `container-type: size` ancestor |
They work together. Use variants for structural changes and units, usually inside clamp(), for fine-grained scaling between those changes.
Common problems
- Units resolve to the window, not the container. Either no ancestor has
@container, or you usedcqhorcqbunder an inline-size container. Check the computed value in dev tools. If a container unit equals the viewport value, the fallback is happening. - The container collapses to zero width.
inline-sizecontainment means the container's width cannot come from its content. A container that isinline-block,float, a flex item with no basis, or a grid item in anautotrack can shrink to nothing. Give it an explicit width, or make it a block that fills its parent. - Container with
cqipadding looks wrong. The element's own units look to its ancestors. Move the padding to a child, as in the example. text-[...]generates the wrong property. Addlength:for font sizes orcolor:for colors.- Unexpectedly tiny or huge text. Unbounded
cqitext follows the container size linearly. Wrap inclamp(). - Tailwind v3. The
@containerclass requires@tailwindcss/container-queries, covered in a later chapter. The arbitrary-value classes such asw-[50cqw]are plain CSS values and work without the plugin, but they need a real container ancestor, which the plugin's@containerclass supplies.
Exercise
A card has the class @container. Inside it, a banner uses h-[20cqh]. The card is 300px wide and 600px tall, and the browser window is 800px tall. What height does the banner get, and why? How would you make it 120px?
Show answer
The banner gets about 160px, which is 20% of the small viewport height (800px). @container sets container-type: inline-size, so the card is not an eligible container for the block axis, and cqh falls back to svh. To make it 120px (20% of 600px), the card must use container-type: size, for example class="h-[600px] [container-type:size]", with an explicit height, since size containment stops the card from deriving its height from its content.
Exercise
You want a heading that is 5% of the container's inline size, never smaller than 18px and never larger than 40px. Write the Tailwind class, and say why it needs the length: prefix.
Show answer
text-[length:clamp(18px,5cqi,40px)]. The text- prefix is used for both font size and text color, and Tailwind may not be able to tell which one a clamp() expression represents. The length: hint makes it generate font-size.