Why does "b" lays over "a" if they're on the same z-index? instead of laying under "a"
It seems that I read all materials on developer.mozilla.org and w3.org explaining z-index property but I still can't explain why the green square overlaps yellow one.
From one hand
Absolutely positioned elements are removed entirely from the document flow. That means they have no effect at all on their parent element or on the elements that occur after them in the source code. An absolutely positioned element will therefore overlap other content unless you take action to prevent it. Sometimes, of course, this overlap is exactly what you desire, but you should be aware of it, to make sure you are getting the layout you want!
From another hand
When no z-index property is specified, elements are rendered on the default rendering layer 0 (zero).
However it is not clearly described what happens when two elements share the same z-index value. In the example both elements doesn't have any specific z-index value so z-index equals "0" zero by default.
Why does "b" lays over "a" if they're on the same z-index? instead of laying under "a"
* {
padding: 0;
margin: 0;
}
.a,
.b {
width: 200px;
height: 200px;
font-size: 36px;
}
.a {
background-color: grey;
position: absolute;
}
.b {
background-color: green;
margin-left: 50px;
position: absolute;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="a">a</div>
<div class="b">b</div>
</body>
</html>