Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding ... when text is too long in a div with only CSS [duplicate]

Tags:

html

css

tags

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?

like image 514
Loredra L Avatar asked May 10 '16 08:05

Loredra L


3 Answers

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>
like image 98
LinkinTED Avatar answered Oct 21 '22 08:10

LinkinTED


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

like image 41
MKAka Avatar answered Oct 21 '22 07:10

MKAka


Try this:

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



.truncate {
     width: 250px;
     white-space: nowrap;
     overflow: hidden;
     text-overflow: ellipsis;
    }
like image 3
Mark Omoniyi Adesina Avatar answered Oct 21 '22 07:10

Mark Omoniyi Adesina