CSS text overflow hidden display ellipsis (single row + multiline)

Source: Internet
Author: User

Text more than a few lines on line, this feature almost every text browsing site will be used, the way to achieve it is also a lot, today simply introduce the way to implement it.

one-line text does not wrap, and the text is hidden beyond

.box-content{

    overflow: hidden;//文本溢出隐藏

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

    white-space: nowarp;//不换行

}

Two. Implement multiple lines of text hiding in the WebKit kernel browser and display ellipses

In the WebKit kernel, you can take advantage of the-webkit-line-clamp property, whose value is a number (1, 2, 3 ...), but it is not a CSS standard, only implemented in WebKit, must be for block-level elements,

and set the display property of the element to:-webkit-box,

And the-webkit-box-orient property is set to: vertical, as follows:

.box-content{           display: -webkit-box; //将元素设为盒子伸缩模型显示      -webkit-box-orient: vertical; //伸缩方向设为垂直方向      -webkit-line-clamp: 3 //超出3行隐藏,并显示省略号Overflow:hidden; }

Because it is WebKit kernel browser implementation, for other browsers are not compatible, so in order to be compatible with more browsers, can be implemented as follows.

three. Ways to be compatible with all browsers

This method is implemented by using the pseudo-selector of CSS, setting the element location to relative, setting the pseudo selector to absolute, and adjusting the position of the pseudo selector. As follows:

Two-line overflow omitted

p {
position:relative;
line-height:20px;
max-height:40px;
Overflow:hidden;
width:60px;
}

P::after {
Display:block;
content: "...";
Position:absolute;
bottom:0;
right:0;
padding-left:40px;
}

tip: To set the number of rows to display, you only need to set height to an integer multiple of line-height.

Because IE6-7 does not display content, it is necessary to add a label compatible with ie6-7 (such as:<span>...<span/>); compatible IE8 needs to be replaced by: after.

CSS text overflow hidden display ellipsis (single row + multiline)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.