| margin-top of div doubles if adjacent parent div has no border
|
basic6

msg:4196112 | 3:25 pm on Sep 3, 2010 (gmt 0) | Hi, I use several (in my example 2) div boxes with absolute width (200) on the left and a box with variable width (remaining space) on the right. The boxes on the left are included in a container (divleft), the box on the right is included in divright. In my example, the right container has a red border. If I remove this border (i.e. change 1px to 0px), then "BOX1" has double its distance to the top of the page - which is 20 pixels (while "BOX RIGHT" is still 10 pixels from the top). If I change the margin of the class "box" from 10px to 5 px, then only "BOX RIGHT" is 5 pixels from the top, while "BOX1" is 10 pixels from the top. Can somebody please explain this behavior to me?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>TEST</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="expires" content="0"> <style type="text/css">
* { margin:0px; padding:0px; }
#divleft { float:left; width:200px; border:0px solid red; } /* left container */ #divright { margin-left:200px; border:1px solid red; } /* right container */
.box { border-style:solid; border-color:gray; border-width:1px; margin:10px; padding:5px; } /* This margin is not always 10px */ .boxtitle { background-color:gray; color:white; font-weight:bold; margin:0px; padding:5px; }
</style> </head> <body>
<div id="divleft"> <!-- left container -->
<div class="box boxleft"> <!-- BOX1 --> <p class="boxtitle">BOX1</p>
---<br>
</div>
<div class="box boxleft"> <!-- BOX2 --> <p class="boxtitle">BOX2</p>
---<br>
</div>
</div>
<div id="divright"> <!-- right container -->
<div class="box boxright"> <!-- BOX RIGHT --> <p class="boxtitle">BOX RIGHT</p>
TESTMAIN! <br>
</div>
</div>
</body> </html>
|
Shado

msg:4196128 | 3:56 pm on Sep 3, 2010 (gmt 0) | I can't see the css for .box boxright and .box boxleft, are they defined?
|
basic6

msg:4196132 | 4:02 pm on Sep 3, 2010 (gmt 0) | No they're not (anymore), but it doesn't make any difference.
|
Fotiman

msg:4196145 | 4:53 pm on Sep 3, 2010 (gmt 0) | Welcome to WebmasterWorld! You're seeing collapsing margins [w3.org]. This is one of the more confusing topics regarding CSS. Basically, the .box in your right box has a margin of 10px, which pushes #divright down from the top by 10px. Borders and padding will both prevent collapsing margins, so when you add a border to #divright, the margin of .box is contained within #divright instead of forcing #divright down from the top. Without the border, #divleft will get 10px of margin as well (collapsing margins), and then the .box within #divleft adds another 10px of margin, giving the appearance of doubling margins. One solution would be to add borders or padding to #divright and #divleft. Another solution (better, I think, in that it's easier to maintain) is to add a wrapper div around both columns, and apply the padding to that element instead. For example: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>TEST</title> <style type="text/css"> * { margin: 0; padding: 0; } #container { padding-top: 1px; } #divleft { float:left; width:200px; } /* left container */ #divright { margin: 0 0 0 200px; border: 0px solid red; } /* right container */ .box { border: 1px solid gray; margin: 5px; } /* This margin is not always 10px */ </style> </head> <body> <div id="container"> <div id="divleft"> <div class="box boxleft"> --- </div> </div> <div id="divright"> <div class="box boxright"> TESTMAIN! </div> </div> </div> </body> </html>
|
|
|
|
|