Languages
[Edit]
EN

JavaScript - detect if text overflows element (horizontally or vertically)

3 points
Created by:
Giles-Whittaker
739

In this article, we would like to show you how to detect if text overflows element horizontally or vertically in JavaScript.

Quick solution:

function isTextOverflowing(element) {
    return (element.clientWidth < element.scrollWidth || element.clientHeight < element.scrollHeight);
}


// Usage example:

const element = document.querySelector('#element');
    
if (isTextOverflowing(element)) {
    // ...
}

 

Practical example

In this example, we check if the element's clientWidth or clientHeight values are less than its scrollWidth or clientHeight values to determine if the text is overflowing the element horizontally or vertically.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    div.text {
        padding: 5px;
        border: 1px solid red;
        width: 200px;        /*   <------ limits element width          */
        height: 23px;        /*   <------ limits element height         */
        white-space: nowrap; /*   <------ prevents text from wrapping   */
        overflow: hidden;    /*   <------ hides content overflowing     */
    }

  </style>
</head>
<body>
  <div id="text-1" class="text">
    Short text
  </div>
  <div id="text-2" class="text">
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
  </div>
  <div id="text-3" class="text">
    1<br />
    2<br />
    3<br />
  </div>
  <script>

    function isTextOverflowing(element) {
        return (element.clientWidth < element.scrollWidth || element.clientHeight < element.scrollHeight);
    }


    // Usage example:

    const text1 = document.querySelector('#text-1');
    const text2 = document.querySelector('#text-2');
    const text3 = document.querySelector('#text-2');

    console.log(isTextOverflowing(text1));  // false
    console.log(isTextOverflowing(text2));  // true
    console.log(isTextOverflowing(text3));  // true

  </script>
</body>
</html>

 

See also

  1. JavaScript - detect if text overflows element horizontally

  2. JavaScript - detect if text overflows element vertically

References

  1. Element.clientWidth - Web APIs | MDN
  2. Element.clientHeight - Web APIs | MDN

Alternative titles

  1. JavaScript - check if text overflows element (horizontally or vertically)
  2. JavaScript - detect if text is overflowing element (horizontally or vertically)
  3. JavaScript - check if text is overflowing element (horizontally or vertically)
  4. JavaScript - detect text overflow (vertical or horizontal)
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join