Back to blog

CSS Typed Arithmetic Turns Lengths Into Plain Numbers

CSS can now divide a length by a length and get a plain number. Use it to let one container width drive line-height, aspect-ratio and more, with no JavaScript.

Close-up of a steel caliper scale marked in both inches and centimeters.
Photo by Bozhin Karaivanov on Unsplash

CSS can now divide a length by another length and hand you a plain number, and that one change lets a single measurement drive properties that were never lengths in the first place. If you have a ResizeObserver whose only job is to write a number into a custom property so CSS can use it, you can probably delete it.

The feature is called CSS typed arithmetic. Chrome shipped it in version 140, Safari in 26, and Firefox 158 joined them on October 13, 2026. That makes it Baseline Newly available.

What actually changed

calc() has always had type rules. You can add 50vw + 2rem because both sides are lengths. You can multiply 200px * 4 because only one side has a unit. Division was the strict one. The thing on the right of the slash had to be a bare number, so calc(100vw / 1px) was simply invalid.

Typed arithmetic relaxes that. When both sides of a division are the same type, the units cancel. calc(640px / 16px) is 40. calc(100cqi / 1ch) is how many characters fit across a container. The units on each side can differ, as long as they measure the same kind of thing.

A unitless number can go anywhere CSS accepts a number. That includes opacity, line-height, aspect-ratio, scale, and color channels. Multiply it by 1deg and it becomes an angle. Multiply it by 1rem and it becomes a length again.

Before this, people got there with a trick. Wrapping a length in tan(atan2(var(--len), 1px)) returns a unitless number, and it usually needed the length registered with @property first. It worked, but nobody reading your stylesheet six months later knew what it did.

Line height that follows the line length

Here is a typography rule most designers already know. Long lines need more space between them, because the eye has farther to travel back to the start of the next line. Doing that in CSS used to mean breakpoints or script.

With typed arithmetic, the paragraph can measure its own column in characters.

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

.post p {
  --chars: calc(100cqi / 1ch);
  line-height: clamp(1.4, 1.15 + var(--chars) / 150, 1.7);
}

100cqi is the full inline size of the nearest container, and 1ch is roughly the width of one character in the paragraph's font. Dividing them gives an estimated character count per line. That works out to about 1.45 at 45 characters per line and 1.65 at 75. The clamp() sets a 1.4 floor, which kicks in near 38 characters, and a 1.7 cap near 83. These numbers are my own illustrative tuning, not a published standard, so adjust them to your type.

The nice detail is that ch resolves against the paragraph's own font. Change the font and the math follows it.

This is the case I run into most on city websites. Content authors drop body copy into Sitefinity layouts, and the same text can end up in a full-width section, a two-column row or a narrow sidebar. I don't control the width, so I can't tune one line-height for all of them. A rule that reads the container and adjusts itself handles every placement without a CSS class for each one.

When you want 0 to 1, reach for progress()

A lot of the old JavaScript looked like this. Measure an element, turn its width into a value between 0 and 1, and push that into CSS.

const feed = document.querySelector(".feed");

const observer = new ResizeObserver(([entry]) => {
  const width = entry.contentRect.width;
  const t = Math.min(Math.max((width - 320) / (960 - 320), 0), 1);
  feed.style.setProperty("--t", t);
});

observer.observe(feed);

You could rebuild that with typed division, but CSS has a function that does exactly this job. progress() became Baseline in September 2026 when Safari 27 and Firefox 155 shipped it. It takes a value, a start, and an end, and returns where the value sits between them as a number clamped between 0 and 1.

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

.card {
  --t: progress(100cqi, 320px, 960px);
  aspect-ratio: calc(4 / 3 + (16 / 9 - 4 / 3) * var(--t));
}

The card is 4/3 when the feed is 320px wide or narrower, 16/9 at 960px and up, and blends between the two in the middle. With an 800px feed, both versions gave a --t of 0.75 in my Chromium test.

My rule of thumb is simple. Use progress() when the question is "how far along is this," and plain division when the question is "how many of these fit."

Know where the number gets calculated

A custom property that nobody registered with @property stores its value as text. The math doesn't run where you declare it. It runs wherever var() pulls it in, and cqi resolves against the nearest container of that element.

So if you set --t: progress(100cqi, 320px, 960px) on a wide wrapper, a child inside a narrower nested container gets a different --t. In my test, the same declaration resolved to 1 at the wrapper and 0.125 inside a 400px nested container.

Often that is exactly what you want. When it isn't, register the property as a number. A registered property is computed on the element that declares it, and the finished number is what children inherit.

@property --t {
  syntax: "<number>";
  inherits: true;
  initial-value: 0;
}

One more surprise is that a container can't measure itself. cqi on the element that has container-type set looks past it to the next container up. If there is none, the unit falls back to the small viewport size.

When to skip it

Typed arithmetic is not a replacement for container queries. If a layout should snap between a few distinct states, a container query says that more clearly than a formula. Save the math for values that should change smoothly.

It is also not something you can lean on everywhere yet. Support sits at about 85 percent of global users, per Can I use in November 2026. Anyone on Safari 18 or an older Firefox is outside it. The usual CSS fallback trick also fails here, and it fails quietly. Normally you write a safe declaration first and the browser drops the line it can't parse. When the math sits inside a custom property, the browser can't tell the value is bad until it substitutes it. At that point the earlier declaration is already gone, and the property falls back to its inherited or initial value. Your safe 1.6 never applies.

Use @supports instead, and keep the safe value as the default.

.post p {
  line-height: 1.6;
}

@supports (line-height: calc(1px / 1px)) {
  .post p {
    --chars: calc(100cqi / 1ch);
    line-height: clamp(1.4, 1.15 + var(--chars) / 150, 1.7);
  }
}

Browsers without typed arithmetic reject that test and keep 1.6. Everyone else gets the adaptive version. That is progressive enhancement doing its job.

On public sector sites, 85 percent isn't a bar I'm trying to clear. It's a question of what the other 15 percent see. Residents use old phones, library computers and locked-down work machines that don't get browser updates. So I only use a feature like this when the fallback is something I'd ship anyway. A fixed 1.6 line-height is fine. If a feature affected whether someone could read the page or finish a form, I'd wait.

Wrap-up

The one thing to remember is that CSS can now cancel units. Divide a length by a length and you get a number, and a number can become an opacity, a ratio, an angle, or a length again. Pair it with progress() for 0 to 1 ranges and with @property when you need the value frozen. If you are trimming script out of a stylesheet's job, it sits nicely next to the features in CSS Can Replace More of Your JavaScript in 2026.

Sources