In the good old days, there existed a trick in webkit for clamping lines using pure css:
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
Though this syntax happily coexists with the display: -webkit-flex
syntax, in all modern webkit-based browsers, it is considered obsolete.
My question is:
How can I achieve line clamping in pure CSS and without the obsolete '-webkit-line-clamp'
trick?
The -webkit-line-clamp CSS property allows limiting of the contents of a block container to the specified number of lines. It only works in combination with the display property set to -webkit-box or -webkit-inline-box and the -webkit-box-orient property set to vertical .
Line clamps are part of the CSS Overflow Module Level 3 which is currently in Editor's Draft and totally unsupported at the moment.
The line-clamp property truncates a text at a specified number of lines. It limits the text, after you tell it the desirable number of lines, adding an ellipsis after the last word or portion of a word that is demonstrated. It is a shorthand for: max-lines.
The only cross-browser solution is to use js afaik. Several solutions to the problem of multi-line truncation with ellipsis are discussed here: http://css-tricks.com/line-clampin/
I hate them all, but welcome to web development.
Try using this CSS
.line-clamp:after {
background: linear-gradient(to right, rgba(255, 255, 255, 0), #FFFFFF 50%) repeat scroll 0 0 rgba(0, 0, 0, 0);
bottom: 0;
content: "...";
font-weight: bold;
padding: 0 20px 1px 45px;
position: absolute;
right: 0;}
.line-clamp {
height: 5.6em;
line-height: 1.4em;
overflow: hidden;
position: relative;}
The CSS Overflow Level 3 specification defines a standard line-clamp
property (without the quirks the ´-webkit-` prefixed solution has). Unfortunately, non of the main browsers supports this feature yet.
So, for now you will have to use a polyfill for browsers that don't support this property. CSS-Tricks describes three solutions, each one having its pros and cons.
Using standard CSS
.fade {
position: relative;
height: 3.6em; /* exactly three lines */
}
.fade::after {
content: "";
text-align: right;
position: absolute;
bottom: 0;
right: 0;
width: 70%;
height: 1.2em;
background:
linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1) 50%);
}
Pros: All current browsers support this. Cons: There's a fade-out instead of an ellipsis and requires heights to be set manually.
Using Opera's -o-ellipsis-lastline
value
.last-line {
height: 3.6em; /* exactly three lines */
text-overflow: -o-ellipsis-lastline;
}
Pros: Display as expected. Cons: Only works in old versions of Opera and requires height to be set manually
Using JavaScript (Clamp.js)
var module = document.getElementById("clamp-this-module");
$clamp(module, {clamp: 3});
Pros: Display as expected and is flexible through different options. Cons: Requires a JS library to work and is less performant than CSS solutions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With