I prepared this example to visualize my question:
body {
text-align: center;
}
.child-1 {
width: 50px;
height: 50px;
background: rgba(200, 200, 255, 0.5);
z-index: 100;
position: absolute;
}
.child-2 {
width: 100%;
height: 50px;
margin-top: 100px;
background: rgba(255, 255, 200, 0.5);
}
.case1 {
background: red;
}
.case2 {
background: red;
border: 5px solid black;
}
<div class="case1">
<div class="child-1">
child 1
</div>
<div class="child-2">
child 2
</div>
</div>
<hr>
<div class="case2">
<div class="child-1">
child 1
</div>
<div class="child-2">
child 2
</div>
</div>
Case 1: Why is the default position of child1 (with position: absolute) not in the top left corner, but somehow moved down because of "margin collapsing" (as someone mentioned)? I don't understand how child2's margin (which is on the same layer as child1) could affect child1's position. And I don't understand what it has to do with collapsing margins.
Case 2: The only thing that was changed was the border of the containing element. But how can adding a border to the parent affect the position of it's childs? I don't get it.