-
Notifications
You must be signed in to change notification settings - Fork 23k
docs: Add reference for CSS value serialization, especially colors #41322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
84438aa
docs: Add reference for CSS value serialization, especially colors
Shrinivassab ad17c8f
Merge branch 'main' into css-value-serialization
Shrinivassab eb3a19d
Update files/en-us/web/api/css_object_model/serializing_css_values/in…
Shrinivassab 0461c6a
Addressed PR comments
Shrinivassab d1661fd
Merge branch 'css-value-serialization' of https://github.com/Shriniva…
Shrinivassab 391a84d
Rewrite
Josh-Cena fc7f1c6
Update files/en-us/web/api/css_object_model/css_value_serialization/i…
Shrinivassab cc3a9a8
Update files/en-us/web/api/css_object_model/css_value_serialization/i…
Shrinivassab 766657b
Update files/en-us/web/api/css_object_model/css_value_serialization/i…
Shrinivassab 3b829f8
Update files/en-us/web/api/css_object_model/css_value_serialization/i…
Shrinivassab 8c1dd71
Update files/en-us/web/api/css_object_model/css_value_serialization/i…
Shrinivassab d8040e9
Merge branch 'main' into css-value-serialization
Josh-Cena fb9973c
Rewrite
Josh-Cena 8f991c4
Merge branch 'main' into css-value-serialization
estelle a7a4a39
Apply suggestions from code review
Josh-Cena c1db814
Add guide links
Josh-Cena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
files/en-us/web/api/css_object_model/css_value_serialization/index.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| --- | ||
| title: CSS value serialization | ||
| slug: Web/API/CSS_Object_Model/CSS_value_serialization | ||
| page-type: guide | ||
| spec-urls: | ||
| - https://drafts.csswg.org/cssom/#serialize-a-css-value | ||
| - https://drafts.csswg.org/css-color-4/#serialization | ||
| --- | ||
|
|
||
| {{APIRef("CSSOM")}} | ||
|
|
||
| When working with CSS through JavaScript APIs, property values are **serialized** into standardized string representations. For example, you might set a color using the `hsl(240 100% 50%)` syntax, but when accessed through JavaScript, the value may be returned in the equivalent `"rgb(0, 0, 255)"` syntax. | ||
|
|
||
| ## When and how values are serialized | ||
|
|
||
| Serialization ensures that CSS values are exposed to JavaScript in a consistent format across browsers. Without this, equivalent values could be returned in different syntaxes (for example, `#f00` vs. `rgb(255, 0, 0)`), breaking code that relies on string comparisons. | ||
Shrinivassab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Serialization happens whenever CSS values are read as strings through JavaScript APIs, such as: | ||
|
|
||
| - {{domxref("CSSStyleDeclaration.getPropertyValue()")}} | ||
| - {{domxref("CSSStyleDeclaration.cssText")}} | ||
| - Accessing properties directly on {{domxref("CSSStyleDeclaration")}} objects (e.g., `element.style.backgroundColor`) | ||
|
|
||
| Note that different APIs retrieve the value at different stages of [value processing](/en-US/docs/Web/CSS/CSS_cascade/Value_processing), which have slightly different serialization behaviors. | ||
|
|
||
| Each CSS value type has an associated serialization format defined by the CSS specifications. Some common rules include: | ||
|
|
||
| - Keywords (like `auto`, `block`, `none`) serialize to all lowercase. | ||
| - [`<color>`](/en-US/docs/Web/CSS/color_value): | ||
| - For sRGB colors: `rgb(R, G, B)` or `rgba(R, G, B, A)`, where all arguments are numbers, and the `rgb` form is selected if the alpha is exactly `1`. The keyword `transparent` serializes as `rgba(0, 0, 0, 0)`. | ||
Shrinivassab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - For `lab()`, `lch()`, `oklab()`, `oklch()`, and `color()` colors: the function form is preserved, with numeric arguments. | ||
estelle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - [`<length>`](/en-US/docs/Web/CSS/length): resolved to `px`. | ||
| - [`<angle>`](/en-US/docs/Web/CSS/angle): resolved to `deg`. | ||
| - [`<percentage>`](/en-US/docs/Web/CSS/percentage): preserved as a percentage. | ||
| - [`<time>`](/en-US/docs/Web/CSS/time): resolved to `s`. | ||
| - [`<frequency>`](/en-US/docs/Web/CSS/frequency): resolved to `Hz`. | ||
| - [`<resolution>`](/en-US/docs/Web/CSS/resolution): resolved to `dppx`. | ||
| - [`<url>`](/en-US/docs/Web/CSS/url): serialized with `url("...")`, with the URL resolved to an absolute URL. | ||
Josh-Cena marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| For shorthand properties, its constituent longhand properties are serialized and combined according to the rules for that shorthand. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Color value serialization | ||
|
|
||
| Colors are one of the most common cases 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 `rgb()` or `rgba()` format. | ||
Shrinivassab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| The following examples demonstrate how different color formats are serialized when accessed through JavaScript. | ||
|
|
||
| ```html | ||
| <div class="example hsl">HSL Color</div> | ||
| <div class="example hwb">HWB Color</div> | ||
| <div class="example named">Named Color</div> | ||
| <div class="example alpha">Transparent Color</div> | ||
| <pre id="output"></pre> | ||
| ``` | ||
|
|
||
| ```css | ||
| .example { | ||
| padding: 10px; | ||
| margin: 5px; | ||
| color: white; | ||
| } | ||
|
|
||
| .hsl { | ||
| background-color: hsl(240 100% 50%); | ||
| } | ||
|
|
||
| .lab { | ||
| background-color: lab(100% 0 0); | ||
| } | ||
|
|
||
| .named { | ||
| background-color: blue; | ||
| } | ||
|
|
||
| .alpha { | ||
| background-color: hsl(120 50% 50% / 0.3); | ||
| } | ||
| ``` | ||
|
|
||
| ```js | ||
| const examples = document.querySelectorAll(".example"); | ||
| const output = document.getElementById("output"); | ||
|
|
||
| examples.forEach((element) => { | ||
| const style = getComputedStyle(element); | ||
| output.textContent += `${element.className}: ${style.getPropertyValue("background-color")}\n`; | ||
| }); | ||
| ``` | ||
|
|
||
| {{EmbedLiveSample("Color serialization examples", 600, 200)}} | ||
|
|
||
| ### Length value serialization | ||
|
|
||
| Lengths are another common case. Relative units (like `em`, `%`) are often resolved to absolute pixels when serialized through JavaScript APIs. | ||
|
|
||
| ```js | ||
| element.style.marginLeft = "2em"; | ||
| console.log(getComputedStyle(element).marginLeft); | ||
| // "32px" (depending on font size) | ||
| ``` | ||
|
|
||
| This normalization allows scripts to compare or calculate lengths consistently. | ||
|
|
||
| ## Specifications | ||
|
|
||
| {{Specifications}} | ||
|
|
||
| ## See also | ||
|
|
||
| - [`CSSStyleDeclaration.getPropertyValue()`](/en-US/docs/Web/API/CSSStyleDeclaration/getPropertyValue) | ||
| - [`Window.getComputedStyle()`](/en-US/docs/Web/API/Window/getComputedStyle) | ||
| - [CSS colors](/en-US/docs/Web/CSS/CSS_colors) | ||
| - [CSS \<color>](/en-US/docs/Web/CSS/color_value) | ||
Shrinivassab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.