Example 1 — Read scrollWidth
Measure the full content width of a narrow box with wide text.
const box = document.getElementById("box");
// width: 120px; overflow-x: auto; long nowrap text
console.log(box.scrollWidth); // e.g. 320 (full content width) How It Works
- Find the box.
getElementById("box")returns the element. - Read the property.
scrollWidthis the full content width in CSS pixels. - Include overflow. Hidden parts of the line still count toward the total.
- Result. A value ≥
clientWidth(larger when content overflows).

