You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: files/en-us/web/api/element/computedstylemap/index.md
+58-2Lines changed: 58 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -25,10 +25,14 @@ None.
25
25
26
26
### Return value
27
27
28
-
A {{domxref("StylePropertyMapReadOnly")}} interface.
28
+
A {{domxref("StylePropertyMapReadOnly")}} object.
29
+
30
+
Unlike {{domxref("Window.getComputedStyle")}}, the return value contains [computed values](/en-US/docs/Web/CSS/CSS_cascade/Value_processing#computed_value), not [resolved values](/en-US/docs/Web/CSS/CSS_cascade/Value_processing#resolved_value). For most properties, they are the same, except a few layout-related properties, where the resolved value is the [used value](/en-US/docs/Web/CSS/CSS_cascade/Value_processing#used_value) instead of the computed value. See the [comparison with `getComputedStyle()`](#comparison_with_getcomputedstyle) example for details.
29
31
30
32
## Examples
31
33
34
+
### Getting default styles
35
+
32
36
We start with some simple HTML: a paragraph with a link, and a definition list to which
33
37
we will add all the CSS Property / Value pairs.
34
38
@@ -79,16 +83,68 @@ In [browsers that support `computedStyleMap()`](#browser_compatibility),
79
83
you'll see a list of all the CSS properties and values.
Did you realize how many default CSS properties a link had? Update the `document.querySelector("a")`
85
89
to `document.querySelector("p")`, and you'll notice a difference in the `margin-top`
86
90
and `margin-bottom` default computed values.
87
91
92
+
### Comparison with getComputedStyle()
93
+
94
+
{{domxref("Window.getComputedStyle()")}} returns [resolved values](/en-US/docs/Web/CSS/CSS_cascade/Value_processing#resolved_value), while `computedStyleMap()` returns [computed values](/en-US/docs/Web/CSS/CSS_cascade/Value_processing#computed_value). These are usually the same, but for some properties, the resolved value is the [used value](/en-US/docs/Web/CSS/CSS_cascade/Value_processing#used_value) instead of the computed value. For example, percentage values for widths are resolved to pixel values _post-layout_, so the used values are in pixels, while the computed values are still in percentages.
95
+
96
+
Note that the way we present it makes the two APIs seem more similar than they are. `computedStyleMap()` contains [CSS Typed OM](/en-US/docs/Web/API/CSS_Typed_OM_API) objects, while `getComputedStyle()` contains strings. The former presents the same information in a more structured and processable way.
97
+
98
+
In this example, the `width` property is specified as a percentage, so the computed value is given as a percentage, but the resolved value is given in pixels. The `height` is always in pixels. The `background-color` is a named color, but it is computed to an RGB value.
Copy file name to clipboardExpand all lines: files/en-us/web/api/window/getcomputedstyle/index.md
+24-71Lines changed: 24 additions & 71 deletions
Original file line number
Diff line number
Diff line change
@@ -13,8 +13,7 @@ The
13
13
containing the values of all CSS properties of an element, after applying active
14
14
stylesheets and resolving any basic computation those values may contain.
15
15
16
-
Individual CSS property values are accessed through APIs provided by the object, or by
17
-
indexing with CSS property names.
16
+
Individual CSS property values are accessed through APIs provided by the returned {{DOMxRef("CSSStyleDeclaration")}} object, or by indexing with CSS property names. The values returned by `getComputedStyle` are [resolved values](/en-US/docs/Web/CSS/CSS_cascade/Value_processing#resolved_value).
object, which updates automatically when the element's styles are changed.
35
+
A _live_ {{DOMxRef("CSSStyleDeclaration")}} object, which updates automatically when the element's styles are changed.
36
+
37
+
Note that:
38
+
39
+
- The returned {{DOMxRef("CSSStyleDeclaration")}} object contains active values for CSS property _longhand_ names as well as shorthand names. For example, the returned object contains entries for {{cssxref("border-bottom-width")}} in addition to the {{cssxref("border-width")}} and {{cssxref("border")}} [shorthand property names](/en-US/docs/Web/CSS/Shorthand_properties).
40
+
- Returned values are sometimes deliberately inaccurate. To avoid the "CSS History Leak" security issue, browsers may lie about the computed styles for a visited link, returning values as if the user never visited the linked URL. See [Plugging the CSS history leak](https://blog.mozilla.org/security/2010/03/31/plugging-the-css-history-leak/) and [Privacy-related changes coming to CSS `:visited`](https://hacks.mozilla.org/2010/03/privacy-related-changes-coming-to-css-vistited/) for examples of how this is implemented.
41
+
- During [CSS transitions](/en-US/docs/Web/CSS/CSS_transitions), `getComputedStyle` returns the original property value in Firefox, but the final property value in WebKit.
42
+
- In Firefox, properties with the value `auto` return the used value, not the value `auto`. So if you apply `top:auto` and `bottom:0` on an element with `height:30px` and a containing block of `height:100px`, Firefox's computed style for `top` returns `70px`, as 100 − 30 = 70.
43
+
- For compatibility reasons, serialized color values are expressed as [`rgb()`](/en-US/docs/Web/CSS/color_value/rgb) colors if the alpha channel value is exactly `1`, and `rgba()` colors otherwise. In both cases, legacy syntax is used, with commas as separators (for example `rgb(255, 0, 0)`).
44
+
45
+
The returned object is the same {{DOMxRef("CSSStyleDeclaration")}} type as the object returned from the element's {{DOMxRef("HTMLElement/style", "style")}} property. However, the two objects have different purposes:
46
+
47
+
- The object from `getComputedStyle` is read-only, and should be used to inspect the element's style — including those set by a `<style>` element or an external stylesheet.
48
+
- The `element.style` object should be used to **set** styles on that element, or inspect styles directly added to it from JavaScript manipulation or the global `style` attribute.
38
49
39
50
### Exceptions
40
51
@@ -52,17 +63,19 @@ object, which updates automatically when the element's styles are changed.
52
63
53
64
## Examples
54
65
66
+
### Retrieving computed styles
67
+
55
68
In this example we style a {{HTMLElement("p")}} element, then retrieve those styles
56
69
using `getComputedStyle()`, and print them into the text content of the
The returned object is the same {{DOMxRef("CSSStyleDeclaration")}} type as the object
98
-
returned from the element's {{DOMxRef("HTMLElement/style", "style")}} property. However,
99
-
the two objects have different purposes:
100
-
101
-
- The object from `getComputedStyle` is read-only, and should be used to
102
-
inspect the element's style — including those set by a `<style>`
103
-
element or an external stylesheet.
104
-
- The `element.style` object should be used to
105
-
**set** styles on that element, or inspect styles directly added to it
106
-
from JavaScript manipulation or the global `style` attribute.
107
-
108
-
The first argument must be an {{domxref("Element")}}. Non-elements, like a
109
-
{{DOMxRef("Text")}} node, will throw an error.
110
-
111
-
## defaultView
112
-
113
-
In many code samples, `getComputedStyle` is used from the
114
-
{{DOMxRef("document.defaultView")}} object. In nearly all cases, this is needless, as
115
-
`getComputedStyle` exists on the `window` object as well. It's
116
-
likely the `defaultView` pattern was a combination of folks not wanting to
117
-
write a testing spec for `window` and making an API that was also usable in
118
-
Java.
119
-
120
-
## Use with pseudo-elements
108
+
### Use with pseudo-elements
121
109
122
110
`getComputedStyle` can pull style info from pseudo-elements (such as
123
111
`::after`, `::before`, `::marker`,
@@ -140,42 +128,6 @@ Java.
140
128
</script>
141
129
```
142
130
143
-
## Notes
144
-
145
-
- The returned {{DOMxRef("CSSStyleDeclaration")}} object contains active values for
146
-
CSS property **_longhand_** names as well as shorthand names. For example, the returned object contains entries for
147
-
{{cssxref("border-bottom-width")}} in addition to the {{cssxref("border-width")}} and
148
-
{{cssxref("border")}} [shorthand property names](/en-US/docs/Web/CSS/CSS_cascade/Shorthand_properties). You can query values with longhand names like
149
-
{{cssxref("font-size")}} as well as shorthand names like {{cssxref("font")}}.
150
-
- CSS property values may be accessed using the
151
-
{{DOMxRef("CSSStyleDeclaration.getPropertyValue", "getPropertyValue(propName)")}} method or by indexing directly into the object
152
-
using array or [dot notation](/en-US/docs/Learn_web_development/Core/Scripting/Object_basics#dot_notation) such as `obj['z-index']` or `obj.zIndex`.
153
-
- The values returned by `getComputedStyle` are [resolved values](/en-US/docs/Web/CSS/CSS_cascade/Value_processing#resolved_value).
154
-
These are usually the same as CSS 2.1's
155
-
[computed values](/en-US/docs/Web/CSS/CSS_cascade/Value_processing#computed_value), but for some older properties
156
-
like `width`, `height`, or `padding`, they are
157
-
instead the same as [used values](/en-US/docs/Web/CSS/CSS_cascade/Value_processing#used_value). Originally, CSS
158
-
2.0 defined the _computed values_ as the "ready to be used" final values of
159
-
properties after cascading and inheritance, but CSS 2.1 redefined them as pre-layout,
160
-
and _used values_ as post-layout. For CSS 2.0 properties,
161
-
`getComputedStyle` returns the old meaning of computed values, now called
162
-
**used values**. An example difference between pre- and post-layout
163
-
values includes the resolution of percentages for `width` or
164
-
`height`, as those will be replaced by their pixel equivalent only for
165
-
_used values_.
166
-
- Returned values are sometimes deliberately inaccurate. To avoid the "CSS History
167
-
Leak" security issue, browsers may lie about the computed styles for a visited link,
168
-
returning values as if the user never visited the linked URL. See [Plugging the CSS history leak](https://blog.mozilla.org/security/2010/03/31/plugging-the-css-history-leak/) and [Privacy-related changes coming to CSS `:visited`](https://hacks.mozilla.org/2010/03/privacy-related-changes-coming-to-css-vistited/) for examples of how this is implemented.
169
-
- During [CSS transitions](/en-US/docs/Web/CSS/CSS_transitions),
170
-
`getComputedStyle` returns the original property value in Firefox, but the
171
-
final property value in WebKit.
172
-
- In Firefox, properties with the value `auto` return the used value, not
173
-
the value `auto`. So if you apply `top:auto` and
174
-
`bottom:0` on an element with `height:30px` and a containing
175
-
block of `height:100px`, Firefox's computed style for `top`
176
-
returns `70px`, as 100 − 30 = 70.
177
-
- For compatibility reasons, serialized color values are expressed as [`rgb()`](/en-US/docs/Web/CSS/color_value/rgb) colors if the alpha channel value is exactly `1`, and `rgba()` colors otherwise. In both cases, legacy syntax is used, with commas as separators (for example `rgb(255, 0, 0)`).
0 commit comments