iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💬

Implementing Multi-line Ellipsis Using Only CSS

に公開
2026/08/16

I will explain how to implement multi-line ellipsis processing using only CSS.

Background

By combining overflow: hidden, whiteSpace: "normal", and textOverflow: ellipsis, you can add an ellipsis when text overflows.

However, this method only works for a single line. There are many situations where you'd want to truncate at two lines, but achieving this with CSS alone was difficult, so I used to struggle with JavaScript implementations.

-webkit-line-clamp vs line-clamp

With this in mind, the line-clamp property has been proposed by the W3C. Meanwhile, there is a property called -webkit-line-clamp that was introduced earlier in WebKit-based browsers and is featured on many sites.

https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-line-clamp

The W3C specification is still a Working Draft and hasn't been widely implemented in browsers yet. Also, since the specification is still subject to change, I will try using -webkit-line-clamp as of the time of writing (2023). Incidentally, -webkit-line-clamp is also formally defined in the W3C draft for backward compatibility, so it can be used with confidence for the time being.

https://www.w3.org/TR/2022/WD-css-overflow-3-20221231/

-webkit-line-clamp is supported by a wide range of browsers. On the other hand, caution is needed as it is not supported in some older browsers.

If opened in an unsupported browser, long strings won't be truncated, which could cause layout issues.

https://caniuse.com/mdn-css_properties_-webkit-line-clamp

Sample

This is a sample that displays the formal ceremonial name of Bangkok in three lines.

div {
  width: 300px;
  overflow: hidden;
  display: -webkit-box;
  text-overflow: ellipsis;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  /* Fail-safe for browsers that do not support this */
  max-height: 24px;
}
<div>
  クルンテープ・マハーナコーン・アモーンラッタナコーシン・マヒンタラーユッタヤー・マハーディロック・ポップ・ノッパラット・ラーチャタニーブリーロム・ウドムラーチャニウェートマハーサターン・アモーンピマーン・アワターンサティット・サッカタッティヤウィサヌカムプラシット
</div>

Summary

  • I learned for the first time that properties with vendor prefixes are included in specifications for backward compatibility
  • Surprisingly, many browsers are supported, so it seems like it can be used with confidence
  • The video that led me to learn the formal ceremonial name of Bangkok Izawa vs Sugai (secretly on a call with Yamamoto) (YouTube)

Discussion