Connecting Tech Pros Worldwide Help | Site Map

css positioning and floats

Member
 
Join Date: Apr 2008
Posts: 45
#1: Jul 31 '09
I have 3 divs. I want one on the left side of the body, one in the middle and one on the right.

I can accomplish this easily with absolute positioning. However when I try to use floats the right div ends up being pushed down.

Suggestions?

Here is my code:


Expand|Select|Wrap|Line Numbers
  1.  
  2. <style type="text/css"> 
  3. <!-- 
  4. body
  5. {
  6.     margin:    0px;
  7.     padding:0px;
  8. }
  9.  
  10. #left_side
  11. {
  12.     float:left;
  13.     width: 200px;
  14.     background:        #06F;
  15.  
  16. }
  17. #middle
  18. {
  19.     margin:        0px auto;
  20.     width: 600px;
  21.     background:     #3F0;
  22.  
  23. }
  24. #right_side
  25. {
  26.     float:right;
  27.     width: 200px;
  28.     background:    #F60;
  29.  
  30.  
  31. }
  32.  
  33. --> 
  34. </style>
  35. </head>
  36.  
  37. <body>
  38.     <div id="left_side"><br />
  39.     <br />   
  40.     </div>
  41.  
  42.     <div id="middle">
  43.     <br />
  44.     </div>
  45.  
  46.     <div id="right_side">
  47.     <br />
  48.     </div>
  49.  
  50.  
  51. </body>
  52. </html>
  53.  
  54.  
drhowarddrfine's Avatar
Expert
 
Join Date: Sep 2006
Posts: 5,561
#2: Jul 31 '09

re: css positioning and floats


The reason is because your center element is still in the normal flow as a block element. Adding float:left to this will solve the dropping problem but you'll still need to add a margin to center it.
Member
 
Join Date: Apr 2008
Posts: 45
#3: Jul 31 '09

re: css positioning and floats


I have to use margin: 0 auto; on the middle part to keep it in the middle. If I give it float then it ends up against the left div. I don't know what the margin should be.
drhowarddrfine's Avatar
Expert
 
Join Date: Sep 2006
Posts: 5,561
#4: Aug 1 '09

re: css positioning and floats


Then just float all three of them left, wrap them in a div and center that.
Expand|Select|Wrap|Line Numbers
  1. #wrapper{
  2.         background:#ccc;
  3.         overflow:auto;
  4.         margin:0 auto;
  5.         width:1000px;
  6. }
  7.  
Reply