Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

force a div to contain floated child divs

I'm trying to customize the twentyeleven theme in Wordpress. It has a 2-columns layout set up by:

<div id="main">
     <div id="primary"></div>
     <div id="secondary"></div>
</div>

and

#main {
    clear: both;
    padding: 1.625em 0 0;
}
#primary {
    float: left;
    margin: 0;
    width: 100%;
}
#secondary {
    float: right;
    margin-right: 7.6%;
    width: 18.8%;
}

I am not sure that there are other relevant css properties inside style.css which I have not recognized. Anyway, the child divs lie outside #main. How can I force to have the child divs contained in #main? (apart from reducing the width of #primary, which yields no effect to me)

like image 497
tic Avatar asked Aug 15 '12 05:08

tic


2 Answers

You need to add overflow: auto to #main:

#main {
    clear: both;
    padding: 1.625em 0 0;
    overflow: auto;
}
like image 107
Blender Avatar answered Oct 02 '22 13:10

Blender


You should use the clearfix method with pseudo-element :after because in your case, the clear isn't really applied after the children.

#main:after {
    content: ".";
    display: block;
    clear: both;
    visibility: hidden;
    line-height: 0;
    height: 0; 
}

http://jsfiddle.net/zfsjb/

EDIT 2020:

Since a while now, this simpler solution works just as fine:

#main::after {
    content: "";
    display: table;
    clear: both;
}

Also it’s now recommended to use ::after instead of :after to match other pseudo-elements.

like image 37
LeBen Avatar answered Oct 02 '22 13:10

LeBen