Question Details

No question body available.

Tags

html resize width

Answers (1)

May 29, 2026 Score: 0 Rep: 19,666 Quality: Low Completeness: 70%

Naively, you can use the HTMLElement.offsetWidth property to calculate the widest element. More than likely, the widest element will be causing the viewport scrollbars to appear. The trick is, though, this doesn't account for elements nested inside ancestors whose overflow is hidden or scrollable. In your case, this is plain old HTML with minimal styling, so this should do the trick:

let widest = null;

document.querySelectorAll("body *").forEach(el => { if (!widest) { widest = el; } else if (el.offsetWidth > widest.offsetWidth) { widest = el; } });

console.log("The widest element is:", widest);