Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

text-overflow ellipsis on one of two spans inside a wrapper

Tags:

html

css

I am having a problem with ellipsis. Here is my HTML:

<div id="wrapper">     <span id="firstText">This text should be effected by ellipsis</span>     <span id="lastText">this text should not change size</span> </div> 

Is there a way to do this with pure css? Here is what I have tried:

#firstText {     overflow: hidden;     white-space:nowrap;     text-overflow:ellipsis; }  #lastText {     white-space:nowrap;     overflow:visible;    } 

This is how it is shown:

This text should be effected by ellipsis this text shoul

While this is the result I want:

This text should be e...this text should not change size

like image 774
funkyfly Avatar asked Nov 14 '11 13:11

funkyfly


People also ask

How do you add an ellipsis to a span tag?

To add an ellipsis in the HTML <span> element having the CSS overflow property set to “hidden”, you need to add the text-overflow property. Use its “ellipsis” value, which will add dots at the end of the content within the <span>.

How can I show dots in a span with hidden overflow?

So by default short text with dots is shown and by clicking long text appears. Clicking again hides that long text and shows short one again. Quite easy thing to do: just add / remove class with text-overflow:ellipsis.

How does text-overflow ellipsis work?

The text-overflow property in CSS deals with situations where text is clipped when it overflows the element's box. It can be clipped (i.e. cut off, hidden), display an ellipsis ('…', Unicode Range Value U+2026) or display an author-defined string (no current browser support for author-defined strings).

How do I force text-overflow ellipsis?

To force overflow to occur and ellipses to be applied, the author must apply the nowrap value to the white-space property on the element, or wrap the content in a <NOBR> tag.


2 Answers

You can give width to your #firstText like this :

#firstText {     overflow: hidden;     white-space:nowrap;     text-overflow:ellipsis;     width:150px;     display:inline-block; } 

Check this example

http://jsfiddle.net/Qhdaz/5/

like image 50
sandeep Avatar answered Oct 19 '22 17:10

sandeep


You could achieve this by changing the order of your spans and give the first span a float right. That way you don't have to set a width to any of your elements:

#firstText {    font-size: 30px;   display: block;   overflow: hidden;   text-overflow: ellipsis;   white-space: nowrap; }  #lastText {    color:red;   font-size:30px;   float:right;   white-space:nowrap; }
<div id="wrapper">     <span id="lastText">this text must be shown in full</span>     <span id="firstText">This text may be cropped</span> </div>
like image 29
Michiel Avatar answered Oct 19 '22 15:10

Michiel