In the realm of web development, the need arises to elegantly handle text overflow within a container with specified width and height. How can we append ellipses (...) to indicate truncation on a multi-line
Possible Solution:
jQuery offers a solution with the following markup and CSS:
<code class="html"><div id="fos"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit...</p> </div></code>
<code class="css">#fos { width: 300px; height: 190px; overflow: hidden; } #fos p { padding: 10px; margin: 0; }</code>
The jQuery code repeatedly trims the last word of the text until the desired height is reached:
<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>
Considerations:
Demonstration:
A live demonstration is available on jsFiddle: https://jsfiddle.net/5638d9y0/
Additional Resources:
The above is the detailed content of How can we append ellipses (...) to indicate truncation on a multi-line `` element with a fixed width and height?. For more information, please follow other related articles on the PHP Chinese website!