Forum Moderators: not2easy
<div id="container">
<div id="left">Left<br /><br /><br /><br /><br />Left</div>
<div id="right">Right<br /><br /><br /><br /><br />Right</div>
<div>
#container{
width: 500px;
margin: auto;
background-color: #FF0000;
}
#left{
width: 100px;
float: left;
background-color: #00FF00;
}
#right{
width:100px;
float: right;
background-color: #0000FF;
}
You can change this by either making #container itself a floated element (which will no doubt ruin its positioning), or use a clearing element below #left and #right in your code to force #container to contain it, and as a consequence, any elements above it.
At its simplest, this clearing element could be:
<div style="clear: both;"><br /></div>
but there are a lot or cross-browser issues with clearing floats, especially if you want to avoid the clearing element taking up any actual space itself. The different methods are covered here:
and I believe Big John at Position Is Everything has detailed a new method using the :after psuedo element.
To make it work properly in standards compliant browsers you need to enclose the floats in a "wrapper" div that is also floated. This wrapper will expand to contain the floated items so it displays the same in all browsers. It will still display correctly in IE, both now and if/when MS decides to "fix" IE so it works correctly.
Edit: It looks like jetboy_70 beat me to the reply on this one. That's what happens when you start a reply and then come back an hour later to finish it.
Jimmy
div.clear {
clear: both;
}
<div class="clear"></div>
...which seemed to work fine in Firefox and IE6 but I didn't test it in anything else.
I just added 'clear: both' to my footer and got rid of the above. The thing is, what if no floats come before my footer, is it still alright to have the clear in there?
Take care,
Emperor
what if no floats come before my footer, is it still alright to have the clear in there?
It certainly won't do any harm, other than maybe taking up a bit of vertical space.
I can sympathize. I recently found out that the clearing DIV I use wasn't being picked up by Firefox on the Mac. It's fine on Windows flavours of Moz - I've tested all the way back to 0.9 - and any other Windows, Mac and *nix browsers that have had any sort of userbase, but this one slipped through!