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. That said, it's defined as a shorthand for max-lines and block-overflow, the former of which is noted as at risk of being dropped in the Candidate Recommendation.

It's easy to see how max-lines would be nixed since it's function (setting the number of lines before truncating) is already baked into line-clamp and any further abstraction might be unnecessary. But that's getting us off track, so let's move on.

Syntax

.module {
  line-clamp: [none | <integer>];
}

line-clamp accepts the following values in the current draft of the spec:

  • none: sets no maximum on the number of lines and no truncation will happen as a result.
  • <integer>: sets the maximum number of lines before truncating the content and then displays an ellipsis (...) at the end of the last line.

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.

Hey, can't I do this with text-overflow?

Fair question, my friend, and the answer is, well...

(See what I did there?)

...sorta.

text-overflow does indeed have an ellipsis value that will truncate text:

.truncate {
  text-overflow: ellipsis;

  /* Needed to make it work */
  overflow: hidden;
  white-space: nowrap;
}

See the Pen text-overflow by Geoff Graham (@geoffgraham) on CodePen.

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?

That's where line-clamp comes into play.

See the Pen line-clamp (-webkit) by Geoff Graham (@geoffgraham) on CodePen.

So, what's the catch?

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.

We can get some line clamping action with a -webkit- prefix. In fact, that's how the demo above was done. Firefox is the only holdout with it though, so even this won't get you full support.

We could go down the JavaScript path if we'd like. Clamp.js will do the trick:

See the Pen line-clamp (clamp.js) by Geoff Graham (@geoffgraham) on CodePen.

Browser Support

This is the support for WebKit's propriety and undocumented implementation of line clamp.

This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.

Desktop

ChromeOperaFirefoxIEEdgeSafari
14*15*NoNoNo5*

Mobile / Tablet

iOS SafariOpera MobileOpera MiniAndroidAndroid ChromeAndroid Firefox
5.0-5.1*46*No2.3*66*No

Additional Resources

This comment thread is closed. If you have important information to share, please contact us.
icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag