I have some dynamic text (comes from a database) and I need to fit into a div
My div
has a fixed size. However, when the text becomes too long, it breaks into two lines. I would like to put three dots at the end of longer texts to make them fit inside a single line.
For example: My really long text
becomes My really lo...
I have committed several attempts, but basically all of them depend on counting characters. That is, whatsoever, not really fortunate, for characters do not have a fixed width. For example, a capital W
is much wider than an small i
Therefore, I encounter two main problems with the character-counting approach:
If the text has many narrow characters, it gets cut and appended with ..., even if the text would actually fit on one line afore trimmed.
When the text contains many wide characters, it ends up on two lines even after I cut it to the maximum number of characters and append the dots.
Is there any solution here (JavaScript or CSS) that takes the actual width of the text into consideration, and not the number of characters?
Solution # 1: Truncate text for single line This solution works for single-line truncation. Force text to be on a single line: white-space: nowrap; Now, our text should be on the same line and should overflow from the box if it's long enough and wrapped before.
The text-overflow CSS property sets how hidden overflow content is signaled to users. It can be clipped, display an ellipsis (' … '), or display a custom string.
The text can also be truncated using the CSS ::after pseudo-element. In the next example, the overflow of the <span> is hidden, and the max-height is set based on the line-height . Example of truncating a multi-line text using the ::after pseudo-element: ¶
If there is a need for spacing around the truncated text, padding must be applied to the parent element. The text can also be truncated using the CSS ::after pseudo-element. In the next example, the overflow of the <span> is hidden, and the max-height is set based on the line-height.
Here, the area of DIV element is shown by the red border, and we can clearly see that text is going beyond it. We can solve this problem by using CSS overflow property. hidden – The overflow is clipped, and the rest of the content will be invisible. <!-- Adding css -->
This problem basically occurs when the height and width of DIV element are small such that all the text can not be fitted in that DIv. Here, the area of DIV element is shown by the red border, and we can clearly see that text is going beyond it. We can solve this problem by using CSS overflow property.
Use these styles:
white-space: nowrap; /*keep text on one line */
overflow: hidden; /*prevent text from being shown outside the border */
text-overflow: ellipsis; /*cut off text with an ellipsis*/
Apart from ellipsis
, I would suggest display the whole text on mouse hover using tooltip
.
fiddle
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