Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to truncate text in TailwindCSS?

I am using truncate in TailwindCSS to make text ellipsis if text-overflow more than one line but it does not work.

My code is below:

<script src="https://cdn.tailwindcss.com/3.0.22"></script> <!-- release 2022-02-11 -->

<div class="max-w-md mx-auto">
  <h2 class="text-xl font-bold">Label:</h2>
  <p class="truncate">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </p>
</div>

How can I fix It?

like image 795
A Little Girl Avatar asked Dec 01 '25 08:12

A Little Girl


1 Answers

Update: line-clamp (2023-10-11)

TailwindCSS now have line clamp plugin by default. So you can just use line-clamp-1.

<div className="ml-1 inline-block w-[200px]">
   <span>Label: </span>
   <span className="font-semibold line-clamp-1">
     long texttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
   </span>
</div>

Original answer

First thing first your parent or the element must have a specific width otherwise how the element suppose to know when to stop and truncate? Also in order to truncate to work your element's display must not be display inline since span is display inline by default you should change it to block.

<div className="ml-1 inline-block w-[200px]">
   <span>Label: </span>
   <span className="font-semibold line-clamp-1">
     long texttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
   </span>
</div>
like image 63
Sarkar Avatar answered Dec 02 '25 22:12

Sarkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!