tags:

views:

2189

answers:

6

I want the outer div, which is black to wrap its divs floating within it. How can this be be done? I dont want to use "style='height: 200px' in the div with the 'outerdiv' id as I want it to be automatically the height of its content (eg, the floating divs).

<div id='outerdiv' style='border: 1px solid black;background-color: black;'>
<div style='width=300px;border: red 1px dashed;float: left;'>
 <p>xxxxxxxxxxxxxxxxxxxxxxxxxxxxx</p>
</div>

<div style='width=300px;border: red 1px dashed;float: right;'>
 <p>zzzzzzzzzzzzzzzzzzzzzzzzzzzzz</p>
</div>

+4  A: 

You can set the outerdiv's CSS to this

#outerdiv {
    overflow: hidden; /* make sure this doesn't cause unexpected behaviour */
}

You can also do this by adding an element at the end with clear: both. This can be added normally, with JS (not a good solution) or with :after CSS pseudo class (not widely supported).

The problem is that containers won't naturally expand to include floated children. Be warned with using the first example, if you have any children elements outside the parent element, they will be hidden. You can also use 'auto' as the property value, but this will invoke scrollbars if any element appears outside.

You can also try floating the parent container, but depending on your design, this may be impossible/difficult.

alex
+2  A: 

You may want to try self-closing floats, as detailed on http://www.sitepoint.com/blogs/2005/02/26/simple-clearing-of-floats/

So perhaps try either overflow: auto (usually works), or overflow: hidden, as alex said.

DarkWulf
A: 

First of all you don't use width=300px that's an attribute setting for the tag not CSS, use width: 300px; instead.

I would suggest applying the clearfix technique on the #outerdiv. Clearfix is a general solution to clear 2 floating divs so the parent div will expand to accommodate the 2 floating divs.

<div id='outerdiv' class='clearfix' style='width:600px; background-color: black;'>
    <div style='width:300px; float: left;'>
        <p>xxxxxxxxxxxxxxxxxxxxxxxxxxxxx</p>
    </div>

    <div style='width:300px; float: left;'>
        <p>zzzzzzzzzzzzzzzzzzzzzzzzzzzzz</p>
    </div>
</div>

Here is an example of your situation and what Clearfix does to resolve it.

Garrett
+1  A: 

Firstly, I highly recommend you do your CSS styling in an external CSS file, rather than doing it inline. It's much easier to maintain and can be more reusable using classes.

Working off Alex's answer (& Garret's clearfix) of "adding an element at the end with clear: both", you can do it like so:

    <div id='outerdiv' style='border: 1px solid black;background-color: black;'>
        <div style='width=300px;border: red 1px dashed;float: left;'>
            <p>xxxxxxxxxxxxxxxxxxxxxxxxxxxxx</p>
        </div>

        <div style='width=300px;border: red 1px dashed;float: right;'>
            <p>zzzzzzzzzzzzzzzzzzzzzzzzzzzzz</p>
        </div>
        <div style='clear:both;'></div>
    </div>

This works (but as you can see inline CSS isn't so pretty).

Lycana
Why did I get rated down?It works, and I rightly acknowledged that it wasn't a pretty solution.
Lycana
I'm not sure, I +1'd to get you back to 0 at least. Maybe you were voted down because you didn't correct his incorrect CSS... i.e. using equals sign to set a CSS property is incorrect.
alex
fair enough. I appreciate the +1 alex. I thought it was my statement was a fair one.
Lycana
A: 

overflow:hidden works for me... Thanks a lot. I also tried overflow:auto but scrollbar is displaying for the outer div

A: 

Thanks for this! Helped me solve a lot of annoying issues as an ex-table designer :)