Today to do something, encountered this problem, Baidu after summing up the results.
First, you need to know the three properties of CSS.
Overflow:hidden; Out-of-text hiding
Text-overflow:ellipsis; Overflow with ellipsis display
White-space:nowrap; Overflow does not break line
These three are the basic properties of CSS and need to be remembered.
But the third attribute, can only display one row, cannot use here, then if displays many lines?
CSS3 solves this problem with the following workaround:
Display:-webkit-box; Displays the object as an elastic telescopic box model.
-webkit-box-orient:vertical; Arranges child elements vertically from top to bottom (sets the arrangement of child elements of the telescopic box)
-webkit-line-clamp:2; This property is not a canonical property of CSS and requires the combination of the above two properties to represent the number of rows displayed.
The final CSS style is as follows:
Overflow:hidden;
Text-overflow:ellipsis;
Display:-webkit-box;
-webkit-box-orient:vertical;
-webkit-line-clamp:2;
If it is a two-row or three-row container, there is no way to implement this method purely with CSS.
Can provide two methods, one is to use the program output truncation word, and the second is to use JS to determine the number of words interception.
JS Demo is as follows:
<! DOCTYPE html>