A few days ago I stumbled upon what I thought was a css bug in Firefox, but after testing the case with Opera and still getting the same "bugged" result I started to wonder if it was the browser that was bugged or just my way of looking at it. Could it be that for once IE6 and IE7 was correctly rendering my page whilst Firefox and Opera was being bad? Also, the "bug" seemed to fix itself if I used borders on the affected divs, weird uh?
The case
My layout consisted of basicly 3 divs. One at the top and a container div holding another div positioned 10px from the top of the container div. Just like below.

Here in the example source I only use simple divs without anything else than different background colors.
CSS:
-
div#top {
-
height: 20px;
-
width: 300px;
-
background: #0076a3;
-
}
-
-
div#content {
-
height: 200px;
-
width: 300px;
-
background: #f8941d;
-
}
-
-
div#banner {
-
margin-left: auto;
-
margin-right: auto;
-
height: 20px;
-
width: 274px;
-
background: #00a651;
-
margin-top: 10px;
-
}
The problem
When lookin at the page in IE6 or IE7 everything looks just fine, however in Opera and Firefox you'll get this result:

It seems that the inner div adds top-margin to it's parent instead of applying it to itself, why this happens I have no idea.
The first solution
One solution I found was that if I apply borders on the affected divs I get Opera and Firefox to render the page correctly. Why borders would change how margins are interpreted I have no idea. Anyway this isn't a solution to stick with since we don't want any borders, if used with background images it will ruin the design.
The final solution
After playing around some more I realized that if I instead of using margin on the div I wished to position I could ofcourse use padding on it's parent for the same result. And ta-da it works in all four browsers! However since I'm a Windows user I have no idea how any of this behaves in Safari, but I will update the post after I check using some online screenshot provider.
CSS:
-
div#top {
-
height: 20px;
-
width: 300px;
-
background: #0076a3;
-
}
-
-
div#content {
-
height: 200px;
-
width: 300px;
-
padding-top: 10px;
-
background: #f8941d;
-
}
-
-
div#banner {
-
margin-left: auto;
-
margin-right: auto;
-
height: 20px;
-
width: 274px;
-
background: #00a651;
-
}
If you have any opinions/facts about this being a bug or not and solutions feel free to post comments. You can view my test page with source and all 3 examples here