CSS Text-overflow Ellipsis Not Displaying
Asked Answered
O

5

21

I have a div with some inner content that I need to have an ellipsis when it overflows. I've done this many times on other elements but for some reason this is not behaving as expected.

Also, I left white-space:nowrap; out on purpose because the content then does not break to the next line within the span, as a result I only see 2-3 words before the ellipsis starts. I would like the text to span the entire height of the parent container then have the ellipsis start for content that exists beyond those bounds.

Here is a working Demo: http://jsfiddle.net/sadmicrowave/DhkSA/

CSS:

.flow-element{
     position:absolute;
     font-size:12px;
     text-align:center;
     width:75px;
     height:75px;
     line-height:70px;
     border:1px solid #ccc;
}

.flow-element .inner{
     position:absolute;
     width:80%;
     height:80%;
     border:1px solid blue;
     top:0px;
     bottom:0px;
     left:0px;
     right:0px;
     margin:auto;
     text-align:center;
}

.flow-element .long{
     float:left;
     height:50px;
     width:100%;
     line-height:12px;
     border:1px solid red;  
     text-overflow:ellipsis;
     overflow:hidden;
}

HTML:

<a class='flow-element' style='top:100px; left:50px;'>
  <div class='inner'>
     <span class='long'>Box 1 and some other content that should wrap and do some other stuff</span>
  </div>
</a>

Can someone please help. I need to display as much text as possible within the red outlined span while having an ellipsis when text content overflows the container...

Thanks in advance

Oho answered 29/3, 2012 at 18:48 Comment(0)
B
43

you can't apply text-overflow: ellipsis to inline elements (span), it can be used with block elements only (div)

and also use white-space:nowrap; when using text-overflow: ellipsis;

check this, i have converted your inner span to div, just for proof of concept

http://jsfiddle.net/3CgcH/5/

i don't know why you have used span, but as per your logic you can make changes as i suggested

Update:

someone will think that in the question if i put white-space: nowrap; to span element then the text-overflow: ellipsis: is working so may be i am wrong, but it is not the case because questioner has used float: left in the span tag that means the span tag will be converted to a box block and work like a normal block level element, which is also wrong thing to do because if you need the block element behavior then use a block level element

Reference:

http://www.w3.org/TR/CSS2/visuren.html#propdef-float

http://dev.w3.org/csswg/css3-ui/#text-overflow

Benenson answered 29/3, 2012 at 18:54 Comment(4)
that is absolutely wrong. works very nice with inline elements.Cellule
then why this w3c draft says that it applies to block contains only ? dev.w3.org/csswg/css3-ui/#text-overflowBenenson
white-space:nowrap; is what I was missingMckinley
I'm a bit late to the party but overflow:hidden also made it work for me.Tonyatonye
S
2

In my case it helped to set display: block;

Selvage answered 10/7, 2022 at 16:27 Comment(0)
C
1

Add this:

white-space:nowrap;

to .flow-element .long

then the overflow-ellispsis works.

Cellule answered 29/3, 2012 at 18:56 Comment(1)
please see my comment to Heather Walters' post of the same solution.Oho
A
1

I think you will find the problem is caused by having text-align: center;

Adrastus answered 22/3, 2013 at 21:31 Comment(0)
A
0

Add white-space:nowrap; to your .inner div.

Agora answered 29/3, 2012 at 18:54 Comment(8)
adding your suggestion does indeed add the ellipsis to the text however the text does not wrap to the width of the inner element. I need to show as much of the text as I can, not just 2-3 words of it...Oho
Yeah, I noticed that and was working on a solution that would wrap further than one line....Agora
@sadmicrowave: You don't understand how text-overflow works. You want to see more? Then you must enlarge the width.Cellule
@Sven, would I not be able to increase the height and also see more?Oho
no, the problm is, that text-overflow don't really works without white-space:nowrap. And that means you have only one line of text.Cellule
disappointing. can you think of any other way to achieve my intended results?Oho
A quick search brought this SO jquery solution for both single and multiline ellipses: #537314Agora
Did you ever get this working correctly? I'd love to know how you achieved it if you did!Agora

© 2022 - 2024 — McMap. All rights reserved.