CSS floats
I must admit that I was extremely confused when I was positioning div
s with css today. I had two div
s inside of another div
. Something like the following.
<div id="container">
<div id="blue">This is the blue div.</div>
<div id="red">This is the red div.</div>
</div>
The css for the page was as follows:
div#container {
width:80%;
margin-left:auto;
margin-right:auto;
border:solid #000000;
border-width:5px;
}
div#blue {
background-color:blue;
width:50%;
float:left;
}
div#red {
background-color:red;
width:50%;
float:left;
}
What I expected and what I got were two different things.
Why did this happen? Well, apparently if you use the float option in css, it doesn’t mean that the div
s will still look like they are in the container. The easiest way to fix this is with a
or a <br />
somewhere after the second div
, though that also has its consequences. Probably the best way i’ve found is to use a div with a clear attribute:
<div id="container">
<div id="blue">This is the blue div.</div>
<div id="red">This is the red div.</div>
</div>
With the css for the div:
#clear {
clear:both;
}
That works quite nicely!