Home >Web Front-end >JS Tutorial >How to Implement Ellipsis with Fixed Width and Height in Multi-Line Text?
Ellipsis with Fixed Width and Height in Multi-Line Text
Multi-line text overflow with ellipsis can be achieved within a fixed width and height container using JavaScript. To understand the concept, imagine a div with predefined dimensions that contains multiple lines of text.
The goal is to ensure that the text is truncated at the specified lines and appends an ellipsis to indicate continuation. Here's how it can be implemented:
<code class="javascript">var $p = $('#fos p'); var divh = $('#fos').height(); while ($p.outerHeight() > divh) { $p.text(function (index, text) { return text.replace(/\W*\s(\S)*$/, '...'); }); }</code>
This approach provides a basic solution for text overflow with ellipsis within fixed bounds. For a more comprehensive implementation, consider implementing server-side truncation or using jQuery plugins that offer enhanced functionality for text truncation.
The above is the detailed content of How to Implement Ellipsis with Fixed Width and Height in Multi-Line Text?. For more information, please follow other related articles on the PHP Chinese website!