tags:

views:

986

answers:

2

I need a div with a border to wrap around arbitrarily sized fixed-width content (this means I do not know what that fixed width will be, it changes from page to page).

The below html does what I'd expect in IE 6, that is, if you size the browser window down to less than the width of the content (in this case a fixed-width <p>, but it could be any fixed width content), the div with the border stops shrinking at the right-hand edge of the content. However, in IE 7 (and FF), the border continues to shrink down to stay within the browser window's width, going behind and showing through the content.

Is there a way to make the div shrink and grow with the browser window, but never have a width less than the content?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
  Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >

  <head>
    <title>Border Test</title>
  </head>

  <body>
    <div style="border: 1px #0000FF solid;padding:3px;">
      <p style="width:700px;">
        Lorem ipsum dolor sit amet, consectetur 
        adipiscing elit. Praesent varius mi a leo
        ultrices consequat. Nam id venenatis ligula. 
        Suspendisse et faucibus eros. Donec quis 
        augue eu nunc cursus blandit vel vel odio.
        Suspendisse rutrum aliquet massa, eu ultricies
        elit laoreet a. Curabitur feugiat cursus.
      </p>
    </div>
  </body>
</html>

Update: I did some poking around in IE 8, and display:table in the <div> style declaration fixes it there.

<div style="border: 1px #0000FF solid;padding:3px;display:table;">

Is there any way to simulate display:table in IE 7?

+1  A: 

add "overflow:auto" to the parent div, so...

<div id="parentDiv" style="border: 1px #0000FF solid;padding:3px;overflow:auto;">  should make it work.

This way, you have the best solution CSS can offer. Now, as for your powerusers, use this bit of jquery to give the parent div the proper width, according to its child div.

Note that i give your divs a class so i can easily target them.

<body>
    <div class="parentDiv" style="border: 1px #0000FF solid;padding:3px;overflow:auto;">
      <p style="width:700px;">
        Lorem ipsum dolor sit amet, consectetur 
        adipiscing elit. Praesent varius mi a leo
        ultrices consequat. Nam id venenatis ligula. 
        Suspendisse et faucibus eros. Donec quis 
        augue eu nunc cursus blandit vel vel odio.
        Suspendisse rutrum aliquet massa, eu ultricies
        elit laoreet a. Curabitur feugiat cursus.
      </p>
    </div>
<script type="text/javascript">
$(function(){
   $('.parentDiv').each(function(){
      var newWidth = $(this).find('p:first').width();
      $(this).width(newWidth);
   });
});

</script>
  </body>
pixeline
Scrollbars are not an option. I would like the div to stop shrinking at the right-hand edge of the fixed-width content.
jball
then i guess you will need to fix the width of the parent div and since you don't know it in advance, you'll have to resort to javascript, to give the parent div's width the value of its child p width, plus padding and margin.
pixeline
I really hope that's not the only solution. I can do that easily enough, but it really feels like a hack.
jball
@pixeline, if you edit your answer to include the js width setting, I'll mark it as the accepted one. Your original answer missed the point my question by a bit, but I think you're right about js being the only solution.
jball
done, using jquery.
pixeline
A: 

Consider using min-width:

<div style="border: 1px solid rgb(0, 0, 255); padding: 3px; min-width: 700px;">
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent varius mi a leo ultrices consequat. Nam id venenatis ligula. Suspendisse et faucibus eros. Donec quis augue eu nunc cursus blandit vel vel odio. Suspendisse rutrum aliquet massa, eu ultricies elit laoreet a. Curabitur feugiat cursus. </p>
</div>

Min-width is not officially supported in IE-6, but there are lots of workarounds which you can find on Google.

Blakomen
Unfortunately I have no idea what the width of the content inside will be, so I wouldn't know what to set min-width to.
jball