Can anyone explain this behavior?
I have three nested elements: a container div, a subcontainer div and a "shape/image" div. The markup is like below:
<html>
<div class="container">
<div class="subcontainer">
<div class="shape"></div>
</div>
</div>
</html>
When I apply transform: translateZ() to the shape, obviously it will only work if one of the parents has some perspective applied. It doesn't need to be necessarily the direct parent, it can be the parent's parent div (the container). When the container has the perspective, the shape moves in the z direction fine. But, when the subcontainer (the parent) has something in the transform, other than unset, the translateZ() from the shape is fully ignored. Of course, applying perspective to the direct parent makes the translateZ() in the shape work, but I wanted to understand what in the parent transform blocks the perspective to be seen by the shape:
Below works:
.container{
perspective: 1000px;
}
.subcontainer{
transform: unset; // <- or if this is not present at all
}
.shape{
transform: translateZ(300px);
}
Below doesn't work:
.container{
perspective: 1000px;
}
.subcontainer{
transform: scale(1.001);
}
.shape{
transform: translateZ(300px); // <- this is fully ignored
}
Here is this example in codepen.
EDIT:
In many docs I read that perspective must be a property set in the parent element. But MDN seems to show that it can in fact be set in the intended element itself, inside the transform rule. So, is perspective: 500px in the parent equivalent to transform: perspective(500px) in the child?