Check if an element has overflow

True when the content is larger than the element’s visible box.

JavaScriptBrowser · DOM Version 1.0.0 · MIT · .md

check-if-an-element-has-overflow.js
function hasOverflow(element) { return element.scrollHeight > element.clientHeight || element.scrollWidth > element.clientWidth; }

Usage

Markup

fixture.html
<div id="box" style="height: 40px; overflow: hidden"><p style="height: 100px; margin: 0">Long content</p></div>
<div id="roomy" style="height: 200px"><p style="margin: 0">Short</p></div>

Script

usage.js
hasOverflow(document.querySelector('#box')); // → true
hasOverflow(document.querySelector('#roomy')); // → false

Lines marked → were executed in headless Chromium against the markup above, and each result was compared with the value shown. Lines marked e.g. are illustrative.

Know the limits

  • Compares content size with the visible box, whatever the overflow style: use it to decide whether to show a “Show more” control.
  • The sizes are rounded to integers, so sub-pixel overflow is missed. Elements with display: none report 0 for both.

The function above is the code the usage lines run. It does not validate its arguments; add checks where it receives untrusted input.

Source & maintenance

Based on Check if an element has overflow from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.

Documented as a reference with executed usage lines. Original MDX retained as inert source evidence.

Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.

Usage lines executed in headless Chromium against the fixture markup; no external services · reviewed October 28, 2026.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.