Sub topic 7
What container units resolve to without a container
This chapter assumes you know media queries, viewport units, custom properties, clamp(), and the basics of declaring a container with container-type. It covers what a container query unit means when no container applies to the element using it.
The rule
Container query units such as cqi, cqw, and cqh measure a size against the nearest ancestor that is a suitable query container. If no ancestor qualifies, the unit does not become zero and it does not make the declaration invalid. It falls back to the matching small viewport unit.
The fallback table is:
| Container unit | Fallback when no container applies |
|---|---|
| `cqw` | `svw` |
| `cqh` | `svh` |
| `cqi` | `svi` |
| `cqb` | `svb` |
| `cqmin` | `svmin` |
| `cqmax` | `svmax` |
So 1cqi with no container is 1svi, which is 1% of the small viewport's inline size. The small viewport is the viewport size when browser UI, such as a mobile address bar, is fully expanded.
Why this matters
The fallback means a component using container units still renders sensibly outside a container. The sizes follow the page width and not the component's width, so nothing collapses. It also hides mistakes, because the layout looks plausible while the component is not responding to its container at all.
Take this markup and CSS:
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Body text.</p>
</article>
</div>.card {
padding: 2cqi;
}
.card h2 {
font-size: clamp(1.125rem, 4cqi, 2rem);
}The wrapper has no container-type. Every cqi here resolves against the small viewport. On a 1200px-wide window, 4cqi is about 48px, so clamp() caps the heading at 2rem. In a narrow sidebar the heading is still 2rem, because the viewport is wide. The component ignores its sidebar.
Now declare the container:
.card-wrapper {
container: card / inline-size;
}The same 4cqi now means 4% of the wrapper's inline size. A 400px wrapper gives 16px, and clamp() raises it to 1.125rem, which is 18px. The heading changes size without any other edit.
Which axes count
A container only supplies the axes it contains. This matters for the fallback.
container-type: inline-sizegives a size for the inline axis only.container-type: sizegives both axes.- A block-axis unit (
cqb, orcqhin a horizontal writing mode) needs a container that contains the block axis.
Unit resolution is per unit. If the nearest container is inline-size, then cqi resolves against it. A cqb unit skips that container and looks for an ancestor with a block-axis size. If none exists, it falls back to svb.
.panel {
container-type: inline-size;
}
.panel .media {
width: 50cqi; /* 50% of .panel's inline size */
height: 20cqb; /* no block-size container: 20svb */
}Here height: 20cqb is a viewport-based height. A common surprise is that the width tracks the component and the height tracks the page.
Detecting the fallback
The fallback is silent, so check for it deliberately.
- In the browser's dev tools, look at the computed value of the property. A
cqivalue that changes when you resize the window but not when you resize the wrapper is using the fallback. - Confirm that an ancestor, not the element itself, has
container-type. An element's own container does not supply units to that element. - Confirm the container type covers the axis you used.
Using the fallback on purpose
You can rely on the fallback as a progressive-enhancement layer. Browsers that support container units but find no container get viewport-relative sizing. If you want a different fallback, declare an earlier value that a supporting browser overrides:
.card h2 {
font-size: 1.5rem;
font-size: clamp(1.125rem, 4cqi, 2rem);
}A browser that does not understand cqi drops the second declaration and keeps 1.5rem. A browser that does understand it never uses 1.5rem, whether or not a container exists. The earlier line only guards against missing support. It does not cover a missing container.
To give an element a deliberate default without a container, set the container on a wrapper you control. A sensible default for the whole page is a container on a high-level wrapper, though that makes every descendant resolve against it and not against the viewport.
Exercise
A .thumb element has width: 30cqi; height: 10cqb;. Its parent has container-type: inline-size, and no ancestor has container-type: size. In a browser that supports container units, what do the width and height each resolve against?
Show answer
The width is 30% of the parent's inline size, because the parent contains the inline axis. The height is 10svb, which is 10% of the small viewport's block size, because no ancestor contains the block axis.
Exercise
A heading uses font-size: clamp(1rem, 5cqi, 2.5rem) and sits in a wrapper that has no container-type. The window is 1000px wide. What font size does the heading get?
Show answer
With no container, 5cqi becomes 5svi, which is 50px on a 1000px viewport. The clamp() maximum limits it to 2.5rem, so the heading is 2.5rem, or 40px at the default root size.