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?
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>
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
Try this:
<div class='truncate'></div>
.truncate {
width: 250px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With