Home >Web Front-end >CSS Tutorial >Is My Text Overflowing and Displaying an Ellipsis?

Is My Text Overflowing and Displaying an Ellipsis?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-11 03:09:10990browse

Is My Text Overflowing and Displaying an Ellipsis?

How to Detect if Text Overflowing and Ellipsis is Active

Overflowing text can be truncated and displayed with an ellipsis (...) using CSS properties like white-space, overflow, and text-overflow. However, detecting which elements' contents are overflowing may be necessary in certain scenarios.

To achieve this, you can employ a JavaScript function like the one provided below:

function isEllipsisActive(e) {
  return (e.offsetWidth < e.scrollWidth);
}

This function accepts an element (typically a span element containing the text) as an argument. It compares the element's offsetWidth (the element's visible width) to its scrollWidth (the total width of the element's content). If the offsetWidth is less than the scrollWidth, it means that the content is being truncated and an ellipsis is active.

To use the function, pass the span element as the argument and check the returned value. A true value indicates that the element's text is overflowing, while a false value indicates normal text display.

The above is the detailed content of Is My Text Overflowing and Displaying an Ellipsis?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn