28

I have a page which has a content like this...

<div id="content">
testingtestingtestingtestingtestingtestingtestingtestingtesting
testingtestingtestingtestingtestingtestingtestingtesting
testingtestingtestingtestingtesting
</div>

How do i apply a max-width on it . I m using this code in the css

#content {
    max-width:50px; /* for standards-compliant browsers */
   /*width:expression(document.body.clientwidth > 650? "650px": "auto" );*/ 
   /* max-width expression for IE only */
}

but i m not getting the results in firefox... http://pradyut.dyndns.org/WebApplicationSecurity/width.jsp

Are there any JavaScript solutions?
Any help
thanks
Pradyut

AxelEckenberger
  • 16,628
  • 3
  • 48
  • 70
Pradyut Bhattacharya
  • 5,440
  • 13
  • 53
  • 83
  • If you go to http://www.css3.com/, in the middle column, you should see a list of the current set of CSS tags up to CSS Level 3. – kafuchau Mar 24 '10 at 21:17

5 Answers5

25

Well the reason why you not getting the expected result on the page you provided by the link is that you are using a span instead of a div.

Add

display: block;

to you style.

AxelEckenberger
  • 16,628
  • 3
  • 48
  • 70
15
#content {
    max-width: 50px;
    display: inline-block;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

You may like to remove display: inline-block; depending on the context of your element.

ekerner
  • 5,650
  • 1
  • 37
  • 31
14

So, in your CSS, you can set the word-wrap property to break-word so that your string of text (w/ no whitespace) will be made to fit into the max-width:

E.g.

<style type="text/css">

#content {
    max-width: 50px;
    word-wrap: break-word;
}

</style>
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
kafuchau
  • 5,573
  • 7
  • 33
  • 38
6

If you used your own example you would find that it works just fine. The page you linked to uses a span element, which is an inline element by default. Inline elements cannot have width or height. If you want to set max width on a span element, set it's display to block or inline-block:

<span id="content" style="max-width:50px; display:block;" >  
testingtestingtestingtestingtestingtestingtestingtestingtesting  
testingtestingtestingtestingtestingtestingtestingtesting  
testingtestingtestingtestingtesting  
</span>
Andy E
  • 338,112
  • 86
  • 474
  • 445
-2

Add spaces in between. I believe this occurs because css/html reads it as one word. eg..

<span id="content" style="max-width:50px; display:block;" >  
testing testing testing testing testing testing testing testing testing  
testing testing testing testing testing testing testing testing  
testing testing testing testing testing  
</span>
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Ken X
  • 1