If you realize the overflow display of a single line of text, you should all know that you can use the text-overflow:ellipsis attribute, and of course you need to add the width attribute to be compatible with partial browsing.
Implementation:
overflow: hidden;
text-overflow:ellipsis;
white-space: nowrap;
The effect is as shown in the figure:
But this property only supports the overflow display ellipsis of single-line text, what if we want to implement multi-line text overflow display ellipsis.
Next, let's focus on multi-line text overflow and display ellipsis, as follows.
Implementation:
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
The effect is as shown in the figure:
Scope of application:
This method is suitable for WebKit browsers and mobile terminals due to the use of WebKit's CSS extension properties;
Note:
- -webkit-line-clamp is used to limit the number of lines of text displayed in a block element. To achieve this effect, it needs to combine other WebKit properties. Common binding properties:
- display: -webkit-box; A property that must be combined to display the object as a flexible box model.
- -webkit-box-orient A property that must be combined to set or retrieve the arrangement of the child elements of the flexbox object.
Implementation:
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 effect is as shown in the figure:
Scope of application:
This method has a wide range of applications, but ellipsis will also appear when the text does not exceed the line. This method can be optimized in combination 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 avoid half of the text.
- Since ie6-7 does not display content content, add tags to be compatible with ie6-7 (eg: <span>…<span/>); for compatibility with ie8, replace ::after with :after.
Article source: http://www.daqianduan.com/6179.html