views:

174

answers:

4

I can't find a good solution to this problem. I have 2 divs inside a parent div. When I try and float the divs right and left to make 2 columns inside the parent div, the child divs end up underneath the parent div.

<head> 
<link rel="stylesheet" type="text/css" href="float.css" /></head><body class="wrapper">
<div class= "whole">
<div class="left">
<p> Hello </p>
</div>
<div class="right">
<p> Hello Again</p>
</div>
</div>
</body>

CSS is

.whole{padding: 30px 30px 15px 30px;background-color: yellow;margin-bottom: 30px;}
.left{width:50%; position:relative; float: left;background-color:green;}
.right{width:50%; position:relative; float: right;background-color:red;}

Why would the content in the child divs "right, left" be below the parent div "whole" ??

A: 

This is simple. Float makes the elements "float" out of the normal page content, which is why it has gone below your container div. Instead of using divs as your child elements, try using a span which are pretty much the same thing but will happily sit next to each other.

m.edmondson
A: 

Try adding these properties to the parent eg whole:

position:relative;
overflow:auto;

And this css to child divs:

position:absolute:
top: xxx;
left: xxx;

Note that you should use id instead of class for an element which is supposed to be only one in the docume'nt.

Sarfraz
overflow: auto; worked great. I didn't have to change the child divs or the position of the parent.
Peter
A: 

This problem is often referred to as clearing floats. This page has a couple solutions http://www.positioniseverything.net/easyclearing.html along with a link to newer information. "When a float is contained within a container box that has a visible border or background, that float does not automatically force the container's bottom edge down as the float is made taller. Instead the float is ignored by the container and will hang down out of the container bottom like a flag."

joelt
A: 

Clearing with overflow:auto; or the using the clearfix:after trick on the parent div should be sufficient. I wouldn't give absolute positioning to the children as this will stop any elements beneath the wrapper flowing naturally with the page.

staticvoid