JavaScriptDOM and events

getComputedStyle()

Returns an element’s computed styles, meaning the CSS values the browser actually applied after taking every rule into account.

Returns
A live, read-only CSSStyleDeclaration object with the computed values of every CSS property.
On this page

Example

#
JavaScript
const badge = document.querySelector('#badge')
const styles = getComputedStyle(badge)

console.log(badge.style.color === '')
console.log(styles.color)
console.log(styles.fontSize)
console.log(styles.fontWeight)
console.log(styles.borderTopColor)
console.log(styles.display)
Console
true
rgb(167, 139, 250)
20px
700
rgb(167, 139, 250)
inline

element.style is empty, since the color comes from the class. The computed styles show the color in rgb() format, the 1.25rem size in pixels, bold as 700, and currentColor turned into a specific color. inline is the default display mode for a <span> element.

Definition and usage

#

The getComputedStyle() function (the full name is window.getComputedStyle()) returns an object with the final values of every CSS property on an element. It accounts for stylesheets, inline styles, inheritance, and the browser’s default values. It’s the simplest way to check, say, a color set by a class in JavaScript, since element.style only sees styles written into the style attribute.

Values are returned in computed form, usually normalized: colors as rgb(…), lengths in pixels (1.25rem becomes, say, "20px"), and keywords like bold as numbers ("700"). They’re always strings, so convert them to numbers before calculating, e.g. with parseFloat().

The returned object is “live”: after changing a class or style, the next read shows the new value. It’s read-only, so change styles through element.style or classList. The second argument lets you read a pseudo-element’s styles, e.g. getComputedStyle(el, "::before").content, and you read CSS variable values with the getPropertyValue("--name") method.

Syntax

#
Syntax
getComputedStyle(element)
getComputedStyle(element, pseudoElement)

getComputedStyle(element).propertyName
getComputedStyle(element).getPropertyValue(name)

Parameters

#
  • element

    Element

    The element whose computed styles you want to read.
  • pseudoElement

    optional, string

    A pseudo-element, e.g. "::before" or "::after".

More examples

#
Calculations on pixel values
JavaScript
const progress = document.querySelector('#progress')
const { width, paddingLeft } = getComputedStyle(progress)

console.log(width, paddingLeft)
console.log(parseFloat(width) - 2 * parseFloat(paddingLeft))
console.log(width + 10)
Console
300px 10px
280
300px10

Computed values are strings with a unit. parseFloat() extracts the numbers from them, while plain addition concatenates the strings, as you can see in the last line.

A pseudo-element and a CSS variable
JavaScript
const lesson = document.querySelector('#lesson')
const before = getComputedStyle(lesson, '::before')

console.log(before.content)
console.log(before.color)
console.log(getComputedStyle(document.documentElement).getPropertyValue('--accent'))
Console
"✓ "
rgb(16, 185, 129)
#10b981
The styles object is live
JavaScript
const panel = document.querySelector('#panel')
const styles = getComputedStyle(panel)

console.log(styles.display)
panel.classList.add('hidden')
console.log(styles.display)
Console
block
none

There’s no need to call getComputedStyle() again. The same object shows the new value right away after the class changes.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Computed values are strings with units, e.g. "16px". Use parseFloat() before calculating with them.
  • You’ll usually get colors in rgb(…) format, even if they were written in CSS as a #hex value or a name. Compare them in the same format.
  • Reading computed styles can force the browser to recalculate styles and the page layout. Don’t interleave reads with style changes in a loop.

Related entries

#

Spotted a mistake or missing an example? Write to us.