Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS ellipsis truncation when width is narrower than ellipsis character

I'm using the following CSS to truncate text with ellipsis.

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

This works well for me, but doesn't behave as I would like when the width of the element is smaller than the width of the ellipsis character. I would expect that the ellipsis character would be truncated, but instead the original text is displayed truncated without the ellipsis.

See this JSFiddle for an example.

While I can avoid this issue with a minimum width, I'm curious to find out if there's a simple way to show a truncated ellipse character instead of the truncated text.

like image 861
Ross Smith Avatar asked Jan 13 '15 15:01

Ross Smith


People also ask

How do you truncate a character in CSS?

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.

How do you overflow text on an ellipsis?

To clip at the transition between characters you can specify text-overflow as an empty string, if that is supported in your target browsers: text-overflow: ''; . This keyword value will display an ellipsis ( '…' , U+2026 HORIZONTAL ELLIPSIS ) to represent clipped text.


1 Answers

I meet the same question with you, there is a width threshold for truncated ellipse character changing to truncated ellipse text, and the width threshold is affected by the font-size, and is different from kind of browsers. if we know the width threshold, we can manually use '...' to replace the original text. however, the width threshold is unknown...

like image 54
keben hu Avatar answered Oct 04 '22 03:10

keben hu