Window getComputedStyle()
Example
Get the computed background color of an element:
const cssObj = window.getComputedStyle(element, null);
let bgColor = cssObj.getPropertyValue("background-color");
More examples below.
Description
The getComputedStyle()
method gets the computed CSS properties and values of an HTML element.
The getComputedStyle()
method returns a
CSSStyleDeclaration object
.
Computed Style
The computed style is the style used on the element after all styling sources have been applied.
Style sources: external and internal style sheets, inherited styles, and browser default styles.
See Also:
Syntax
Parameters
The element to get the computed style for.
A pseudo-element to get.
Return Value
More Examples
Get all the computed styles from an element:
const cssObj = window.getComputedStyle(element, null);
let text = "";
for (x in cssObj) {
cssObjProp = cssObj.item(x)
text += cssObjProp + " = " + cssObj.getPropertyValue(cssObjProp) + "<br>";
}
Get computed font size of the first letter in an element (using pseudo-element):
let size = cssObj.getPropertyValue("font-size");
Browser Support
getComputedStyle()
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |