CSS Typed Object Model: Animating and Reading CSS Values with JavaScript

The CSS Typed Object Model: read and set CSS values as numbers

Reading a style in JavaScript returns strings: getComputedStyle( el ).width is "320px", and changing a value means building a string like `${ x }px`. Parsing and concatenating units is a source of small bugs and of work on every animation frame. The CSS Typed OM represents values as objects with a number and a unit: CSS.px( 320 ), el.computedStyleMap().get( 'width' ).value.

Problem: A theme’s parallax and sticky-header script parses computed styles with parseFloat(), builds transform strings on every scroll frame, and breaks when a value is in rem or % instead of px.

Solution: Read values with computedStyleMap() and convert them with .to( 'px' ), set values with attributeStyleMap.set() and typed objects such as CSS.px() or new CSSTranslate(), and fall back to string styles where the Typed OM is not available.

Read typed values

const header = document.querySelector( '.site-header' );
const styles = header.computedStyleMap();

const height = styles.get( 'height' );           // CSSUnitValue { value: 72, unit: 'px' }
const padding = styles.get( 'padding-top' ).to( 'px' ).value;
const opacity = styles.get( 'opacity' ).value;  // 1, a number

console.log( height.value + padding );           // arithmetic without parseFloat

computedStyleMap() returns a read-only map of computed values as typed objects. Lengths come as CSSUnitValue with value and unit; .to( 'px' ) converts compatible units. Keywords such as auto come as CSSKeywordValue, so check the type before doing math with it.

Set values without strings

const hero = document.querySelector( '.ha-hero__image' );

function haParallax() {
    const offset = window.scrollY * 0.3;
    hero.attributeStyleMap.set( 'transform', new CSSTransformValue( [ new CSSTranslate( CSS.px( 0 ), CSS.px( offset ) ) ] ) );
}

window.addEventListener( 'scroll', () => requestAnimationFrame( haParallax ), { passive: true } );

attributeStyleMap is the typed version of element.style. A transform is a list of components (CSSTranslate, CSSRotate, CSSScale) wrapped in a CSSTransformValue. Typed values skip the string parsing that the browser would do for style.transform = 'translate(0px, 120px)', which matters in code that runs every frame. Scheduling the update with requestAnimationFrame() keeps it to one update per frame: requestAnimationFrame and requestIdleCallback.

Math with units

const gap = CSS.rem( 1.5 );
const total = CSS.px( 300 ).add( gap );    // CSSMathSum: calc(300px + 1.5rem)
card.attributeStyleMap.set( 'width', total );

const max = CSSNumericValue.parse( 'clamp(1rem, 2vw, 2rem)' );
console.log( max.toString() );

Mixed units produce math objects that serialize to calc() expressions, so the browser resolves them at layout time. CSSNumericValue.parse() turns a CSS string into a typed value.

Custom properties

document.documentElement.attributeStyleMap.set( '--ha-progress', CSS.percent( 40 ) );

For registered custom properties (@property with a syntax), computedStyleMap() returns typed values too; unregistered ones come back as unparsed strings: CSS @property.

Fallback

const typed = 'attributeStyleMap' in Element.prototype;
function haSetTranslateY( el, px ) {
    if ( typed ) el.attributeStyleMap.set( 'transform', new CSSTransformValue( [ new CSSTranslate( CSS.px( 0 ), CSS.px( px ) ) ] ) );
    else el.style.transform = `translate(0, ${ px }px)`;
}

NOTE: The Typed OM works in Chrome and Edge since version 66 (2018) and in Safari since 16.4 (March 2023); Firefox does not support it yet, so the fallback is required for a real share of visitors. For many animations, CSS or the Web Animations API avoid per-frame JavaScript altogether: the Web Animations API.