Home Web Front-end CSS Tutorial How to achieve the effect of single-line and multi-line text exceeding the display omission in css

How to achieve the effect of single-line and multi-line text exceeding the display omission in css

Nov 21, 2018 pm 03:06 PM

The content of this article is about how to achieve the effect of single-line and multi-line text beyond display and omission in css. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Single line text omitted

How to achieve the effect of single-line and multi-line text exceeding the display omission in css

1

2

3

4

5

6

7

8

.ellipsis-line {

border: 1px solid #f70505;

padding: 8px;

width: 400px;

overflow: hidden;

text-overflow: ellipsis; //文本溢出显示省略号

white-space: nowrap; //文本不会换行

}

Copy after login

Syntax:

1

text-overflow:clip/ellipsis;

Copy after login

Default value: clip

Applies to: All elements

clip: When the text in the object overflows, the omission mark (...) is not displayed, but the overflowed part is cut off.

ellipsis: Displays an ellipsis mark (…) when the text within the object overflows.

When using it, sometimes it is found that the omitting mark effect does not appear. After testing, it is found that when using ellipsis, it must be combined with overflow:hidden; white-space:nowrap; width: specific value; these three Styles are effective when used together.

Multiple lines of text are omitted

Directly use the css attribute-webkit-line-clamp:n; to set

in WebKit browser or mobile terminal (mostly WebKit kernel browser) page implementation is relatively simple, you can directly use WebKit's CSS extended attribute (WebKit is a private attribute) -webkit-line-clamp; note: this is an unsupported attribute (unsupported WebKit property), it does not appear in CSS specification draft.

-webkit-line-clamp is used to limit the number of lines of text displayed in a block element. In order to achieve this effect, it needs to be combined with other WebKit properties. Commonly combined attributes:

display: -webkit-box; must be combined to display the object as a flexible box model.

-webkit-box-orient must be combined with the attribute to set or retrieve the arrangement of the child elements of the flex box object.

text-overflow: ellipsis;, can be used in the case of multi-line text, using the ellipsis "..." to hide the text that exceeds the range.

This attribute is only suitable for WebKit browsers or mobile browsers (most of which are WebKit core) browsers

1

2

3

4

5

6

7

8

9

.multi-line {

border: 1px solid #f70505;

width: 400px;

overflow: hidden;

text-overflow: ellipsis;

display: -webkit-box;

-webkit-line-clamp: 3;

-webkit-box-orient: vertical;

}

Copy after login

The effect is as shown in the figure:

How to achieve the effect of single-line and multi-line text exceeding the display omission in css

From the effect point of view, its advantages are:

1. Responsive truncation, making adjustments according to different widths

2. The ellipsis will be displayed only when the text exceeds the range, otherwise it will not Display the ellipsis

3. The browser implements it natively, so the ellipsis position is displayed exactly

, but the shortcoming is also very direct, because -webkit-line-clamp is an irregular attribute and it does not appear in CSS specification draft. In other words, only browsers with webkit core support this attribute. Browsers such as Firefox and IE do not support this attribute, and the browser compatibility is not good.

Usage scenarios: Mostly used for mobile pages, because mobile device browsers are more based on the webkit kernel. In addition to poor compatibility, the truncation effect is good.

Use positioning and pseudo-class elements

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

p{

position: relative;

width:400px;

line-height: 20px;

max-height: 60px;

overflow: hidden;

}

p::after{

content: "…";

position: absolute;

bottom: 0;

right: 0;

padding-left: 40px;

background: -webkit-linear-gradient(left, transparent, #fff 55%);

background: -o-linear-gradient(right, transparent, #fff 55%);

background: -moz-linear-gradient(right, transparent, #fff 55%);

background: linear-gradient(to right, transparent, #fff 55%);

}

Copy after login

The effect is as shown in the figure:

How to achieve the effect of single-line and multi-line text exceeding the display omission in css

##Suitable scenario: there is a lot of text content, make sure the text content is certain It will exceed the container, so it is a good choice to choose this method. However, ellipses will also appear when the text does not exceed the line. This method can be optimized with js.

Note:

Set height to an integer multiple of line-height to prevent excess text from being exposed.

Add a gradient background to p::after to prevent only half of the text from being displayed.

Since ie6-7 does not display content content, you need to add tags to be compatible with ie6-7 (such as: ...); to be compatible with ie8, you need to replace::after with:after .

Combined with js optimization code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

css:

p {

position: relative;

width: 400px;

line-height: 20px;

overflow: hidden;

}

.p-after:after{

content: "…";

position: absolute;

bottom: 0;

right: 0;

padding-left: 40px;

background: -webkit-linear-gradient(left, transparent, #fff 55%);

background: -moz-linear-gradient(left, transparent, #fff 55%);

background: -o-linear-gradient(left, transparent, #fff 55%);

background: linear-gradient(to right, transparent, #fff 55%);

}

Copy after login

js:

1

2

3

4

5

6

7

8

9

10

11

12

13

$(function(){

//获取文本的行高,并获取文本的高度,假设我们规定的行数是五行,那么对超过行数的部分进行限制高度,并加上省略号

$('p')。each(function(i, obj){

var lineHeight = parseInt($(this)。css("line-height"));

var height = parseInt($(this)。height());

if((height / lineHeight) >3 ){

$(this)。addClass("p-after"

$(this)。css("height","60px");

}else{

$(this)。removeClass("p-after");

}

});

})

Copy after login

Use third-party plug-ins or write your own script control

There are many introductions on the Internet about using JavaScript to achieve multiple functions To solve the problem of overflow and omission of line text, some use plug-ins, and some use self-encapsulated JavaScript files. However, I think it is better to use js written by yourself.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

//div

<div class="box">北京时间11月18日,苏州太湖马拉松女子比赛中,中国选手何引丽最终获得亚军,落后冠军5秒。但是赛后,何引丽在社交媒体上道歉,称自己最后时刻跑累了,没有拿稳国旗,这究竟是怎么回事?</div>

//css

.box {

width: 400px;

height: 40px;

border: 1px solid #f70505;

line-height: 20px;

}

//js

<script type="text/javascript">

$(function() {

var content_arr = []; //定义一个空数组

$(&#39;.box&#39;)。each(function() { //遍历box内容

var content = $.trim($(this)。text()); //去掉前后文空格

content_arr.push(content); //内容放进数组

})

for var i = 0; i < content_arr.length; i++) { //遍历循环数组

if (content_arr[i].length >= 50) { //如果数组长度(也就是文本长度)大于等于50(数字可自己定义)

content = content_arr[i].substr(0, 50) + &#39;…&#39;; //添加省略号并放进box文字内容后面

$(".box")。eq(i)。text(content);

} else {

Copy after login
The above is a complete introduction to how CSS can achieve the effect of single-line and multi-line text exceeding the display omission. If you want to know more about

CSS3 tutorial, please pay attention to the PHP Chinese website.


The above is the detailed content of How to achieve the effect of single-line and multi-line text exceeding the display omission in css. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles