Sometimes when designing a web page, you may encounter text that is too long, causing the page to be unsightly. So how to solve this problem? This article will introduce to you the implementation method of css text that exceeds the ellipses in a single line and multi-line text in the ellipses.
First of all, let’s take a look at the CSS method to achieve a single line of text beyond the ellipses.
css To implement the overflow display of ellipsis in a single line of text, you should use thetext-overflow:ellipsis attribute. Of course, you also need to add the width attribute to be compatible with partial browsing.
The implementation code for css single line text exceeding the ellipses:
The effect of css single line text exceeding the ellipsis is as follows:
text-overflow: The ellipsis attribute only supports the overflow display of ellipsis in a single line of text. What if we want to implement the overflow display of ellipsis in multiple lines of text? Next, we will continue to talk about thecss method to implement multi-line text beyond the ellipsis. (For more information on the text-overflow:ellipsis attribute, please refer to thecss manual)
1. CSS realizes that when multi-line text exceeds the ellipsis, it can be directly set with css attributes (only -webkit kernel Only then will it work)
Syntax:
overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
Most mobile browsers are based on WebKit core, so this method is suitable for mobile terminals;
-webkit -line-clamp is used to limit the number of lines of text displayed in a block element. This is an unsupported WebKit property that does not appear in the CSS draft specification.
display: -webkit-box Display the object as a flexible box model.
-webkit-box-orient sets or retrieves the arrangement of the child elements of the flex box object.
text-overflow: ellipsis is used for multi-line text, using ellipses "..." to hide text that exceeds the range.
2. Use absolute positioning and padding to implement multi-line text beyond the ellipsis using css; (cross-browser solution)
Syntax:
p{ position: relative; line-height: 20px; max-height: 40px; overflow: hidden; } p::after{ content: "..."; position: absolute; bottom: 0; right: 0; padding-left: 40px; background: -webkit-linear-gradient(left, transparent, #fff 55%); background: -o-linear-gradient(right, transparent, #fff 55%); background: -moz-linear-gradient(right, transparent, #fff 55%); background: linear-gradient(to right, transparent, #fff 55%); }
The principle of this method is: first embed a...in the element containing text, then leave a position of... (padding-right) on the right side of the element containing text, and finally Use absolute positioning to position... to the padding-right area on the right.
Note: This method has a wide range of applications, but ellipses will also appear when the text does not exceed the line. This method can be optimized with js.
Note:
Set height to an integer multiple of line-height to prevent excess text from being exposed.
Add a gradient background to p::after to prevent only half of the text from being displayed.
Since ie6-7 does not display content content, you need to add tags to be compatible with ie6-7 (such as:...); to be compatible with ie8, you need to replace ::after with :after .
This article ends here. For more information about css text beyond the ellipses, please pay attention to the php Chinese website.
The above is the detailed content of How to realize that css text exceeds the ellipses in a single line and multiple lines?. For more information, please follow other related articles on the PHP Chinese website!