views:

625

answers:

3

I would like the parent-div (red) to grow with the green child-div. Now it just stops at the viewport.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html lang="de" xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
<head>
<title>simple document</title>
<style type="text/css">
 * {
  font-family: verdana;
  margin: 0px;
  padding: 0px;
 }
</style>
</head>
<body>
<div style="margin: 30px; background: red; padding: 10px;">
 <div style="background: green; width: 2000px;">dxyf</div>
</div>
</body>
</html>

I don't want to use display:table; since it does not work well in IE.

Any ideas?

+5  A: 

Use display: inline-block; on the parent <div> and it will work as expected

Josh Stodola
Yeah! That's great! Thank you, I never thought this would work in IE too - just learnt something useful!
vyden_st
Quirksmode (http://www.quirksmode.org/css/display.html) says that you can't use the `inline-block` display on elements that don't default to `display: inline` for IE 6 and 7. Have you tested it in those?
Alex Ciminian
@Alex True, but this is the forward-thinking solution that adheres to the standard. To address older browsers, conditional comments can be used to apply a `float` to the parent element (if needed).
Josh Stodola
I solved it by changing the div to a span und give the parent display:inline-block;This way IE 6 - 8 and FF will display the site correct.I just have to test if this will work in FF2 too..But anyway - thanks!
vyden_st
Err.. apparently we need the div (no inline-element). Is there a way without conditional comments? Pure CSS? Would be awesome :)
vyden_st
+1  A: 

Make the parent div float:left; and it will be expanded as desired.

Paul
In FF2 this would not work _properly_ - the child grows larger than the parent.. how could we solve this? Apparently this is due the margin of the parent-div.
vyden_st
A: 

Hi,

I know I'm late, but here's what I do to fix the problem:

Add the clear INSIDE the parent at the bottom, and make the parent overflow: hidden.

Here's the modified code:

.clear{
    clear: both;
    /* make sure there is no height set to it */
    line-height: 0;
    height: 0;
    font-size: 0em;
}

<div style="overflow: hidden; margin: 30px; background: red; padding: 10px;">
 <div style="background: green; width: 2000px;">dxyf</div>
 <div class="clear">/div>
</div>

Works in FF3 and IE7, but not tested in other browsers though.

Hope to, at least, help you with your problem.

Jean-Francois