This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The Window.getComputedStyle() method returns an object containing the values of all CSS properties of an element, after applying active stylesheets and resolving any basic computation those values may contain.
Individual CSS property values are accessed through APIs provided by the returned CSSStyleDeclaration object, or by indexing with CSS property names. The values returned by getComputedStyle are resolved values.
getComputedStyle(element) getComputedStyle(element, pseudoElt)
elementThe Element for which to get the computed style.
pseudoElt OptionalA string specifying the pseudo-element to match. Omitted (or null) for real elements.
A live CSSStyleDeclaration object, which updates automatically when the element's styles are changed.
Note that:
CSSStyleDeclaration object contains active values for CSS property longhand names as well as shorthand names. For example, the returned object contains entries for border-bottom-width in addition to the border-width and border shorthand property names.:visited for examples of how this is implemented.getComputedStyle returns the original property value in Firefox, but the final property value in WebKit.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.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)).The returned object is the same CSSStyleDeclaration type as the object returned from the element's style property. However, the two objects have different purposes:
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.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.TypeErrorIf the passed object is not an Element or the pseudoElt is not a valid pseudo-element selector or is ::part() or ::slotted().
Note: Valid pseudo-element selector refers to syntactic validity, e.g., ::unsupported is considered valid, even though the pseudo-element itself is not supported.
In this example we style a <p> element, then retrieve those styles using getComputedStyle(), and print them into the text content of the <p>.
<p>Hello</p>
p {
width: 400px;
margin: 0 auto;
padding: 20px;
font: 2rem/2 sans-serif;
text-align: center;
background: purple;
color: white;
}
const para = document.querySelector("p");
const compStyles = window.getComputedStyle(para);
para.textContent =
`My computed font-size is ${compStyles.getPropertyValue("font-size")},\n` +
`and my computed line-height is ${compStyles.getPropertyValue(
"line-height",
)}.`;
getComputedStyle can pull style info from pseudo-elements, such as ::after, ::before, ::marker, or ::line-marker.
<h3>Generated content</h3>
h3::after {
content: " rocks!";
}
const h3 = document.querySelector("h3");
const result = getComputedStyle(h3, "::after").content;
console.log("the generated content is: ", result); // returns ' rocks!'
| Specification |
|---|
| CSS Object Model (CSSOM)> # dom-window-getcomputedstyle> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
getComputedStyle |
1 | 12 | 1Before version 62 this function returnednull when called on a Window with no presentation (e.g. an iframe with display: none; set). Since 62 it returns a CSSStyleDeclaration object with length 0, containing empty strings (bug 1467722; also see bug 1471231 for further work). |
7.2 | 3 | 18 | 4Before version 62 this function returnednull when called on a Window with no presentation (e.g. an iframe with display: none; set). Since 62 it returns a CSSStyleDeclaration object with length 0, containing empty strings (bug 1467722; also see bug 1471231 for further work). |
10.1 | 1 | 1.0 | 4.4 | 1 |
pseudo_element_support |
11 | 12 | 3.5 | ≤15 | 5.1 | 18 | 4 | ≤14 | 5 | 1.0 | 4.4 | 5 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle