No Login Data Private Local Save

CSS Typed OM Playground - Online attributeStyleMap Explorer

8
0
0
0

CSS Typed OM Playground

Explore element.attributeStyleMap โ€” the typed, programmatic way to manipulate CSS

Typed OM Supported Not Supported
The demo box above receives all Typed OM operations
2nd operand
0 props

No properties set via attributeStyleMap yet.
Use Set or click a Preset above.

Property Type Value
๐ŸŸข Ready โ€” CSS Typed OM Playground initialized. Try a preset or set a property!
๐Ÿ“‹ Demo element: div#cto-demo-box
Frequently Asked Questions
CSS Typed OM (part of the CSS Houdini APIs) provides a typed, object-based interface for manipulating CSS values in JavaScript. Instead of working with strings like element.style.width = '200px', you use typed objects such as CSSUnitValue, CSSMathValue, and CSSKeywordValue. The attributeStyleMap is a property on every DOM element that returns a StylePropertyMap โ€” a Map-like object for getting and setting CSS properties using these typed values. It provides better performance, type safety, and eliminates string parsing.
Featureelement.styleattributeStyleMap
Value formatStrings onlyTyped objects (CSSUnitValue, etc.)
Type safetyNone (string parsing)Full type information preserved
Math operationsRequires calc() stringsCSSMathValue objects, programmatic
IterationManual parsingMap-like: for...of, entries(), keys()
PerformanceString concat + reparseDirect typed manipulation, faster
Browser supportUniversalChrome 66+, Edge 79+, Opera 53+
Both ultimately affect the same inline styles โ€” they are synchronized. Setting via one method is reflected in the other.
  • CSSUnitValue โ€” Represents a numeric value with a unit. E.g., new CSSUnitValue(200, 'px') or shorthand CSS.px(200). Properties: .value (number) and .unit (string).
  • CSSKeywordValue โ€” Represents a CSS keyword like block, none, auto, or color names like red. Created via new CSSKeywordValue('block').
  • CSSMathValue โ€” Abstract base for mathematical expressions. Subclasses include CSSMathSum, CSSMathProduct, CSSMathMin, CSSMathMax, and CSSMathClamp. These enable programmatic calc()-like operations without string building.
  • CSSTransformValue โ€” Represents a CSS transform as an array of transform components like CSSTranslate, CSSRotate, CSSScale.
As of 2024, CSS Typed OM is supported in Chromium-based browsers: Google Chrome 66+, Microsoft Edge 79+, Opera 53+, Samsung Internet 9.2+, and Android WebView 66+. Firefox and Safari have not yet shipped full support (tracking in Firefox Bug 1709411 and WebKit Bug 205737). You can feature-detect using if (typeof CSSUnitValue === 'function') or if (element.attributeStyleMap) and gracefully fall back to element.style for unsupported browsers.
Yes! CSS custom properties (variables like --my-color) work seamlessly with attributeStyleMap. You can set them using element.attributeStyleMap.set('--my-color', new CSSKeywordValue('blue')) or with parsed values. This is particularly powerful because it allows type-safe manipulation of custom properties that might be consumed by other parts of your stylesheet via var(). Try the Custom Props preset in this playground to see it in action.
Traditional CSS manipulation via element.style requires converting between strings and internal typed representations repeatedly โ€” every time you read a value, the browser serializes it to a string; every time you write, it parses the string back. With Typed OM, values stay in their typed representation, eliminating serialization and parsing overhead. For animation-heavy applications or frequent style updates (e.g., during scroll, pointer events, or Web Animations API usage), this can significantly reduce jank and improve frame rates. Google's benchmarks show up to ~30% faster style operations in some scenarios.
Since attributeStyleMap returns a StylePropertyMap (which behaves like a Map), you can iterate using standard patterns:
// Using for...of
for (const [prop, value] of element.attributeStyleMap) {
  console.log(prop, value.toString(), value.constructor.name);
}
// Using entries()
for (const [prop, value] of element.attributeStyleMap.entries()) {
  console.log(`${prop}: ${value}`);
}
// Using forEach
element.attributeStyleMap.forEach((value, prop) => {
  console.log(prop, value);
});
You can also use .keys(), .values(), .has(prop), and .size.
Shorthand CSS properties like border, margin, padding, background, and font represent multiple longhand properties simultaneously. The Typed OM generally requires you to use longhand properties (e.g., margin-top, border-left-width) when setting values via attributeStyleMap.set(). Attempting to set a shorthand may throw a TypeError. This is by design โ€” it ensures type precision. The playground above primarily uses longhand properties for this reason. You can still get computed values of shorthands via element.computedStyleMap().get('margin') in some cases, but the returned value structure varies.
Key API Reference
Factory Methods
  • CSS.px(200) โ†’ CSSUnitValue {value: 200, unit: 'px'}
  • CSS.em(1.5) โ†’ CSSUnitValue {value: 1.5, unit: 'em'}
  • CSS.percent(80) โ†’ CSSUnitValue {value: 80, unit: '%'}
  • CSS.deg(45) โ†’ CSSUnitValue {value: 45, unit: 'deg'}
  • CSS.vw(50), CSS.vh(100), CSS.s(2)
Math Operations
  • new CSSMathSum(CSS.px(100), CSS.px(50))
  • new CSSMathProduct(CSS.px(10), 3)
  • new CSSMathMin(CSS.px(100), CSS.px(200))
  • new CSSMathMax(CSS.vw(50), CSS.px(300))
  • new CSSMathClamp(CSS.px(50), CSS.px(100), CSS.px(200))
StylePropertyMap Methods
  • .set(prop, CSSStyleValue) โ€” Set a typed value
  • .get(prop) โ€” Returns CSSStyleValue or null
  • .has(prop) โ€” Returns boolean
  • .delete(prop) โ€” Remove a property
  • .clear() โ€” Remove all inline styles
  • .size โ€” Number of properties