How to detect HTML text-overflow ellipsis with JavaScript?

Spread the love

Sometimes, we want to detect HTML text-overflow ellipsis with JavaScript.

In this article, we’ll look at how to detect HTML text-overflow ellipsis with JavaScript.

How to detect HTML text-overflow ellipsis with JavaScript?

To detect HTML text-overflow ellipsis with JavaScript, we check if offsetWidth is less than scrollWidth.

For instance, we write

const isEllipsisActive = (e) => {
  return e.offsetWidth < e.scrollWidth;
};

to define the isEllipsisActive function.

In it, we get the element e and check if its offsetWidth is less than its scrollWidth.

If it’s true, then there’s overflowing text.

Conclusion

To detect HTML text-overflow ellipsis with JavaScript, we check if offsetWidth is less than scrollWidth.

Leave a Reply

Your email address will not be published.