Here is the puzzle. Main page, one element:
http://jsfiddle.net/PZj6t/
HTML:
<div id="container"></div>โ
CSS
body, html { height: 100%; margin: 0; padding: 0; background-color: black; } #container { position: relative; margin: 0 auto; width: 400px; height: 100%; background-color: #666; }โ
It looks the way I want it, with a C # container, gently flush to the top. But when I add a nested element:
http://jsfiddle.net/PZj6t/1/
HTML:
<div id="container"> <nav id="topnav"></nav> </div>โ
CSS (new):
#topnav { width: 100%; height: 40px; margin: 30px 0; background-color: red; }โ
The container is dumped. It seems that the margin-top from #topnav is somehow being passed to the container, and now there is a scroll bar on the page that I don't want. (I am testing in Chrome.) How can I prevent this?
(As an added secret, if I add border: 1px solid white; in #container CSS, the jump will disappear. This will be fine, except that it also adds unwanted two-pixel scrolls to the page.)
source share