MDN self-hosted library · e01.org

Range: getClientRects() method

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

html
<div></div>
<pre id="output"></pre>
css
div {
  height: 80px;
  width: 200px;
  background-color: blue;
}
js
const range = document.createRange();
range.selectNode(document.querySelector("div"));
rectList = range.getClientRects();

const output = document.querySelector("#output");
for (const rect of rectList) {
  output.textContent = `${output.textContent}\n${rect.width}:${rect.height}`;
}