Sub topic 6
Fluid type and spacing with cqi and clamp()
This chapter assumes you know CSS custom properties, the clamp() function, and how to declare a container with container-type. It also assumes you can write a basic @container rule. Here you will use container query units to scale type and spacing smoothly with the width of the component's container, instead of switching values at fixed breakpoints.
Why units instead of breakpoints
An @container rule changes values in steps. A card is either narrower than 30rem or it is not. Container query units change values continuously. 1cqi equals 1% of the container's inline size. If the container is 400px wide, 1cqi is 4px. If the container grows to 600px, 1cqi is 6px.
These units resolve against the nearest ancestor container that can answer the question. For cqi, that is the nearest ancestor with inline-size containment. The element you style cannot be its own container, so the units always read from an ancestor.
The markup used in this chapter is the same card as before:
<div class="card-wrapper">
<article class="card">
<h2>Title</h2>
<p>Body text that reflows with the wrapper, not the viewport.</p>
</article>
</div>Scaling with cqi
This CSS makes the wrapper a container and uses cqi for padding and heading size:
.card-wrapper {
container: card / inline-size;
}
.card {
padding: 2cqi;
}
.card h2 {
font-size: clamp(1.125rem, 4cqi, 2rem);
}The container shorthand sets the name (card) and the type (inline-size) in one declaration. The padding is 2cqi, so it is 2% of the wrapper's width. The heading uses 4cqi, which is 4% of the wrapper's width.
Why clamp() is needed
A pure cqi value has no limits. In a 200px container, 4cqi is 8px, which is unreadable. In an 1800px container, it is 72px, which is far too large. clamp(min, preferred, max) fixes this:
- The first argument is the smallest allowed size, here
1.125rem. - The second is the preferred value, here
4cqi. - The third is the largest allowed size, here
2rem.
The heading grows with the container only between the two limits. Below the lower limit it stays at 1.125rem. Above the upper limit it stays at 2rem.
The limits are in rem, so they respect the user's font-size setting. A pure cqi size does not. Keeping a rem floor and ceiling is also better for accessibility.
| Container width | 4cqi | Result of clamp() |
|---|---|---|
| 250px | 10px | 18px (1.125rem floor) |
| 400px | 16px | 18px (floor) |
| 600px | 24px | 24px |
| 900px | 36px | 32px (2rem ceiling) |
The table assumes a 16px root font size.
Choosing a unit
Use cqi for most work. It follows the inline axis, which is horizontal in left-to-right languages and vertical in vertical writing modes. Use cqw only when you specifically want physical width.
Block-axis units (cqb, cqh) need a container with block-size containment. Setting container-type: size can provide it, but the container then needs an explicit height, or it collapses. Fluid type almost never needs this, so stay with cqi.
Spacing that scales
You can use the same approach for gaps and margins. A custom property keeps the formula in one place:
.card-wrapper {
container: card / inline-size;
}
.card {
--space: clamp(0.5rem, 3cqi, 1.5rem);
padding: var(--space);
display: grid;
gap: var(--space);
}Note that cqi inside --space is resolved where var(--space) is used. Because a custom property is substituted as text, each element that uses it computes the unit against its own nearest container.
Common errors
- Using cqi on the container itself. If
.card-wrappersetspadding: 2cqi, the unit does not use.card-wrapperas its container. It looks further up the tree. If no ancestor is a container, it falls back to the small viewport unit (1cqibecomes1svi). The result looks wrong without any error. - Forgetting the container declaration. Without
container-type, the units use the fallback and the text scales with the viewport. - Dropping clamp(). The size becomes unbounded and can shrink below a readable value.
- Mixing lengths in clamp().
clamp(1rem, 4cqi, 2rem)is valid. An unquoted value likeclamp(1, 4cqi, 2)is not, because the arguments do not have matching types.
Exercise
A container is 500px wide and the root font size is 16px. What is the computed font size of font-size: clamp(1rem, 5cqi, 1.75rem) on a descendant?
Show answer
5cqi is 25px. The ceiling 1.75rem is 28px and the floor 1rem is 16px. 25px sits between them, so the font size is 25px.
Exercise
Why does padding: 2cqi on the element that has container-type: inline-size not use that element's own width?
Show answer
An element cannot be the query container for itself. Container units resolve against the nearest ancestor container, so the value comes from a higher ancestor or from the viewport fallback.