with
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
"..." will be shown in the end of the line if overflowed. However, this will be shown only in one line. But I would like it to be shown in multi-lines.
It may looks like:
+--------------------+
|abcde feg hij dkjd|
|dsji jdia js ajid s|
|jdis ajid dheu d ...|/*Here it's overflowed, so "..." is shown. */
+--------------------+
I've found this css (scss) solution that works quite well. On webkit browsers it shows the ellipsis and on other browsers it just truncates the text. Which is fine for my intended use.
An example by the creator: http://codepen.io/martinwolf/pen/qlFdp
Here is the closest solution I could get using just css.
HTML
CSS
Working Fiddle (resize the window to check)
Link to my blog for explanation
Updated Fiddle
I hope now some css expert would have got idea on how to make it perfect. :)
I have hacked around until I've managed to achieve something close to this. It comes with a few caveats:
I should also note that the text will be broken at a word boundary, not a character boundary. This was deliberate (since I consider that better for longer texts), but because it's different from what
text-overflow: ellipsis
does, I thought I should mention it.If you can live with these caveats, the HTML looks like this:
And this is the corresponding CSS, using the example of a 150 pixel wide box with three lines of text on a white background. It assumes you have a CSS reset or similar that sets margins and paddings to zero where necessary.
The result looks like this:
To clarify how it works, here's the same image, except that
.hidedots1
is hightlighted in red, and.hidedots2
in cyan. These are the rectangles that hide the ellipsis when there's no invisible text:Tested in IE9, IE8 (emulated), Chrome, Firefox, Safari, and Opera. Does not work in IE7.
Here's a recent css-tricks article which discusses this.
Some of the solutions in the above article (which are not mentioned here) are
1)
-webkit-line-clamp
and 2) Place an absolutely positioned element to the bottom right with fade outBoth methods assume the following markup:
with css
1) -webkit-line-clamp
line-clamp FIDDLE (..for a maximum of 3 lines)
2) fade out
Fade out FIDDLE
Solution #3 - A combination using @supports
We can use @supports to apply webkit's line-clamp on webkit browsers and apply fade out in other browsers.
@supports line-clamp with fade fallback fiddle
CSS
In your case, the following shall be efficient and enough.
Bit late to this party but I came up with, what I think, is a unique solution. Rather than trying to insert your own ellipsis through css trickery or js I thought i'd try and roll with the single line only restriction. So I duplicate the text for every "line" and just use a negative text-indent to make sure one line starts where the last one stops. FIDDLE
CSS:
HTML:
More details in the fiddle. There is an issue with the browser reflowing that I use a JS redraw for and such so do check it out but this is the basic concept. Any thoughts/suggestions are much appreciated.