How to implement CSS to display ellipsis when single-line or multi-line text overflows

How to implement CSS to display ellipsis when single-line or multi-line text overflows

1. Single row overflow

1. If a single line overflows, the excess part will be displayed...or intercepted. The premise must be width.
CSS: {width:xxpx;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;}, intercepted as clip;

Implementation code:

width:300px;    
overflow: hidden;    
text-overflow:ellipsis;    
whitewhite-space: nowrap;

The effect is as shown below:


However, this property only supports the display of ellipsis for overflow of single-line text. What if we want to display ellipsis for overflow of multiple lines of text?

Next, let’s focus on displaying ellipsis when multi-line text overflows, as follows.

2. Multi-line overflow

{display:-webkit-box;overflow:hidden;text-overflow:ellipsis;-webkit-line-clamp:2;-webkit-box-orient:vertical;}

Implementation method:

display: -webkit-box;    
-webkit-box-orient: vertical;    
-webkit-line-clamp: 3;    
overflow: hidden;

The effect is as shown below:


Scope of application:

Since WebKit's CSS extended properties are used, this method is applicable to WebKit browsers and mobile devices;

Note:

1.-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 be combined with other WebKit properties. Common combination properties:
2.display: -webkit-box; This must be combined with the property to display the object as an elastic box model.
3.-webkit-box-orient must be combined with the property to set or retrieve the arrangement of the child elements of the telescopic box object.

Implementation method:

p{position: relative; line-height: 20px; max-height: 40px; overflow: hidden;}    
p::after{content: "..."; position: absolute; bottombottom: 0; rightright: 0; padding-left: 40px;    
background: -webkit-linear-gradient(left, transparent, #fff 55%);    
background: -o-linear-gradient(rightright, transparent, #fff 55%);    
background: -moz-linear-gradient(rightright, transparent, #fff 55%);    
background: linear-gradient(to rightright, transparent, #fff 55%);    
}

Scope of application:
This method has a wide range of applications, but ellipsis will appear when the text does not exceed the line. This method can be optimized in combination with js.

Note:

1. Set the height to an integer multiple of the line-height to prevent the text that exceeds the height from being exposed.
2. Adding a gradient background to p::after can prevent the text from being displayed only halfway.
3. Since IE6-7 does not display content, you need to add tags to be compatible with IE6-7 (such as: <span>…<span/>); to be compatible with IE8, you need to replace ::after with :after.

123WORDPRESS.COM editor adds:

IE-based browsers must define line-height and height, and -webkit-line-clamp means a few lines, for example

line-height: 20px;

max-height: 40px;

display: -webkit-box;

-webkit-box-orient: vertical;

-webkit-line-clamp: 2;

overflow: hidden;

-webkit-line-clamp

-webkit-line-clamp is an unsupported WebKit property that does not appear in the CSS draft specification.
Limits the number of lines of text displayed in a block element. To achieve this effect, it needs to combine other exotic WebKit properties. Common combination properties:
display: -webkit-box; is a required attribute that displays the object as an elastic box model.
-webkit-box-orient must be combined with the property to set or retrieve the arrangement of the child elements of the flex box object.
text-overflow can be used to hide the text that exceeds the range with ellipsis "..." in the case of multiple lines of text.

<<:  CSS scroll-snap scroll event stop and element position detection implementation

>>:  HTML Marquee character fragment scrolling

Recommend

How to separate static and dynamic state by combining Apache with Tomcat

Experimental environment Apache and Tomcat are bo...

Basic tutorial on controlling Turtlebot3 mobile robot with ROS

Chinese Tutorial https://www.ncnynl.com/category/...

Windows 10 installation vmware14 tutorial diagram

Software Download Download software link: https:/...

How to get datetime data in mysql, followed by .0

The data type of MySQL is datetime. The data stor...

How to change the default character set of MySQL to utf8 on MAC

1. Check the character set of the default install...

mysql 5.7.18 winx64 password change

After MySQL 5.7.18 is successfully installed, sin...

Pitfalls and solutions for upgrading MySQL 5.7.23 in CentOS 7

Preface Recently, I found a pitfall in upgrading ...

Detailed explanation of Vue development website SEO optimization method

Because the data binding mechanism of Vue and oth...

Docker pull image and tag operation pull | tag

I re-read the source code of the Fabric project a...

MySQL 8.0.22 installation and configuration graphic tutorial

MySQL8.0.22 installation and configuration (super...

Use of kubernetes YAML files

Table of contents 01 Introduction to YAML files Y...

How to design and create adaptive web pages

With the popularization of 3G, more and more peop...