Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text overflow ellipsis on two lines

Tags:

html

css

I know you can use a combination of CSS rules to make text end with ellipsis (...) when it's time to overflow (get out of parent's bounds).

Is it possible (feel free to just say, no) to achieve the same effect, but let the text wrap on more than one line?

Here's a demo.

div {   width: 300px;    height: 42px;    overflow: hidden;    text-overflow: ellipsis;    white-space: nowrap; } 

As you can see, the text ends with ellipsis when it goes wider than the div's width. However, there is still enough space for the text to wrap on a second line and go on. This is interrupted by white-space: nowrap, which is required for the ellipsis to work.

Any ideas?

P.S.: No JS solutions, pure CSS if possible.

like image 413
Tony Bogdanov Avatar asked Apr 09 '13 18:04

Tony Bogdanov


People also ask

How do I force text-overflow ellipsis?

To force overflow to occur and ellipses to be applied, the author must apply the nowrap value to the white-space property on the element, or wrap the content in a <NOBR> tag.

How do I make two lines of text in CSS?

The overflow–wrap CSS property is applicable to inline elements & specifies that the browser can break the line inside the selected element into multiple lines in an otherwise unbreakable place.

What is line clamp?

The line-clamp property truncates text at a specific number of lines. The spec for it is currently an Editor's Draft, so that means nothing here is set in stone because it's a work in progress.


1 Answers

Easy CSS properties can do the trick. The following is for a three-line ellipsis.

display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; text-overflow: ellipsis; 
like image 90
pxthxk Avatar answered Sep 18 '22 13:09

pxthxk