How do I center and then float the two divs 'left' and 'right' to left and right and still keep the red color in the div midt? ********************* ******** HTML ******** ********************* <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link rel="stylesheet" type="text/css" href="style.css" /> <title>CSS layout</title> </head> <body> <div id="top"> <div id="bar1"> Bar 1 </div> <div id="bar2"> Bar 2 </div> </div> <div id="midt"> <div id="bar3"> Bar 3 </div> <div id="left"> Indhold 1 - venstre </div> <div id="right"> Indhold 2 - højre </div> </div> <div id="bund"> <div id="bottom"> Bar 4 </div> </div> </body> </html> ********************* ******** CSS ********* ********************* body { margin: 0; padding: 0; } #top { margin:0; padding:0; width:100%; background-color: #000099; } #bar1 { margin: 0 auto; width: 750px; background-color: #FF3366; } #bar2{ margin: 0 auto; width: 750px; background-color: #33CC33; } #midt { margin:0; padding:0; width:100%; background-color: #FF0000; } #bar3 { margin: 0 auto; width: 750px; background-color: #333333; } #left { margin: 0 auto; width: 450px; background-color: #CCFF33; } #right { margin: 0 auto; width: 300px; background-color: #0000FF; } #bund { clear:both; margin:0; padding:0; width:100%; background-color:#000099; } #bottom { margin: 0 auto; width: 750px; background-color: #9933CC; }
The easiest thing to do would be to create a container DIV. #container { margin: 0 auto; width: 750px; } #left { float: left; width: 450px; background-color: #CCFF33; } #right { float: right; width: 300px; background-color: #0000FF; } Code (markup): Then you would just structure it like <div id="container"> <div id="left">left column</div> <div id="right">right column</div> </div> [I]<div style="clear:both"></div>[/I] Code (markup): I haven't tested it but that should work. Just don't forget to clear your floats or that can mess things up down the road. See if that does it for you. Cheers
Hi Jared, Thanks, it works perfect, you just saved my day Can you explain this line: <div style="clear:both"></div> ...and why we don't define that in the CSS? Thanks again!
http://css.maxdesign.com.au/floatutorial/clear.htm If you don't clear you floats, things can start to get weird... things might not wrap around the DIVS properly. It is kind of hard to explain. Alternately you could do this: .clearfloat { clear:both; } Code (markup): <div id="container"> <div id="left">left column</div> <div id="right">right column</div> </div> <div class="clearfloat"></div> Code (markup): cheers
Or better yet, put the clear property on the element that comes after the main DIV container and save yourself the unnecessary markup.
Clearing the floats is always good practice. Say the container div had a background colour... well if you dont clear the divs that you are floating left and right, the background colour of your container is likely to completely disappear - its a problem i came against a while ago, took me a while to find out how to resolve the issue!