UPDATE: This question was marked as duplicate by a friend, but I think the answer is still very valuable. I looked into those answers in duplicate questions and no one mentioned transform-style: preserve-3d
can do transform without creating new stacking context. So this problem is more specific than how z-index works. It's also about how transform works.
I was trying to add some animation when hovering over a div element. But when I added transform in hover, its pseudo child element's background color covered div's. It seems that this wired behavior only happens when using transform. I want to know what's the mechanism behind this behavior.
In the following codepen example, the first one is hover with transform, the second one is normal hover.
https://codepen.io/neirongkuifa/pen/PgaEZd
.container {
width: 100px;
height: 100px;
background-color: red;
position: relative;
margin-bottom:100px;
}
.move:hover {
transform: translateY(3px);
}
.changeColor:hover{
background-color:white
}
.container::after {
content: '';
display: inline-block;
position: absolute;
top: 0;
left: 0;
background-color: green;
width: 150px;
height: 150px;
z-index: -1;
}
<div class="container move">Content</div>
<div class="container changeColor">Content</div>