MDN self-hosted library · e01.org

Window: getDefaultComputedStyle() method

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

js
const elem = document.getElementById("elemId");
const style = getDefaultComputedStyle(elem);
html
<div id="elem-container">dummy</div>
<div id="output"></div>
css
#elem-container {
  position: absolute;
  left: 100px;
  top: 200px;
  height: 100px;
}
js
const elem = document.getElementById("elem-container");
const theCSSprop = getDefaultComputedStyle(elem).position;
document.getElementById("output").textContent = theCSSprop; // outputs "static"
html
<h3>generated content</h3>
css
h3::after {
  content: " rocks!";
}
js
const h3 = document.querySelector("h3");
const result = getDefaultComputedStyle(h3, "::after").content;

console.log("the generated content is: ", result); // returns 'none'