{"id":270051,"date":"2018-04-27T06:53:04","date_gmt":"2018-04-27T13:53:04","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=270051"},"modified":"2019-08-17T19:19:38","modified_gmt":"2019-08-18T02:19:38","slug":"line-clamp","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/properties\/l\/line-clamp\/","title":{"rendered":"line-clamp"},"content":{"rendered":"
The 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. That said, it’s defined as a shorthand for <\/p>\n It’s easy to see how That ellipsis should render as a Unicode character (U+2026) but could be replaced by an equivalent based on the content language and writing mode of the User-Agent being used.<\/p>\n Fair question, my friend, and the answer is, well…<\/p>\n (See what I did there?)<\/p>\n …sorta<\/em>.<\/p>\n See the Pen text-overflow<\/a> by Geoff Graham (@geoffgraham<\/a>) on CodePen<\/a>.<\/p>\n Nice nice, that’s a good start. But what if we want to introduce the ellipsis not on the first line but somewhere, say, the third line of text?<\/p>\n That’s where See the Pen line-clamp (-webkit)<\/a> by Geoff Graham (@geoffgraham<\/a>) on CodePen<\/a>.<\/p>\n Firefox supports this now, exactly this way<\/em> (with the As of right now, it’s browser support. Line clamps are part of the CSS Overflow Module Level 3 which is currently in Editor’s Draft and totally unsupported at the moment.<\/p>\n We can get some line clamping action with a We could go down the JavaScript path if we’d like. Clamp.js<\/a> will do the trick:<\/p>\n See the Pen line-clamp (clamp.js)<\/a> by Geoff Graham (@geoffgraham<\/a>) on CodePen<\/a>.<\/p>\n This is the support for WebKit’s propriety and undocumented implementation of line clamp.<\/p>\n This browser support data is from Caniuse<\/a>, which has more detail. A number indicates that browser supports the feature at that version and up.<\/p><\/div>line-clamp<\/code> property truncates text at a specific number of lines.<\/p>\n
max-lines<\/code> and
block-overflow<\/code>, the former of which is noted as at risk of being dropped in the Candidate Recommendation.<\/p>\n
max-lines<\/code> would be nixed since it’s function (setting the number of lines before truncating) is already baked into
line-clamp<\/code> and any further abstraction might be unnecessary. But that’s getting us off track, so let’s move on.<\/p>\n
Syntax<\/h3>\n
.module {\r\n line-clamp: [none | <integer>];\r\n}<\/code><\/pre>\n
line-clamp<\/code> accepts the following values in the current draft of the spec:<\/p>\n
\n
none<\/code>: sets no maximum on the number of lines and no truncation will happen as a result.<\/li>\n
<integer><\/code>: sets the maximum number of lines before truncating the content and then displays an ellipsis (…) at the end of the last line.<\/li>\n<\/ul>\n
Hey, can’t I do this with text-overflow?<\/h3>\n
text-overflow<\/code> does indeed have an
ellipsis<\/code> value that will truncate text:<\/p>\n
.truncate {\r\n text-overflow: ellipsis;\r\n\r\n \/* Needed to make it work *\/\r\n overflow: hidden;\r\n white-space: nowrap;\r\n}<\/code><\/pre>\n
line-clamp<\/code> comes into play. Just note that a combination of three properties are used to make it happen:<\/p>\n
.line-clamp {\r\n display: -webkit-box;\r\n -webkit-line-clamp: 3;\r\n -webkit-box-orient: vertical; \r\n overflow: hidden;\r\n}<\/code><\/pre>\n
-webkit-<\/code> prefixes and all). <\/p>\n
So, what’s the catch?<\/h3>\n
-webkit-<\/code> prefix (which, weirdly enough, works across all major browsers). In fact, that’s how the demo above was done.<\/p>\n
Browser Support<\/h3>\n
Desktop<\/h4>