CKTIS / JavaScript
Window.getComputedStyle — reading resolved styles
Window.getComputedStyle reads an element's resolved CSS values after stylesheets have been applied. It returns a live, read-only object rather than a copy of inline declarations.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
window.getComputedStyle(element)
window.getComputedStyle(element, pseudoElt)Window.getComputedStyle, called as window.getComputedStyle(element, pseudoElt), is a CSSOM browser API, not an ECMAScript function. The method is widely supported; having Node 24 does not establish its availability. element must be an Element object. Optional pseudoElt, such as "::before", selects a pseudo-element; omission or null means the element itself.
The result contains resolved values: usually computed values, but used values for some layout-dependent properties. It need not preserve the stylesheet's original notation. Property reads return strings. Element.computedStyleMap() differs: it exposes computed values as typed objects rather than resolved strings.
The returned CSSStyleProperties inherits from CSSStyleDeclaration; earlier specifications named the latter type. Read values using, for example, styles.marginLeft or styles.getPropertyValue('margin-left'). The object is live: another read reflects a style change. To keep a snapshot, save the string you read. The result is read-only; change styles through element.style, which represents inline declarations rather than the outcome of stylesheets.
An invalid element argument causes TypeError. Under the CSSOM algorithm, the method attempts to parse a pseudoElt string as a pseudo-element selector only if it starts with :. If parsing fails or the selector specifies ::part() or ::slotted(), the algorithm returns a style object with an empty declaration list. Under that algorithm, a string without a leading colon, such as "before", is ignored: the method reads the element's own styles. The MDN method reference, by contrast, documents TypeError for invalid pseudo-element selectors and for ::part() and ::slotted(). For privacy, values concerning :visited may deliberately be inaccurate—do not use this method to detect browsing history.
Examples
(() => {
const frame = document.createElement('iframe');
document.body.append(frame);
try {
const doc = frame.contentDocument;
const box = doc.createElement('div');
box.style.cssText = 'margin-left: 12px;';
doc.body.append(box);
const styles = frame.contentWindow.getComputedStyle(box);
console.log(styles.getPropertyValue('margin-left'));
box.style.marginLeft = '24px';
console.log(styles.marginLeft);
} finally {
frame.remove();
}
})();Result
12px 24px
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.