Home >Web Front-end >JS Tutorial >How to Implement Ellipsis for Multi-line Text Overflow within a Fixed Container?
In web development, it's often necessary to display multi-line text within a container of specified width and height. However, when the text exceeds the available space, it can create an unsightly overflow. To address this, developers often seek methods to implement an ellipsis, indicating that there's more text that is hidden.
One such scenario is where the text is contained within a
Using jQuery provides a convenient way to manipulate the text content of the
To ensure that the overflow remains invisible, we utilize the overflow: hidden; CSS property on the
Here's the CSS and jQuery code for this solution:
<code class="css">#fos { width: 300px; height: 190px; overflow: hidden; } #fos p { padding: 10px; margin: 0; }</code>
<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 offers a simple and efficient solution to the problem of multi-line text overflow with ellipsis handling within a container of fixed width and height. By combining jQuery with CSS, you can achieve the desired visual effect while maintaining accessibility even when JavaScript is disabled.
The above is the detailed content of How to Implement Ellipsis for Multi-line Text Overflow within a Fixed Container?. For more information, please follow other related articles on the PHP Chinese website!