I have been reading some tutorials about floats from various sources on the web. They say that if you want to float one div element next to another you are to put a float attribute into one of the div elements. for example one website says this...
Quote From - maxdesign.com
To float the left nav, we need to use the rule: "#leftnav {float: left;}". When a div is set to float, a width must also be included, so we can add another declaration: "width: 160px;".
However, none of these examples appear to work within my code. When I add float: left;
to the #left
div element, in the code below, the #right
div element disapears behind the #left
div element with its text pushed down below the #left
div element. Yet if I were to add it to the class .columns
it happily aligns the two div elements like in the quoted example.
HTML Code
<!DOCTYPE html>
<html>
<body>
<div class="columns" id="left">left</div>
<div class="columns" id="right">right</div>
</body>
</html>
CSS Code
body { padding: 0px; margin: 0px; }
.columns { width: 100px; height: 170px; padding: 10px; border: 1px solid black; }
#left { background: #AAAAFF; margin-right: 10px; }
#right { background: #FFAAAA; }
Could someone please explain what I am doing wrong or misunderstand?