Friday, 7 August 2009

Div containing floating divs collapses

After about an hour of trying to work out why the spacing between my posts was working in Internet Explorer but not in Firefox, I stumbled upon a common issue.

If a Div contains floating divs it will not resize the height to contain the floating divs. So adding padding to the containg div has no effect. This is how floating divs are supposed to behave. IE ignores this and contains them anyway, but most other modern browsers follow the rules and let the inner divs float 'uncontained'.

The simple solution to this problem is to include and empty div after the floating divs and within the container. This empty div must not have a floating property and must be set to clear the floating divs. This gives the container div something to wrap around. The code will look something like this:

<div class="container">


<div class="float-left"> </div>


<div class="float-right"> </div>


<div style="clear:both;"> </div>


</div>



So the magic bit is "<div style="clear:both;"> </div>" just insert it just before you close the container div.

2 comments:

  1. Thanks a lot for that.

    My first search on the subject and I got the answer.

    ReplyDelete
  2. I've been meaning to read this and just never got a chance. Its an issue that Im really interested in. I just started reading and Im glad I did. Youre a fantastic blogger and I can't wait to see what you come up with next!

    ReplyDelete