I have 2 elements, each given a margin-top of 20px. The first one is positioned absolutely. Naturally, the second element ends up at the top and recedes from the edge of the screen by 20px.
div {
width: 200px;
height: 200px;
}
.posa {
margin-top: 20px;
position: absolute;
background-color: #1239;
}
.normal {
margin-top: 20px;
margin-left: 20px;
background-color: #aaa9;
}
<div class="posa">position</div>
<div class="normal">normal</div>
CSS absolute positioned elements and margins - This question was almost answered here, but it was talking about the parent element
Questions:
- Why does an absolutely positioned element take the margin of the second element that was below?
- Why don't their margins collapse so that the
.normal
element will recede from the top edge by 20px and the positioned element will recede by 40px. - Why does only the margin-top affect and the margin-left doesn't work that way?