Home  >  Article  >  Web Front-end  >  How to realize that css text exceeds the ellipses in a single line and multiple lines?

How to realize that css text exceeds the ellipses in a single line and multiple lines?

不言
不言Original
2018-09-19 10:45:372723browse

Sometimes when designing a web page, you may encounter text that is too long, causing the page to be unsightly. So how to solve this problem? This article will introduce to you the implementation method of css text that exceeds the ellipses in a single line and multi-line text in the ellipses.

First of all, let’s take a look at the CSS method to achieve a single line of text beyond the ellipses.

css To implement the overflow display of ellipsis in a single line of text, you should use the text-overflow:ellipsis attribute. Of course, you also need to add the width attribute to be compatible with partial browsing.

The implementation code for css single line text exceeding the ellipses:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset=utf-8"> 
<title>省略号 test</title> 
<style type="text/css"> 
*{ 
margin:0; 
padding:0; 
} 
body{ 
padding:10px; 
font-family:Arial; 
} 
#test { 
position:relative; 
width:150px; 
height:20px; 
line-height:20px; 
text-overflow:ellipsis; 
white-space:nowrap; 
*white-space:nowrap; 
overflow:hidden; 
border:1px solid #999; 
} 
#test span{ 
position:absolute; 
top:0; 
right:0; 
display:block; 
float:left; 
} 
</style> 
</head> 
<body> 
<div id="test">php中文网php中文网php中文网php中文网php中文网php中文网php中文网</div> 
</body> 
</html>

The effect of css single line text exceeding the ellipsis is as follows:

How to realize that css text exceeds the ellipses in a single line and multiple lines?

text-overflow: The ellipsis attribute only supports the overflow display of ellipsis in a single line of text. What if we want to implement the overflow display of ellipsis in multiple lines of text? Next, we will continue to talk about the css method to implement multi-line text beyond the ellipsis . (For more information on the text-overflow:ellipsis attribute, please refer to the css manual)

1. CSS realizes that when multi-line text exceeds the ellipsis, it can be directly set with css attributes (only -webkit kernel Only then will it work)

Syntax:

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

Most mobile browsers are based on WebKit core, so this method is suitable for mobile terminals;

-webkit -line-clamp is used to limit the number of lines of text displayed in a block element. This is an unsupported WebKit property that does not appear in the CSS draft specification.

display: -webkit-box Display the object as a flexible box model.

-webkit-box-orient sets or retrieves the arrangement of the child elements of the flex box object.

text-overflow: ellipsis is used for multi-line text, using ellipses "..." to hide text that exceeds the range.

2. Use absolute positioning and padding to implement multi-line text beyond the ellipsis using css; (cross-browser solution)

Syntax:

p{
position: relative; 
line-height: 20px; 
max-height: 40px;
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%);
}

The principle of this method is: first embed a ... in the element containing text, then leave a position of... (padding-right) on the right side of the element containing text, and finally Use absolute positioning to position... to the padding-right area on the right.
Note: This method has a wide range of applications, but 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 .

This article ends here. For more information about css text beyond the ellipses, please pay attention to the php Chinese website.

The above is the detailed content of How to realize that css text exceeds the ellipses in a single line and multiple lines?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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