Add Ellipsis to HTML Tags to Prevent Text Overflow
Maintaining a clean layout in a dynamic webpage can be challenging, especially when content is variable in length. If headlines (h2) in an elastic layout exceed the width of the browser window, they may wrap into multiple lines, creating an undesirable appearance.
Thankfully, implementing an elegant solution to truncate headlines and add an ellipsis indicator (...) is remarkably simple with CSS only. This cross-browser solution works seamlessly on all modern browsers, except Firefox 6.0.
By applying the following CSS styles to the headline tag, you can ensure that the text is displayed on a single line and truncated with ellipsis if necessary:
<code class="css">.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; -o-text-overflow: ellipsis; }</code>
The combination of these properties forces the text to remain on one line with no wrapping, hides any overflow beyond the available width, and adds an ellipsis character to indicate truncation.
For comprehensive support, including multiline text wrapping and compatibility with older versions of Firefox, consider implementing a JavaScript solution.
The above is the detailed content of How Can I Prevent Text Overflow in HTML Tags Using CSS?. For more information, please follow other related articles on the PHP Chinese website!