DOM Window.getComputedStyle() Method
The getComputedStyle() method returns the final values of all CSS properties of an element, after style sheets, inheritance and inline styles are applied.
Description
The getComputedStyle() method returns an object with the resolved values of every CSS property of an element, after all style sheets, inherited values and inline styles are applied. Values are generally in absolute units, such as pixels and rgb() colours. It can also read pseudo-elements such as ::before, and custom properties with getPropertyValue('--name').
The result is read-only; to change styles, use the element's style property or classes. Reading computed styles can force a layout calculation, so avoid doing it in tight loops.
Shorthand properties such as margin may return an empty string in some browsers; read the individual longhands, such as marginTop, instead. For hidden elements with display: none, layout-dependent values such as width may be reported as auto, since the element has no layout.
Syntax
getComputedStyle(element)
getComputedStyle(element, pseudoElement)Parameters
- elementThe element to get styles for.
- pseudoElementOptional. A pseudo-element such as
"::before".
Return Value
A live, read-only CSSStyleDeclaration of the element's final style values.
Example
Browser Support
The following information will show you the current browser support for the Window.getComputedStyle() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





