CSS Typed OM Reference

CSSNumericValue, CSSKeywordValue, CSSStyleValue types with parse and compute methods

Reference for the CSS Typed Object Model (Houdini) — CSSStyleValue, CSSUnitValue, CSSMathSum, CSSKeywordValue, CSSTransformValue and the attributeStyleMap/computedStyleMap APIs — with creation, arithmetic and unit conversion.

What problem does CSS Typed OM solve?

The classic CSSOM exposes styles as strings, so reading element.style.width gives '100px' that you must parse and re-stringify. Typed OM exposes values as typed objects like CSSUnitValue { value: 100, unit: 'px' }, so you read .value as a number, do arithmetic, and write back without error-prone string concatenation.

The CSS Typed Object Model, part of Houdini, replaces stringly-typed style access with real typed objects. This reference covers the value types and the style-map APIs.

How it works

Instead of strings, styles are objects with numeric fields:

// old CSSOM — strings
el.style.width;                 // "100px"  → must parse

// Typed OM — objects
el.computedStyleMap().get("width");      // CSSUnitValue { value: 100, unit: "px" }
el.attributeStyleMap.set("width", CSS.px(100));

The base class is CSSStyleValue. Concrete subtypes include CSSUnitValue (a number + unit), CSSKeywordValue (an identifier like auto), CSSMathSum/CSSMathProduct (calc expressions), CSSTransformValue and CSSColorValue. The CSS.px(...), CSS.percent(...), CSS.deg(...) and CSS.number(...) factories build unit values directly.

Arithmetic and conversion

CSSNumericValue supports add, sub, mul, div, min, max:

  • Compatible units reduce to a single CSSUnitValueCSS.px(10).add(CSS.px(5))15px.
  • Incompatible units stay symbolic — CSS.px(10).add(CSS.percent(50)) → a CSSMathSum (a calc()), because the result cannot be resolved without layout.
  • .to(unit) converts within a unit family (e.g. CSS.cm(1).to("mm")10mm).

Read computed values with the read-only element.computedStyleMap(); read/write inline values with element.attributeStyleMap. Feature-detect with "computedStyleMap" in Element.prototype.