13

I would like to create a tag system . When a text in a div (a tag) is too long, I would truncate it and add "..." in the end. Is there any CSS solution in order to do it?

Loredra L
  • 1,485
  • 2
  • 16
  • 32

3 Answers3

40

You can use text-overflow: ellipsis for that. Further information is in the comments of the code.

div {
  width: 10em; /* the element needs a fixed width (in px, em, %, etc) */
  overflow: hidden; /* make sure it hides the content that overflows */
  white-space: nowrap; /* don't break the line */
  text-overflow: ellipsis; /* give the beautiful '...' effect */
}
<div>This is a text that is too long for this div.</div>
GreyRoofPigeon
  • 17,833
  • 4
  • 36
  • 59
7

Here is your required css.

.tag{
    display: inline-block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    width: 100px;
}

Here

Width can be changed to required size.

white-space will make sure the text is on same line.

overflow:hidden will hide excess content allow text-overflow to add dots.

text-overflow: ellipsis will add dots

MKAka
  • 232
  • 2
  • 9
3

Try this:

<div class='truncate'></div>



.truncate {
     width: 250px;
     white-space: nowrap;
     overflow: hidden;
     text-overflow: ellipsis;
    }