Truncate text with ellipsis for single and multiple lines
Quick Answer
For single-line truncation use `overflow: hidden; white-space: nowrap; text-overflow: ellipsis;`. For multi-line use `-webkit-line-clamp`.
1/* Single line */
2.truncate {
3 white-space: nowrap;
4 overflow: hidden;
5 text-overflow: ellipsis;
6}
7
8/* Multiple lines */
9.truncate-multiline {
10 display: -webkit-box;
11 -webkit-line-clamp: 3;
12 -webkit-box-orient: vertical;
13 overflow: hidden;
14}Text truncation prevents long strings from breaking card and list layouts. The single-line version uses white-space, overflow, and text-overflow. The multi-line version uses the -webkit-line-clamp property, which is now supported across all modern browsers despite its vendor-prefix origin.
Yes. Despite the vendor prefix, `-webkit-line-clamp` is now part of the CSS Overflow specification and is supported in all modern browsers including Firefox, Chrome, Safari, and Edge.
This free css code snippet for truncate text is production-ready and copy-paste friendly. Whether you are building a web app, API, or frontend interface, this beginner-level example will help you implement truncate text quickly and correctly.
All snippets in the Snippetly library follow css best practices and are tested for real-world use. You can adapt this code to work with React, Vue, Node.js, or any project that uses css.
Organise your team's code snippets with Snippetly. Share knowledge and boost productivity across your organisation.