brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: -webkit-line-clamp in CSS cuts a teaser off after three lines

Css
~/tips/tip-css-line-clamp-three-lines

.excerpt {

Excerpts get the same height in a card grid, with an ellipsis at the end.

For one line there is text-overflow. For a teaser of exactly three lines with dots at the end, the tool is line-clamp, in its prefixed form.

.excerpt {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}

It looks like legacy code, and partly it is: the old -webkit-box display mode is required for the clamp to work. But this exact combination is what every current browser supports, including Firefox since version 68. The text stops after the third line and the last visible line ends in an ellipsis.

I like it for post excerpts in a card grid, so one long summary does not make its card taller than the rest.

The unprefixed line-clamp property exists in the spec, but according to MDN it is only in preview builds and behind flags right now, so do not use it on its own yet.

Two things to watch. The hidden text is still in the page, so screen readers read all of it, which is usually fine but worth knowing. And if the element has padding at the bottom, a fourth line can peek through, so put the padding on a wrapper instead.

Further reading: MDN: line-clamp

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link