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

MySQL foreign key constraint disable and enable commands

Disabling and enabling MySQL foreign key constrai...

SQL implementation of LeetCode (181. Employees earn more than managers)

[LeetCode] 181.Employees Earning More Than Their ...

Problems encountered by MySQL nested transactions

MySQL supports nested transactions, but not many ...

Basic usage and examples of yum (recommended)

yum command Yum (full name Yellow dog Updater, Mo...

Summary of experience in using div box model

Calculation of the box model <br />Margin + ...

How to query the intersection of time periods in Mysql

Mysql query time period intersection Usage scenar...

Ubuntu 18.04 installs mysql 5.7.23

I installed MySQL smoothly in Ubuntu 16.04 before...

How to use the HTML form attributes readonly and disabled

1. readonly read-only attribute, so you can get th...

Several CSS3 tag shorthands (recommended)

border-radius: CSS3 rounded corners Syntax: borde...

How to set the width and height of html table cells

When making web pages, you often encounter the pr...

How to install PHP7 Redis extension on CentOS7

Introduction In the previous article, we installe...

Enabling or disabling GTID mode in MySQL online

Table of contents Basic Overview Enable GTID onli...