I'm learning 3D transforms, playing around with building some interactive 3D models with CSS. Here's a codepen outlining a basic example. In the "sibling" layout, the result is as expected. In the "nested" layout, the Z-transforms don't "stack" as I would've thought: https://codepen.io/jconnorbuilds/pen/wBwwqqb
A (slightly) more complicated example, HTML only:
I started with a simple cube, with each of the faces laid out as siblings.
<div class="cube">
<div class="front"></div>
<div class="back"></div>
<div class="top"></div>
<div class="bottom"></div>
<div class="left"></div>
<div class="right"></div>
</div>
The problem came up when I tried building on top of the cube. What I tried: I initially spent a lot of time trying to nest elements. What I expected: Elements would stack on top of each other in 3D space. What happened: Each nested element would seem to take the correct X and Y rotations from the parent, but would stay flat in the Z direction, relative to the parent.
My HTML would look something like this:
<div class="scene">
<div class="cube__front"></div>
<div class="cube__back"></div>
<div class="cube__top">
<div class="smaller-cube__front"></div>
<div class="smaller-cube__back"></div>
<!-- etc. etc. -->
</div>
<div class="cube__bottom"></div>
<div class="cube__left"></div>
<div class="cube__right"></div>
</div>
I was able to get the effect I was looking for by organizing all of the elements as siblings to each other, but is there any way to do it with the nested approach I tried initially? Example of a working structure:
<div class="scene">
<div class="cube__front"></div>
<div class="cube__back"></div>
<div class="cube__top"></div>
<div class="cube__bottom"></div>
<div class="cube__left"></div>
<div class="cube__right"></div>
<!-- smaller cube to go on top of the main cube -->
<div class="smaller-cube__front"></div>
<div class="smaller-cube__back"></div>
<!-- more smaller-cube faces -->
</div>