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

HTML scroll bar textarea attribute setting

1. Overflow content overflow settings (set whether...

How to add automatic completion commands for docker and kubectl on Mac

Introduction to kubectl kubectl is a command line...

Analysis of the methods of visual structure layout design for children's websites

1. Warm and gentle Related address: http://www.web...

Analysis of MySQL duplicate index and redundant index examples

This article uses examples to describe MySQL dupl...

CSS achieves colorful and smart shadow effects

background Ever wondered how to create a shadow e...

MySQL count detailed explanation and function example code

Detailed explanation of mysql count The count fun...

How to set horizontal navigation structure in Html

This article shares with you two methods of setti...

MySQL million-level data paging query optimization solution

When there are tens of thousands of records in th...

A brief discussion on the execution details of Mysql multi-table join query

First, build the case demonstration table for thi...

Example analysis of the impact of MySQL index on sorting

This article uses examples to illustrate the impa...

Using CSS3 and JavaScript to develop web color picker example code

The web color picker function in this example use...

Detailed tutorial on compiling and installing python3.6 on linux

1. First go to the official website https://www.p...

How to configure two or more sites using Apache Web server

How to host two or more sites on the popular and ...

Advantages of MySQL covering indexes

A common suggestion is to create indexes for WHER...