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

Detailed explanation of the use of router-view components in Vue

When developing a Vue project, you often need to ...

Vue implements small search function

This article example shares the specific code of ...

Implementation of deploying war package project using Docker

To deploy war with Docker, you must use a contain...

How to implement variable expression selector in Vue

Table of contents Defining the HTML structure Inp...

How to install setup.py program in linux

First execute the command: [root@mini61 setuptool...

Nginx access control and parameter tuning methods

Nginx global variables There are many global vari...

MySQL 5.7.23 version installation tutorial and configuration method

It took me three hours to install MySQL myself. E...

How to configure CDN scheduling using Nginx_geo module

Introducing the Geo module of Nginx The geo direc...

Detailed explanation of Axios asynchronous communication in Vue

1. First, we create a .json file for interactive ...

About debugging CSS cross-browser style bugs

The first thing to do is to pick a good browser. ...

Detailed description of mysql replace into usage

The replace statement is generally similar to ins...

How to build Jenkins+Maven+Git continuous integration environment on CentOS7

This article takes the deployment of Spring boot ...