In the article on fluid typography with clamp(), we calculated a linear interpolation to make text grow between two known sizes, without having to guess what should happen at every width in between.

It worked well: a wider screen, larger text. But what if that text lives in a card that takes up a third of the screen? And what if the same card then appears in a sidebar?

The viewport is still huge, but the space available to our component might be quite small, and the heading hasn’t noticed.

The line still works

Before changing anything, let’s separate the roles of our variables:

  • --D1 and --D2 are the minimum and maximum sizes we want for the text.
  • --T1 and --T2 are the reference dimensions at which it should reach those sizes.
  • T is the dimension available at any given moment.

In the first article, T was the viewport width. Now we want it to be the container’s. The calculation is the same; only what we’re measuring changes.

D = a × T + b

a = (D2 - D1) / (T2 - T1)
b = D1 - a × T1

We don’t need to convert --D2 or --T2 to percentages. These variables still define two points on our line. What changes is the unit we use to express the measurement: where we used 100vw, we’ll use 100cqw or 100cqi.

From viewport to container

Container units let us express lengths relative to a query container:

  • 1cqw is 1% of its physical width.
  • 1cqi is 1% of its inline size, the dimension along the axis in which text flows.

cqi is the logical unit corresponding to the physical unit cqw. The i refers to inline-size, and the w to width. They match in horizontal writing; in vertical writing, the inline axis becomes vertical.

In horizontal writing, both measurements match. In vertical writing, the inline axis is vertical, so they don’t represent the same thing. For a scale that should follow the space available for text layout, cqi usually expresses the intention better.

But first, we need to establish the container:

<article class="card">
  <h2 class="card__title">A heading that knows its space</h2>
  <p>The same component can live in very different places.</p>
</article>
.card {
  container-type: inline-size;
}

With inline-size, we can query the size along the inline axis. That’s enough for this example, and it avoids also containing the block size, as size would.

The container must be an ancestor of the element using the unit. Declaring container-type on the h2 itself doesn’t make its font-size calculate relative to its own size.

First, the same example

In the previous article, we wanted to go from 30px to 80px between 375px and 1440px. Keeping those two points, the expression becomes:

.card__title {
  font-size: clamp(30px, calc(12.394366px + 4.694836cqi), 80px);
}

The heading now measures approximately 30px when the container’s inline size is 375px, and 80px when it reaches 1440px. The decimals are rounded; the line is calculated using the same fractions as in the previous article.

The version with cqw expresses that progression relative to the physical width:

.card__title {
  font-size: clamp(30px, calc(12.394366px + 4.694836cqw), 80px);
}

With writing-mode: horizontal-tb and the container in this example, the result is the same. To work with other writing modes, check that the container allows queries on the axis used by the unit.

The points need to make sense too

Changing the unit adapts the reference, but it doesn’t adapt the design decisions. A card that will never be 1440px wide won’t reach that maximum text size either. It’s not a problem with clamp(); we’ve asked it to go too far.

Let’s define a more reasonable range for our example:

  • 320px container: 24px text.
  • 800px container: 48px text.
a = (48 - 24) / (800 - 320) = 0.05
b = 24 - 0.05 × 320 = 8

D = 0.05 × T + 8

With T and D measured in pixels, this expression means that the text size is 5% of the container’s inline size, plus 8 pixels.

Since 1cqi represents 1% of that dimension, the full size of the container is expressed as 100cqi. Taking 5% therefore gives us:

0.05 × 100cqi = 5cqi

The 8px is the intercept of our line. Translating the expression into CSS gives us calc(5cqi + 8px), which we can use inside clamp() to keep the size within our limits:

.card__title {
  font-size: clamp(24px, calc(8px + 5cqi), 48px);
}

For example, if the container’s inline size is 560px:

1cqi = 560px / 100 = 5.6px
5cqi = 28px

Text size = 28px + 8px = 36px
Container size Text size
240px 24px (minimum)
320px 24px
560px 36px
800px 48px
960px 48px (maximum)

All of this can happen without changing the window size. Just place the card in a narrower or wider column.

A configurable version

To avoid recalculating the expression every time the design changes, we can keep the variables from the previous article. In this example, the numbers are unitless and represent design measurements in pixels. This makes the slope a number and avoids dividing lengths by other lengths.

.card {
  container-type: inline-size;
}

.card__title {
  --D1: 24;
  --D2: 48;
  --T1: 320;
  --T2: 800;

  --a: calc((var(--D2) - var(--D1)) / (var(--T2) - var(--T1)));
  --b: calc(var(--D1) - var(--a) * var(--T1));
  --fluid: calc(var(--a) * 100cqi + var(--b) / 16 * 1rem);

  font-size: clamp(calc(var(--D1) / 16 * 1rem), var(--fluid), calc(var(--D2) / 16 * 1rem));
}

Here, we’ve expressed the limits and the intercept in rem, using a 16px reference. With that root font size, we get the values in the table. If the user changes the root font size, those terms change too: we no longer promise to reach the same pixel sizes at the same points.

Mixing rem and cqi allows part of the formula to respond to text preferences, but it doesn’t guarantee accessible typography on its own. We need to test zoom, increased text size, line wrapping, and whether any content gets clipped.

And the two conditions for the formula to make sense: --T2 must be greater than --T1, and --D2 must not be smaller than --D1 for an increasing scale.

A few pitfalls

The first is thinking that cqi means “the parent”. It doesn’t. It uses the nearest ancestor that allows queries on the relevant axis (container-type). Adding another container between the card and the heading can change the reference without touching the formula. Adding a container-name doesn’t, by itself, fix which container cqi refers to either.

The second is forgetting to establish the container. If there isn’t an eligible one, these units fall back to the small viewport on the corresponding axis. The text can keep growing and make everything look as though it’s working, even though we’re measuring something else.

The third is adding containment to any element without reviewing the layout. container-type affects how its size is calculated, so the space should come from the surrounding layout, such as a grid column. If the width previously depended on the content, check the result.

And one last thing: we don’t need to write an @container rule to use cqi. The units give us continuous variation. Queries with @container are useful when we also want to change a composition, such as switching a card from a vertical to a horizontal layout above a certain size.

Conclusion

Linear interpolation knows nothing about screens. It only relates two measurements, and we were the ones who chose the viewport as the reference.

With cqi, we can make text respond to the space available to its component while keeping the same calculation. A card heading no longer needs to know how wide the window is: it just needs to know where it’s going to live.

Resources