text-overflow: ellipsis not working

This is what I tried (see here):

body { overflow: hidden; } span { border: solid 2px blue; white-space: nowrap; text-overflow: ellipsis; } 

Essentially, I want the span to shrink with ellipsis when the window is made small. What did I do wrong?

3

12 Answers

You need to have CSS overflow, width (or max-width), display, and white-space.

span { border: solid 2px blue; white-space: nowrap; text-overflow: ellipsis; width: 100px; display: block; overflow: hidden } 
body { overflow: hidden; } span { border: solid 2px blue; white-space: nowrap; text-overflow: ellipsis; width: 100px; display: block; overflow: hidden }
<span>Test test test test test test</span>

Addendum If you want an overview of techniques to do line clamping (Multiline Overflow Ellipses), look at this CSS-Tricks page:

Addendum2 (May 2019)
As this link claims, Firefox 68 will support -webkit-line-clamp (!)

25

Make sure you have a block element, a maximum size and set overflow to hidden. (Tested in IE9 and FF 7)

div { border: solid 2px blue; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 50px; } 
5

For multi-lines in Chrome use :

display: inline-block; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; // max nb lines to show -webkit-box-orient: vertical; 

Inspired from youtube ;-)

1

For multiple lines

In chrome, you can apply this css if you need to apply ellipsis on multiple lines.

You can also add width in your css to specify element of certain width:

.multi-line-ellipsis { overflow: hidden; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; }
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
word-wrap: break-word; 

this and only this did the job for me for a

<pre> </pre> 

tag

everthing else failed to do the ellipsis....

I was having an issue with ellipsis under chrome. Turning on white-space: nowrap seemed to fix it.

max-width: 95px; max-height: 20px; overflow: hidden; display: inline-block; text-overflow: ellipsis; border: solid 1px black; font-size: 12pt; text-align: right; white-space: nowrap; 

Just a headsup for anyone who may stumble across this... My h2 was inheriting

text-rendering: optimizelegibility; //Changed to text-rendering: none; for fix 

which was not allowing ellipsis. Apparently this is very finickey eh?

Add this below code for where you likes to

example

p{ display: block; /* Fallback for non-webkit */ display: -webkit-box; max-width: 400px; margin: 0 auto; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; text-overflow: ellipsis; } 
0

Without Fixed Width

For those of us that do not want to use fixed-width, it also works using display: inline-grid. So along with required properties, you just add display

span { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; display: inline-grid; } 
1

You may try using ellipsis by adding the following in CSS:

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

But it seems like this code just applies to one-line trim. More ways to trim text and show ellipsis can be found in this website:

Add the Following lines in CSS for ellipsis to work

 p { display: block; // change it as per your requirement overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } 

I use:

  • overflow-x: clip to just collapse it horizontally
  • white-space: nowrap to avoid a new line
  • text-overflow: ellipsis to add the ... at the end
overflow-x: clip; white-space: nowrap; text-overflow: ellipsis; 

Have into account that display as flex it wont work, just change it to one that suites your needs.

You Might Also Like