Get to know MDN better
Some CSSOM APIs serialize property values into standardized string representations based on the value's data type. For example, you might set a color using the hsl(240 100% 50%) syntax, but when accessed through JavaScript, the value will be returned in the equivalent "rgb(0, 0, 255)" syntax.
CSS data types can often be expressed in multiple syntaxes. For example, the <color> data type can be represented using named colors (red), hexadecimal notation (#ff0000), functional notation (rgb(255 0 0)), and more. These different syntaxes are exactly equivalent at every stage of CSS value processing, similar to how in JavaScript, the same string can be written with single quotes or double quotes, or the same number can be written in different formats (like 16, 16.0, or 0x10).
Because CSS converts all these surface representations to the same underlying value during value processing, it is often impossible to recover the original syntax from the already-parsed CSSOM. Furthermore, a canonical representation is often more useful for scripts, because it allows comparisons and calculations based on how the content is presented to the user, rather than how it was originally authored.
Serialization happens whenever CSS property values are read as strings through JavaScript APIs, such as:
Different APIs return CSSStyleDeclaration objects at different stages of value processing, which have slightly different serialization behaviors. For example, Window.getComputedStyle() and HTMLElement.style returns the resolved value of properties, while CSSStyleRule.style returns more or less the declared value.
Note: The CSS Typed OM API is able to represent units and other CSS syntaxes; however, style declarations retrieved from an element are still processed and don't preserve the original syntax. For example, CSS.cm(1).toString() returns "1cm" instead of serializing to pixels, but element.computedStyleMap().get("margin-left").toString() returns the resolved pixel value.
Each CSS value type has an associated serialization format defined by the CSS specifications. Some common rules include:
Note that <percentage> values often get computed into absolute dimensions (like <length>) during value processing, so they may not appear as percentages when serialized from computed styles. For dimensions with units, such as <frequency>, <length>, <resolution>, and <time>, the serialized unit depends on the context and is not well-specified. getComputedStyle() and element.style serialize them into Hz, px, dppx, and s respectively.
When serializing the value for shorthand properties, its constituent longhand properties are serialized and combined according to the rules for that shorthand.
Note: There are a lot of complex details regarding how CSS properties are serialized, especially for complex properties like font. They may be unspecified in the specifications or even inconsistent across browsers. You need to test and verify the behavior for your specific use case.
Colors are among the most common types affected by serialization. Regardless of whether you define a color using hsl(), hwb(), a keyword, or a modern color space, JavaScript usually returns it in legacy rgb() or rgba() format.
The following examples demonstrate how different color formats are serialized when accessed through JavaScript.
Lengths are another common case. Relative units (like em, %) are often resolved to absolute pixels when serialized through JavaScript APIs.
This normalization allows scripts to compare or calculate lengths consistently.
| CSS Object Model (CSSOM) # serialize-a-css-value |
| CSS Color Module Level 4 # serialization |
This page was last modified on Dec 17, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.