DEV Community

Temitope Ayodele
Temitope Ayodele

Posted on • Edited on

10

How to truncate multi-line string with Pure CSS

It is easy to truncate a single-line text.

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
Enter fullscreen mode Exit fullscreen mode

Multi-line Tuncation

However, what if you want to truncate after some lines? The CSS -webkit-line-clamp property comes in handy. It helps to limit the block of text into specified number of lines ( 1 or more, as desired). Then ellipsis is added automatically at the end after the truncated point.

   display: -webkit-box;
   -webkit-box-orient: vertical;

   /* to specify the number of lines you want the text to run through... */
   -webkit-line-clamp: 3;
   /* hide the overflowing text, i.e, texts that did not fit in to the box */
   overflow: hidden;
Enter fullscreen mode Exit fullscreen mode

This is a practical example in the codepen below.

I hope that is clear enough!

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay