Here is the situation:
body {
margin: 0;
background: pink;
color: #fff;
}
.box {
margin-top: 20px;
background: red;
}
.bottom {
text-align: right;
background: green;
animation: animate 2s infinite alternate linear;
}
@keyframes animate {
from {
margin-top: 10px;
}
to {
margin-top: -40px;
}
}
<div class="box">
some content
</div>
<div class="bottom">
other content
</div>
What's happening?
As you may see we have two div
without any complex styling (simply background-color). I am making the second div
to overlap the first one by applying a negative margin-top
. I am expecting to see one completely overlapping the other but this is not the case. The second div
is sliding between the content and the background of the first one and it's a strange behavior for me.
The animation has nothing to do here, I simply use it to better show the behavior. We can simply add negative margin without animation and we will have the same thing:
body {
margin: 0;
background: pink;
color: #fff;
}
.box {
margin-top: 20px;
background: red;
}
.bottom {
margin-top:-10px;
text-align: right;
background: green;
}
<div class="box">
some content
</div>
<div class="bottom">
other content
</div>
So my question is : why such behavior?
By the way, we all know that there is some tricky things with CSS that we don't suspect when we face them the first time (like margin-collapsing, background propagation from body to html, white space issue,etc...) but they are clearly explained somewhere and I hope to find an official resource where I can clearly understand this and not only get something like "Maybe this happen because...", "I suspect this related to...", "I think it have something to do with...",etc.
My opinion/explanation about this
I suppose content like text are more important than background and other visual styling so maybe when we have overlapping we place all the text at the top and all the other styling at the bottom, we decide about the order inside each group and then we print the result.
Here is a more complex example:
body {
margin: 0;
background: pink;
color: #fff;
}
div {
font-size: 39px;
line-height: 28px;
margin-bottom: -20px;
font-weight: bold;
}
body :nth-child(1) {
background: red;
border:3px solid brown;
}
body :nth-child(2) {
background: blue;
border:3px solid yellow;
color: #000;
}
body :nth-child(3) {
background: green;
border:3px solid orange;
}
<div>
some content
</div>
<div>
other content
</div>
<div>
more content
</div>
We can clearly see that the visual stack is as follow (starting from bottom to top):
- Styling of the first div(background + border)
- Styling of the second div(background + border)
- Styling of the third div(background + border)
- text content of the first div
- text content of the second div
- text content of the third div
Important notice: Before answering, please note that I am not looking for a fix to this or how to avoid this. By simply adding position:relative
the behavior disappear and we can play with z-index
to decide about stacking. Am looking to understand why such thing happen.