How to truncate text in TailwindCSS?

Viewed 2088

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

My code not works below:

  <div className="ml-1 inline-block">
     <span>Label: </span>
     <span className="font-semibold truncate">
       long texttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
     </span>
  </div>

How can I fix it?

1 Answers

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 truncate block">
       long texttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
     </span>
  </div>
Related