Problem:
How can we create an ellipsis on a <div> with a fixed width and multiple lines, ensuring that the overflowed text is truncated?
Solution:
To achieve this, we can utilize a jQuery snippet to repeatedly remove the last word of the text until it fits within the desired height.
Implementation:
) to contain the text.
Create a jQuery function to perform the truncation. This function will:
element and store it in the $p variable.
with the updated, truncated text.
Additional Considerations:
CSS to ensure it takes up the full width of the <div>.
Example:
HTML
<div>
CSS
#fos { width: 300px; height: 190px; overflow: hidden; } #fos p { width: 100%; padding: 10px; margin: 0; }
jQuery
var $p = $('#fos p'); var divh = $('#fos').height(); while ($p.outerHeight() > divh) { $p.text(function (index, text) { return text.replace(/\W*\s(\S)*$/, '...'); }); }
Demo: https://jsfiddle.net/w0n5cy2j/
The above is the detailed content of How to Create Multi-Line Text Overflow with Ellipsis in a Fixed-Width Div?. For more information, please follow other related articles on the PHP Chinese website!