JavaScript Window getComputedStyle() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.getComputedStyle() returns a live, read-only style object with the resolved CSS values for an element after stylesheets and the cascade (MDN).

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call window.getComputedStyle. Start with this line.

JavaScript
getComputedStyle(el[, pseudo])
CallWhat it does
getComputedStyle(el[, pseudo])Primary signature.
window.getComputedStyle(...)Call with the arguments your case needs.

Built into the browser. Call window.getComputedStyle(...) (or the global getComputedStyle(...)) — no import required.

Library declarations
JavaScript
getComputedStyle(el[, pseudo])

Parameters

ParameterDescription
elementRequired. Must be an Element (else TypeError)
pseudoEltOptional. Pseudo selector string, e.g. "::before"

Return Value

Return ValueDescription
*Live read-only style object

JavaScript Examples

Example 1 — Basic Read

Read computed color and font-size with getPropertyValue.

JavaScript
const el = document.getElementById("box");
const styles = window.getComputedStyle(el);

console.log(styles.getPropertyValue("color"));
console.log(styles.getPropertyValue("font-size"));
Try It Yourself

How It Works

  1. Find the element. Here #box.
  2. Get computed styles. Cascade is already applied.
  3. Read each property. Hyphenated CSS names.
  4. Colors serialize. Often as rgb(...) / rgba(...).

Example 2 — vs element.style

Inline style is only what you set; computed includes the cascade.

JavaScript
const el = document.getElementById("box");
// only inline:
console.log("inline color:", el.style.color);
// final rendered:
console.log("computed color:", getComputedStyle(el).color);
Try It Yourself

How It Works

  1. Check el.style. Empty string if not set inline.
  2. Check computed. Always has a resolved value (or default).
  3. Same property, different sources. Classes/CSS files show up only in computed.
  4. Write with style/classes. Never assign on the computed object.

Example 3 — CamelCase Access

The returned object also exposes camelCase property names.

JavaScript
const styles = getComputedStyle(document.getElementById("box"));
console.log(styles.fontSize);       // same idea as getPropertyValue("font-size")
console.log(styles.backgroundColor);
Try It Yourself

How It Works

  1. Call getComputedStyle. Keep the object.
  2. Use camelCase. font-size → fontSize.
  3. Same resolved values. Just a different accessor.
  4. Prefer getPropertyValue when the name is dynamic or custom.

Example 4 — Pseudo-element

Pass a second argument to read ::before / ::after styles.

JavaScript
const h3 = document.querySelector("h3");
const after = getComputedStyle(h3, "::after");
console.log(after.getPropertyValue("content"));
// e.g. '" rocks!"' when CSS sets content: " rocks!";
Try It Yourself

How It Works

  1. Select the host element. Pseudo-elements are not in the DOM tree as nodes.
  2. Pass "::after". Second argument to getComputedStyle.
  3. Read content. Often quoted in the returned string.
  4. Invalid pseudo (e.g. ::part()) can throw TypeError.

Example 5 — parseFloat for Math

Computed lengths include units — strip them when you need a number.

JavaScript
const styles = getComputedStyle(document.getElementById("box"));
const widthPx = parseFloat(styles.width);
console.log(styles.width); // e.g. "200px"
console.log(widthPx);      // e.g. 200
Try It Yourself

How It Works

  1. Read width. Usually a string with px.
  2. Use parseFloat. Keeps the leading number.
  3. Do math on the number. Add padding, compare sizes, etc.
  4. Layout note. For some properties the resolved value is the used (layout) value.

Browser Support

Window.getComputedStyle() is marked Baseline Widely available on MDN (since July 2015). Browser CSSOM — not a Node.js built-in.

✓ Baseline · Widely available

getComputedStyle()

Read resolved CSS values across modern browsers.

99% Core tag support
Chrome Yes
Full support
Edge Yes
Full support
Firefox Yes
Full support
Safari Yes
Full support
Opera Yes
Full support
HTML element 100% supported

Bottom line: Safe default for reading final styles. Prefer classes for writing styles.

Frequently Asked Questions

It returns a live, read-only style object with the resolved CSS values for an element after stylesheets, inheritance, and the cascade are applied.
No. MDN marks Window.getComputedStyle() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
element.style only shows inline styles (the style attribute / JS assignments). getComputedStyle() shows what the browser actually uses, including classes and stylesheets.
No. Computed styles are read-only. Change appearance with element.style, CSS classes, or stylesheets.
An optional string like "::before" or "::after". Pass it as the second argument to read styles of that pseudo-element.
Both work. getPropertyValue("font-size") uses CSS names with hyphens. CamelCase (styles.fontSize) is also supported on the returned object.

Next: matchMedia()

Test CSS media queries with window.matchMedia().

matchMedia() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful