DEV Community

Cover image for CSS Typed OM: CSS values as objects, not strings
richi.codes
richi.codes

Posted on Originally published at richi.codes

CSS Typed OM: CSS values as objects, not strings

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.attributeStyleMap is a live map of the element's inline styles that you can set(), get(), delete() and clear().
  • 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
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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()); }
}
Enter fullscreen mode Exit fullscreen mode

Other things to know:

  • Firefox doesn't support the color interfaces. Its release notes say CSSColorValue and CSSRGB aren't supported "in Firefox or any other browser". Colors are still strings.
  • Unregistered custom properties come back as CSSUnparsedValue, a list of text tokens. Use CSSNumericValue.parse() to get a number out of them.
  • attributeStyleMap only sees the inline styles (the style attribute or el.style), not your stylesheets. For those, use computedStyleMap().

What to keep an eye on

  • How fast typed set() support catches up between engines for shorthands and properties like rotate. The demo panel is a quick way to check any browser.
  • Color: until CSSColorValue ships somewhere, colors stay strings.
  • When it reaches Baseline widely available. Until then, feature detection and a string fallback.

Sources


Originally published at richi.codes.

Top comments (0)