Sub topic 7
What container units resolve to without a container
This chapter covers what happens when a container query unit such as cqi is used where no container applies. It assumes you can write @container rules, declare container-type, and use clamp() and custom properties. Earlier chapters introduced the six units. This one explains the case that causes the most confusing bugs: the unit has nothing to measure.
The rule: fall back to the small viewport
A container query unit is a percentage of a container's content box. If the element has no ancestor that can supply that measurement, the unit does not become 0 and does not make the declaration invalid. It resolves to the matching small viewport unit.
| Container unit | Measures | Falls back to |
|---|---|---|
| `cqw` | 1% of container width | `svw` |
| `cqh` | 1% of container height | `svh` |
| `cqi` | 1% of container inline size | `svi` |
| `cqb` | 1% of container block size | `svb` |
| `cqmin` | the smaller of `cqi` and `cqb` | the smaller of `svi` and `svb` |
| `cqmax` | the larger of `cqi` and `cqb` | the larger of `svi` and `svb` |
The small viewport is the viewport as it looks when browser toolbars are fully expanded. On desktop it is the same as the ordinary viewport. On mobile it can be smaller than the dynamic (dvh) or large (lvh) viewport.
So padding: 5cqi on an element with no container behaves like padding: 5svi. In a horizontal writing mode that is 5% of the viewport width.
A worked example
This page uses the same .badge rule twice. One badge sits inside a 400px container. The other sits outside any container.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Container unit fallback</title>
<style>
.box {
container-type: inline-size;
width: 400px;
border: 1px solid #333;
margin-bottom: 1rem;
}
.badge {
padding: 5cqi;
background: gold;
margin: 0;
}
</style>
</head>
<body>
<div class="box">
<p class="badge">Inside the container</p>
</div>
<p class="badge">No container</p>
</body>
</html>Open the page in a window 1000px wide. The two badges get different padding:
- Inside
.box, the content box is 400px wide, so5cqiis 20px. - Outside, no ancestor is a container, so
5cqibecomes5svi, which is 50px at a 1000px viewport.
The CSS rule is identical, but the padding differs by 30px. Resize the window and the outside badge changes with it. The inside badge does not.
Fallback happens per axis and per unit
Each unit is resolved on its own. The browser asks, for each use of a unit, whether an eligible container exists for that unit's axis. An eligible container is the nearest ancestor that:
- has
container-type: size, or hascontainer-type: inline-sizeand the unit is on the inline axis, and - is an ancestor of the element, not the element itself.
Two consequences follow from that definition.
An inline-size container does not supply block-axis units
container-type: inline-size contains only the inline axis. The container's block size still depends on its content, so the browser cannot offer it as a measurement. Block-axis units therefore skip it.
.box {
container-type: inline-size;
width: 400px;
}
.panel {
width: 50cqi; /* 200px: measured against .box */
height: 10cqb; /* 10svb: .box has no block containment */
}Here cqi resolves against .box, but cqb falls back to the viewport. The same element gets one value from the container and one from the viewport.
To make cqh and cqb measure the container, use container-type: size. That value contains both axes, so the container needs an explicit height or a parent that supplies one. Otherwise its block size collapses to zero.
`cqmin` and `cqmax` can mix sources
Because each side is resolved separately, cqmin and cqmax compare a container value with a viewport value in the situation above.
.box {
container-type: inline-size;
width: 400px;
}
.dot {
width: 50cqmin;
aspect-ratio: 1;
}cqmin is the smaller of cqi and cqb. In .box, cqi is 4px per unit (400px / 100). cqb is svb, which is 1% of the viewport height. If the viewport is 900px tall, that is 9px per unit. The smaller value is 4px, so .dot is 200px wide. If the viewport is 300px tall, svb is 3px per unit, and cqmin suddenly measures the viewport. The .dot shrinks to 150px even though the container did not change.
The container itself does not count
An element's own container-type does not make it a source for its own units. Units on a container resolve against that container's ancestors. The same is true for @container rules, which cannot style the container they query.
.box {
container-type: inline-size;
padding: 5cqi; /* NOT measured against .box */
}If .box has no container ancestor, 5cqi here is 5svi. If .box sits inside another container, it is 5% of that outer container's inline size. In both cases the value is not 5% of .box.
The usual fix is the structure from the earlier chapters: a wrapper is the container, and its child uses the units.
<div class="wrapper">
<section class="content">...</section>
</div>.wrapper {
container-type: inline-size;
}
.content {
padding: 5cqi;
}Which container wins when containers are nested
For each unit, the browser walks up from the element and takes the nearest ancestor that is eligible for that axis. It does not take the nearest container of any type.
<div class="outer">
<div class="inner">
<p class="text">Hello</p>
</div>
</div>.outer {
container-type: size;
width: 800px;
height: 600px;
}
.inner {
container-type: inline-size;
width: 400px;
}
.text {
margin-inline: 5cqi; /* 5% of .inner: 20px */
margin-block: 5cqb; /* 5% of .outer: 30px */
}.inner is closer, so it supplies cqi. It is not eligible for the block axis, so the walk continues to .outer, which supplies cqb. The fallback to the viewport happens only when the walk reaches the root without finding an eligible container.
Custom properties resolve at the point of use
An unregistered custom property stores tokens, not computed values. When var() substitutes those tokens into a property, the container unit is resolved on the element that uses it, not on the element that declared it.
:root {
--gap: 3cqi;
}
.card-wrapper {
container-type: inline-size;
}
.card {
gap: var(--gap);
}The 3cqi is declared on :root, which has no container ancestor. A reader might expect it to fall back to 3svi everywhere. It does not. The .card uses var(--gap), so the unit resolves relative to .card, and .card-wrapper supplies the measurement. A .card used outside any wrapper gets 3svi.
This is useful for design tokens, such as --gap or --pad, that adapt to whichever container they are used in. It also means you cannot read a token's value by looking at where it is declared.
Writing safe fallbacks for older browsers
The viewport fallback applies only in browsers that understand container units. In a browser that does not, the declaration padding: 5cqi is invalid, and the browser drops it while parsing. The usual cascade technique works: put a plain declaration first and the container-unit declaration after it.
.card h2 {
font-size: 1.25rem;
font-size: clamp(1.125rem, 4cqi, 2rem);
}A browser without support ignores the second line and keeps 1.25rem. A browser with support uses the second line.
There is a second kind of fallback to plan for: a supporting browser where the component is used outside a container. Here 4cqi becomes 4svi and clamp() limits the damage. At a 1200px viewport, 4svi is 48px, and the clamp reduces it to 2rem. At a 300px viewport, it is 12px, and the clamp raises it to 1.125rem. This is why the earlier chapter paired container units with clamp(): the bounds keep the size sensible whichever source of measurement is in effect.
Checking which source is in use
When a size looks wrong, you can test the fallback directly.
- Open DevTools and select the element.
- Look at the computed value. A container-based value usually matches 1% of the wrapper's content width times your multiplier. A viewport-based value matches the viewport.
- Resize the window. If the value changes while the wrapper stays the same width, the unit has fallen back to the viewport.
- Check the wrapper for
container-type. A wrapper with onlycontainer-nameorcontainer-type: normalis not a size container. - Check that the unit is on a descendant of the wrapper, not on the wrapper.
- If the unit is
cqhorcqb, check that the container issize, notinline-size.
Summary of the failure modes
| Symptom | Likely cause | Fix |
|---|---|---|
| Units follow the viewport instead of the wrapper | No eligible ancestor has `container-type` | Add `container-type: inline-size` to the wrapper |
| Container padding ignores its own width | Units on the container resolve against its ancestors | Move the unit to a child |
| `cqb` or `cqh` follows the viewport | Container is `inline-size` | Use `container-type: size` with a defined height |
| `cqmin` jumps when the window height changes | One side fell back to `svb` | Use `cqi`, or use `size` containment |
| Token behaves differently in two places | `var()` resolves units at the point of use | Treat tokens as formulas, not fixed values |
Exercise
A page has .wrap { container-type: inline-size; width: 600px; } and, inside it, .item { width: 10cqi; height: 10cqb; }. The viewport is 1000px wide and 800px tall. What are the computed width and height of .item, and why?
Show answer
The width is 60px, because .wrap is an inline-size container with a 600px content box and 10cqi is 10% of that. The height is 80px. .wrap has no block-axis containment, so no eligible container exists for cqb, and 10cqb falls back to 10svb, which is 10% of the 800px small viewport height.
Exercise
You write .card { container-type: inline-size; padding: 4cqi; } and the padding changes when you resize the browser window, not when you resize the card's parent. Why does this happen, and how do you fix it?
Show answer
An element's units never resolve against its own container type. .card has no container ancestor, so 4cqi falls back to 4svi, which follows the viewport. Wrap the card in an element with container-type: inline-size and apply the padding to the card inside it, or move the padding to a child of .card.
Exercise
A browser that supports container units shows font-size: clamp(1rem, 5cqi, 2.5rem) on a heading that is not inside any container. The viewport is 360px wide. What font size does the heading get?
Show answer
With no container, 5cqi becomes 5svi, which is 5% of 360px, or 18px. That sits between the clamp bounds of 16px (1rem at default size) and 40px (2.5rem), so the heading is 18px.