Menu

Stop Using JavaScript to Strip Units. CSS Typed Arithmetic Is Baseline
📰

Stop Using JavaScript to Strip Units. CSS Typed Arithmetic Is Baseline

DEV Community: css·JKC·about 13 hours ago
#fuWHMLCq
#devtocss#devto#dev#calc#opacity
Reading 0:00
15s threshold

TL;DR: CSS can now divide a length by a length, so calc(100vw / 1px) gives you a plain, unitless number. Firefox 158 shipped it on October 13, 2026, joining Chrome and Safari, so CSS typed arithmetic is Baseline Newly available.

The resize listener that only exists to delete "px"

You want a background image to fade as the viewport gets narrower. Or a heading whose line-height tightens as its container grows. Both need the same thing: a width, as a number.

CSS had the width. It just refused to hand it over without a unit attached. So you wrote this:

const root = document . documentElement ;

new ResizeObserver ( ( [ entry ] ) => { root . style . setProperty ( "--w" , entry . contentRect . width ) ; } ) . observe ( root ) ;

Enter fullscreen mode

Exit fullscreen mode

A whole observer, running on every resize, whose only job is to remove two letters. CSS knew the answer the entire time.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.