This note has an interactive demo. Try it in the original
Since Firefox 161 shipped on November 24, 2026, JavaScript in every major engine can read and write CSS as typed objects ({ value: 3, unit: "cm" }) instead of strings like "3cm". The CSS Typed Object Model has been in Chrome since version 66 and in Safari since 16.4. Firefox was the last one missing, so it's now Baseline.
It sounds like a small detail, and it mostly is. But anyone who has written el.style.width = size + 'px' and then parseFloat(getComputedStyle(el).width) to read it back knows how much glue code it replaces.
What it is and why it matters
The classic CSSOM speaks in strings. You build them by concatenating values and units, and you parse them back with parseFloat and a little hope. The Typed OM hands you objects instead. A length is a CSSUnitValue with a value and a unit. A calc() comes back as a CSSMathSum whose parts you can inspect. A keyword like auto is a CSSKeywordValue. MDN notes that turning strings into typed JavaScript values and back through HTMLElement.style "can incur a significant performance overhead", and the Typed OM is there to skip that round trip.
There are three entry points:
-
CSS.px(),CSS.cm(),CSS.turn(),CSS.number()and the rest of the numeric factory functions create values. -
el.attributeStyleMapis a live map of the element's inline styles that you canset(),get(),delete()andclear(). -
el.computedStyleMap()gives you a read-only map of computed styles.
How it works
This is the same code the demo above runs:
const orb = document.querySelector('.orb');
// Before: build a string, then parse it back
orb.style.width = 3 + 'cm';
parseFloat(getComputedStyle(orb).width); // 113.375, a string turned into a number
// After: a number with a unit
orb.attributeStyleMap.set('width', CSS.cm(3));
const w = orb.attributeStyleMap.get('width');
w.value; // 3
w.unit; // "cm"
w.to('px').value; // 113.38582677165354
to() is what makes the size slider in the demo work. When you switch the unit from cm to in, the demo runs CSS[prev](n).to(next) and the orb keeps its size. It converts only between units with a fixed relationship: absolute lengths (px, cm, mm, Q, in, pt, pc), angles, times, frequencies and resolutions. Relative units such as em, vw and % can't be converted, and trying throws a TypeError:
CSS.turn(1.5).to('deg').toString(); // "540deg"
try {
CSS.px(1).add(CSS.em(1)).to('px');
} catch (e) {
e instanceof TypeError; // true: an em depends on the font size
}
A typed value also works anywhere a string is expected, because toString() serializes it as valid CSS. The spin slider uses that to feed a custom property that rotates a conic-gradient:
orb.style.setProperty('--spin', CSS.turn(0.25).to('deg')); // "90deg"
The opacity slider shows the difference between the two maps. Set opacity to 3 and attributeStyleMap keeps the 3 you wrote, but computedStyleMap() returns 1, because the computed value is clamped. It's the same example Chrome uses in its own documentation:
orb.attributeStyleMap.set('opacity', CSS.number(3));
orb.attributeStyleMap.get('opacity').value; // 3
orb.computedStyleMap().get('opacity').value; // 1
There's one more difference worth knowing. computedStyleMap() returns computed values, so a width: 50% stays 50%. getComputedStyle() returns resolved values, which for that width means pixels.
Browser support and gotchas
According to the web-features data on webstatus.dev, it shipped in Chrome 66 (2018), Safari 16.4 (2023) and Firefox 161 (November 2026). That makes it Baseline newly available, not widely available yet, so detect support and keep a string fallback:
const hasTypedOM = 'CSS' in window && 'px' in CSS && 'attributeStyleMap' in HTMLElement.prototype;
That's what the demo does. In a browser without the Typed OM it shows a warning, applies strings and converts units with its own table (96 px per inch). That table is exactly the code the Typed OM lets you delete.
The interesting part is the "tested right now" panel. Each row really runs a set() in your browser and catches the error. In my runs with Playwright, Chromium 151 threw a TypeError for set('padding', CSS.px(12)), for set('rotate', CSS.deg(30)) and for set('width', CSS.percent(50).add(CSS.px(20))). WebKit 26.5 accepted all three. Chromium accepted the same values as strings, though (set('rotate', '30deg') works). My conclusion: not every engine accepts a typed value for every property yet, so wrap set() and fall back to the string:
function setTyped(el, prop, value) {
try { el.attributeStyleMap.set(prop, value); }
catch { el.style.setProperty(prop, value.toString()); }
}
Other things to know:
-
Firefox doesn't support the color interfaces. Its release notes say
CSSColorValueandCSSRGBaren't supported "in Firefox or any other browser". Colors are still strings. -
Unregistered custom properties come back as
CSSUnparsedValue, a list of text tokens. UseCSSNumericValue.parse()to get a number out of them. -
attributeStyleMaponly sees the inline styles (thestyleattribute orel.style), not your stylesheets. For those, usecomputedStyleMap().
What to keep an eye on
- How fast typed
set()support catches up between engines for shorthands and properties likerotate. The demo panel is a quick way to check any browser. - Color: until
CSSColorValueships somewhere, colors stay strings. - When it reaches Baseline widely available. Until then, feature detection and a string fallback.
Sources
- CSS Typed Object Model API, MDN
- CSSNumericValue.to(), MDN
- Firefox 161 release notes for developers, MDN
- Working with the new CSS Typed Object Model, Chrome for Developers
- CSS Typed OM Level 1, specification
Originally published at richi.codes.
Top comments (0)