How to (kinda) "mimic" -webkit-line-clamp in other browsers

This article was first published on css-101.org (11-12-2012).

-webkit-line-clamp is an unsupported WebKit property that limits the number of lines of text displayed in a block element. In order to achieve the effect, it needs to be combo-ed with a couple of other exotic WebKit properties. http://dropshado.ws/post/1015351370/webkit-line-clamp

“line-clamp” (for short) does some serious magic, but its syntax is not plain vanilla. I’ll let you judge for yourself:

.giveMeEllipsis {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    /* number of lines to show */
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

What can we do across browsers?

One thing that should work across the board is generating content to show ellipsis in the bottom right corner of the box. The “trimming” part is done by setting the height of the box in relation to the line-height value.

Examples

Showing 3 lines:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in repre henderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

p {
    position:relative;
    line-height:1.4em;
    /* 3 times the line-height to show 3 lines */
    height:4.2em;
}
p::after {
    content:"...";
    font-weight:bold;
    position:absolute;
    bottom:0;
    right:0;
    padding:0 20px 1px 45px;
    background:url(/extras/line-clamp_for_non_webkit-based_browsers/ellipsis_bg.png) repeat-y;
}

We add the ellipsis via the CSS content property, but because it could overlap some text in the bottom right corner of the paragraph, we style the background of the generated box with a linear gradient to prevent it from cutting off a letter.

Note that this approach works even if there is a font-size increase/decrease in that box, like the paragraph below shows with a font-size set to 2em:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in repre henderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Showing 4 lines:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in repre henderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

p {
    position: relative;
    line-height: 1.4em;
    /* 4 times the line-height to show 4 lines */
    height: 5.6em;
}
p::after {
    content: "...";
    font-weight: bold;
    position: absolute;
    bottom: 0;
    right: 0;
    padding: 0 20px 1px 45px;
    background: url(ellipsis_bg.png) repeat-y;
}

Showing 4 lines instead of 3 is just a matter of increasing the height of the box by 1.4em (the value of our line-height).

Things to take into consideration

You cannot generate content in IE 6 and 7 via the content property and for IE 8 you need to rely on the single colon syntax (i.e. :after instead of ::after). But you can make this work in IE 6 and 7 using a CSS expression, that will plug that content for you, see “Styling Elements With Glyphs, Sprites and Pseudo-Elements”.

Depending on how your users access the “truncated” content, you may prefer the use of a DOM element in lieu of a pseudo-element - to make the ellipsis (or whatever you want to show there) actionable.

Which way to go?

You could decide to not provide ellipsis in oldIE and use this trick for all other browsers.
Or you could choose to use a CSS gradient to save a HTTP request and go with a data url for IE 8/9 and Opera mini. You could also decide to do some feature detection and go with -webkit-line-clamp for UAs that support it; it’s up to you…

Isn’t our job easy? :)

Update: @hzr reminded me that Opera has -o-ellipsis-lastline which is much simpler to implement than -webkit-line-clamp.

Share