Sub topic 6
Fluid type and spacing with cqi and clamp()
This chapter assumes you know the @container rule, container-type: inline-size, and the container shorthand from earlier chapters. It also assumes you are comfortable with clamp() and custom properties. Here you will combine container query units with clamp() so that type and spacing scale smoothly with a component's container instead of jumping at breakpoints.
Why pair cqi with clamp()
An @container rule changes a component in steps. At 30rem the card becomes a grid, and below that it is a block. Steps suit layout changes. They suit continuous values such as font size and padding less well, because you would need many rules to approximate a smooth curve.
A container query unit gives you the continuous part. 1cqi is 1% of the query container's inline size. In a 40rem container, 1cqi is 0.4rem. A heading set to 4cqi therefore grows and shrinks as the container does.
A raw cqi value has no limits, though. In a very narrow container the heading becomes unreadable, and in a very wide one it becomes huge. clamp(min, preferred, max) fixes this. It uses the preferred value while that value lies between the bounds, and otherwise it uses the nearest bound.
A first fluid card
The markup is the same wrapper and article used in earlier chapters:
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Body text that reflows with the wrapper, not the viewport.</p>
</article>
</div>The wrapper is the container. The card and its heading are descendants, so their cqi values resolve against the wrapper:
.card-wrapper {
container: card / inline-size;
}
.card {
padding: 2cqi;
}
.card h2 {
font-size: clamp(1.125rem, 4cqi, 2rem);
}The heading is 4cqi wide-ish. It is at least 1.125rem and at most 2rem. With a 16px root font size, the preferred value reaches the minimum at about 28rem, because 4% of 28rem is 1.12rem. It reaches the maximum at 50rem.
Place this card in a sidebar that is 18rem wide and the heading stays at its minimum. Place the same card in a main column that is 60rem wide and the heading stays at its maximum. The viewport width does not matter in either case.
Deriving a clamp() from two sizes
Guessing a coefficient like 4cqi works, but you usually know what you want: "1.125rem when the container is 20rem wide, 2rem when it is 60rem wide." You can compute the preferred value from those two points.
The preferred value is a line: intercept + slope * container width. Because 1cqi is width / 100, slope * 100 gives the number of cqi to write.
- Slope = (max size − min size) / (max width − min width) = (2 − 1.125) / (60 − 20) = 0.021875 rem of font size per rem of width.
- The
cqicoefficient = slope × 100 = 2.1875. - Intercept = min size − slope × min width = 1.125 − 0.021875 × 20 = 0.6875rem.
The result:
.card h2 {
font-size: clamp(1.125rem, 0.6875rem + 2.1875cqi, 2rem);
}You can check both ends. At 20rem, 1cqi is 0.2rem, so the preferred value is 0.6875 + 2.1875 × 0.2 = 1.125rem. At 60rem, 1cqi is 0.6rem, so it is 0.6875 + 2.1875 × 0.6 = 2rem. Between the two the size grows linearly, and outside them the bounds hold it.
The resulting curve for the heading above looks like this. The size is flat below 20rem, rises linearly to 60rem, and is flat again above.
Fluid spacing
Spacing works the same way. Padding, gap, and margins can all take a clamped cqi value:
.card-wrapper {
container: card / inline-size;
}
.card {
display: grid;
gap: clamp(0.5rem, 2cqi, 1.5rem);
padding: clamp(0.75rem, 3cqi, 2rem);
}Percentage padding already scales, but it resolves against the width of the element's containing block. That block may be a grid area or flex item whose width has nothing to do with the component's container. cqi always refers to the container you named, so the padding stays tied to the component's own space.
Keep the scale in custom properties
If several properties share one fluid scale, store it in custom properties:
.card-wrapper {
container: card / inline-size;
}
.card {
--space-s: clamp(0.5rem, 2cqi, 1rem);
--space-m: clamp(0.75rem, 3cqi, 1.5rem);
display: grid;
gap: var(--space-s);
padding: var(--space-m);
}A normal (unregistered) custom property stores its value as text and substitutes it where var() appears. The cqi in --space-m is therefore resolved on the element that uses it. Child elements that read var(--space-m) resolve it against their own nearest container, which is usually what you want.
A registered property behaves differently. If you declare @property --space-m { syntax: "<length>"; inherits: true; initial-value: 0px; }, the value is computed to an absolute length on the element that declares it. Descendants then inherit that fixed length, even if a nested container sits between them. Use unregistered properties for fluid tokens unless you want that freezing.
Keep text accessible
A font size written only in cqi, such as font-size: 4cqi, ignores the user's default font size setting. If someone raises their browser's default text size, the heading does not respond, because cqi depends only on the container width.
This is why the bounds and the intercept above are written in rem. A rem term lets the size follow the user's preference. Browser zoom still works. Zooming shrinks the container's width in CSS pixels, which changes the cqi result as well.
Two habits help:
- Write the minimum and maximum in
rem, never inpx. - Add a
rembase to the preferred value (0.6875rem + 2.1875cqi) so the user's text setting still scales it.
Keep the maximum within a reasonable multiple of the minimum. The maximum must not be so small that zooming in to 200% cannot make the text larger than it was at 100%.
Edge cases
The container cannot size itself with cqi
Container units resolve against the nearest ancestor container, never the element's own box. This rule styles the container itself:
.card-wrapper {
container: card / inline-size;
padding: 2cqi; /* resolves against an ancestor container, not .card-wrapper */
}Here 2cqi refers to some outer container. If there is none, it falls back to the small viewport (svi). Move the declaration onto a descendant, such as .card, if you want it tied to .card-wrapper.
cqb needs block containment
cqi works with container-type: inline-size. cqb (1% of the block size) only resolves against a container with block-axis containment, which means container-type: size. With inline-size alone, a cqb value skips that container and looks for another ancestor that does have block containment. If none exists, it falls back to svb. Prefer cqi for type and spacing, because container-type: size also requires the container to have an explicit height.
Nested containers
With nested containers the nearest one wins:
.page {
container: page / inline-size;
}
.panel {
container: panel / inline-size;
}
.panel .note {
padding: 4cqi; /* 4% of .panel, not of .page */
}Units cannot name a container. The container-name syntax belongs to @container, not to unit values. If a descendant needs to scale against an outer container while an inner one exists, restructure the markup or pass a computed length down with a registered <length> property.
A collapsed container gives collapsed sizes
If the container's inline size is 0, every cqi value is 0, and the clamp() minimums take over. This happens when a flex or grid child with container-type: inline-size has no width from its layout. Give the wrapper a definite width, for example flex: 1 1 0 or width: 100%. Do not rely on its content.
Fallback without a container
Without a matching ancestor container, container units fall back to the small viewport units. A clamp() that uses cqi still produces a sensible size in that case, because the bounds still apply. Browsers that do not know container units at all drop the whole declaration as invalid, so put a plain declaration before it:
.card h2 {
font-size: 1.5rem;
font-size: clamp(1.125rem, 0.6875rem + 2.1875cqi, 2rem);
}An older browser ignores the second line, and a current browser overrides the first with it.
Quick reference
| Goal | Value |
|---|---|
| Heading that scales with its container | `clamp(1.125rem, 0.6875rem + 2.1875cqi, 2rem)` |
| Padding tied to the container width | `clamp(0.75rem, 3cqi, 2rem)` |
| Gap between items | `clamp(0.5rem, 2cqi, 1.5rem)` |
| Pure `cqi` size (avoid for text) | `4cqi` |
Exercise
A container is 24rem wide and the root font size is 16px. What is the computed value of clamp(1rem, 0.5rem + 3cqi, 2rem) on a descendant? Then give the container width at which the maximum is first reached.
Show answer
At 24rem, 1cqi = 0.24rem, so 3cqi = 0.72rem and the preferred value is 0.5 + 0.72 = 1.22rem. That lies between 1rem and 2rem, so the result is 1.22rem. The maximum is reached when 0.5rem + 3cqi = 2rem, so 3cqi = 1.5rem and 1cqi = 0.5rem, which means the container is 50rem wide.
Exercise
You want padding of 1rem when the container is 20rem wide and 3rem when it is 50rem wide, with clamp() holding those values outside the range. Write the declaration.
Show answer
Slope = (3 − 1) / (50 − 20) = 0.0667rem per rem of width, so the coefficient is 6.667cqi. Intercept = 1 − 0.0667 × 20 = −0.333rem. The declaration is padding: clamp(1rem, -0.333rem + 6.667cqi, 3rem);. At 20rem, 6.667 × 0.2 = 1.333, and 1.333 − 0.333 = 1rem. At 50rem, 6.667 × 0.5 = 3.333, and 3.333 − 0.333 = 3rem.
Exercise
A developer writes .card-wrapper { container: card / inline-size; font-size: 3cqi; } and the text does not follow the wrapper's width. Why, and how do you fix it?
Show answer
Container units resolve against the nearest ancestor container, not the element itself, so the 3cqi on .card-wrapper refers to an outer container or falls back to the small viewport. Move the font size to a descendant such as .card, or set it on a child element inside the wrapper.