You can see in the code below that the h1
pushes down the body and the absolutely-positioned block .absolute
does not stick to the top. But you also can see that the same block is stuck to the top of its parent .wrapper
. Why?
I'm not asking how to do that trick; I know how, e.g. padding instead margin to h1, or clearfix to parent and so on.
I'm interested in only one thing: why h1
's margin pushes down the body
, but is not pushing down .wrapper
?
body {
position: relative;
margin: 0;
padding: 0;
overflow: hidden;
background-color: silver;
}
.absolute {
position: absolute;
top: 0;
left: 0;
width: 50px;
height: 50px;
background-color: darkblue;
}
.wrapper {
position: relative;
overflow:hidden;
width: 50%;
height: 200px;
overflow: hidden;
background-color: yellow;
}
.wrapper > .absolute {
background-color: darkcyan;
}
<div class="absolute"></div>
<h1>Some title</h1>
<div class="wrapper">
<div class="absolute"></div>
<h1>Some title</h1>
</div>
OK, I'll try to be more clear. If you click on the link below, you can see my JSFiddle. There is background-color: silver
in the body
tag. When I look in the code inspector, I see that the body tag begins slightly lower due to the h1 margin. But background-color
begins at the top. This means that the code inspector is lying to me, and body begins from top. But why, then, does an absolutely-positioned element that's a direct child of body
not start at the top?