Hi,
Shouldn't the margin of the h1 behave exactly the same as if it's wrapper had a padding rather than passing the margin onto it's wrapper?
No it shouldn't 
This is the exact behavior that margins should have as described in the specifications and it is called "collapsing Margins". Every author should know about collapsing margins as it has a direct effect on the way that elements are rendered.
Margins on vertically adjacent elements or nested elements that have touching edges. (i.e. no padding or borders separate them) will collapse their margin into one margin. Effectively the margin collapses onto the parent and becomes the parents margin instead.
Floated elements or elements with overflow do not collapse their margins (nor do inline-block or absolute elements). IE6 also won't collapse margins if the element has "haslayout" (but doesn't adhere to overflow's non collapsing behaviour).
In you example a mixture of overflow and "haslayout" will fix the problem.
e.g.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style>
div { background: orange;overflow:auto }
* html div{height:1%}
h1 { background: red;margin:1em 0}
</style>
</head>
<body>
<div style='background:green; height: 10px;overflow:hidden'></div>
<div>
<h1>Test</h1>
</div>
</body>
</html>
Overflow may not be the best choice and in those circumstances simply adding 1px of padding to the elements concerned will stop the collapse.
Although margin collapse may at first be unintuitive it does have its good points also.
See here for more info.
Bookmarks