CSS controls single-line or multi-line text beyond the display ellipsis

1. Single Line Text

Use the text-overflow:ellipsis property

text-overflow: clip|ellipsis|string;

clip: Trim the text.

ellipsis: Displays ellipses to represent trimmed text.

string: Use the given string to represent the trimmed text.

Example:

css:

p{
	overflow: hidden;/*The excess part is hidden*/
	text-overflow:ellipsis;/* Display ellipsis in excess */
	white-space: nowrap;/*Specifies that the text in the paragraph does not wrap*/
	width: 250px;/*It needs to be used with width*/
	border: 1px solid red;
	font-size: 30px;
}

html:  

<p>Single-line text exceeding part displays ellipsis I am mdzz</p>

Effect picture:

                 

2. Multi-line text displays ellipsis

  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  width: 250px;
  border: 1px solid red;
  font-size: 30px; 

Because the CSS extension property of WebKit is used, this method is suitable for WebKit browsers and mobile terminals;

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 combine other WebKit properties. Common binding properties:
  2. display: -webkit-box; A property that must be combined to display the object as a flexible box model.
  3. -webkit-box-orient A property that must be combined to set or retrieve the arrangement of the child elements of the flexbox object.

Effect picture:

                

Scope of application:
This method has a wide range of applications, but ellipsis will also 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 line-height to prevent the excess text from being exposed.

  

 

  2.

Add a gradient background to p::after to avoid half of the text.

 

  3. Since ie6-7 does not display content content, add tags to be compatible with ie6-7 (eg: <span>…<span/>); for compatibility with ie8, replace ::after with :after.

Applicable methods for compatibility:

Note:

This method needs to be adjusted according to your own font size and the width you set yourself. Although it is compatible (pro-test), it is still a bit troublesome. But it is possible to display the ellipsis in the middle in the last row.

p{
	posingfation: relative;
	line-height: 20px; //variable
	max-height: 80px;//Variable
	overflow: hidden;
	width: 240px;//Variable
	border: 1px solid red;
}
p::after{
	content: "...";
	position: absolute;
	bottom: 0;
	right: 7px; //variable
	padding-right: 124px;//Variable 
     //Compatibility processing background: -webkit-linear-gradient(left, transparent, #fff 0%); background: -o-linear-gradient(right, transparent, #fff 0%); background: -moz-linear-gradient(right, transparent, #fff 0%); background: linear-gradient(to right, transparent, #fff 0%); }

Effect picture:

                 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324742403&siteId=291194637