CKTIS / JavaScript
Reading `visibility` with `window.getComputedStyle()`
Read the browser’s resolved CSS `visibility` value and distinguish it from the declaration stored in an element’s inline style.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
window.getComputedStyle(element).visibility
window.getComputedStyle(element, "::before").visibility
window.getComputedStyle(element).getPropertyValue("visibility")
element.style.visibility = "hidden"Window.getComputedStyle and CSSStyleDeclaration.visibility work together when you need the CSS value the browser resolved for an element. window.getComputedStyle(element) returns a live, read-only style object. Its .visibility property returns a string that reflects the cascade and inheritance; getPropertyValue("visibility") reads the same value. The current return interface is CSSStyleProperties, which extends CSSStyleDeclaration; older specifications named CSSStyleDeclaration directly. This is a Web API, not part of ECMAScript 2024.
Omit the second argument or pass null for the element itself. Pass a string such as "::before" to read a pseudo-element’s styles. The first argument must be an Element; passing a non-Element value throws TypeError. If pseudo-element parsing fails or produces ::part() or ::slotted(), the CSSOM algorithm returns a style object with no declarations rather than throwing TypeError. Browsers may mask :visited styles to protect browsing history.
element.style.visibility reads only the inline declaration and returns "" when none is set. To change it, write to element.style.visibility, not to the object returned by getComputedStyle(). Reading .visibility again on the same computed-style object reflects the change, but a previously saved string does not change.
hidden hides the element’s box but keeps its space in layout; a descendant can override the inherited value with visible. display: none removes the box from layout. collapse can also remove space, for example for table rows or columns; its effect depends on the element type and browser support. visible alone does not prove the element is shown on screen: an ancestor could have display: none.
Examples
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Computed and inline visibility</title>
</head>
<body>
<script>
const parentElement = document.createElement("div");
const child = document.createElement("span");
parentElement.style.visibility = "hidden";
child.textContent = "Example";
parentElement.append(child);
document.body.append(parentElement);
try {
const styles = window.getComputedStyle(child);
console.log(JSON.stringify([child.style.visibility, styles.visibility]));
child.style.visibility = "visible";
console.log(JSON.stringify([child.style.visibility, styles.visibility]));
} finally {
parentElement.remove();
}
</script>
</body>
</html>Result
["","hidden"] ["visible","visible"]
Save the example as an HTML file, open it in a browser, and read the output in the developer console. The first line contrasts an unset inline declaration with inherited `hidden`. The second shows that the same style object now reports `visible`.
Related lessons
- JavaScript · Lesson 40 · Intermediate · Version: 3