It is convenient for isometric grids to start at the top like this:
I have the following code to draw a "3d" isometric box:
body {
display: flex;
justify-content: center;
align-items: center;
}
.plane {
position: relative;
width: 600px;
height: 600px;
}
.container {
position: absolute;
width: 0;
height: 0;
}
.left {
background-color: #3e8fe1;
height: var(--width);
width: var(--height);
transform-origin: 0 0;
transform: rotate(90deg) skewX(-30deg) scaleY(0.864);
}
.right {
position: relative;
background-color: #2870bd;
height: var(--height);
width: var(--length);
transform-origin: 0 0;
transform: rotate(-30deg) skewX(-30deg) scaleY(0.864);
bottom: var(--width);
}
.top {
position: relative;
background-color: #80bdfe;
height: var(--length);
width: var(--width);
transform-origin: 0 0;
transform: rotate(210deg) skew(-30deg) scaleY(0.864);
bottom: calc(var(--width) + var(--height));
}
<body>
<div class="plane">
<div class="container" style="--width: 50px; --length: 200px; --height: 100px; left: 150px; top: 150px;">
<div class="left"></div>
<div class="right"></div>
<div class="top"></div>
</div>
</div>
</body>
This works, but is there any way to make it so that increasing the --width
, --length
, and --height
CSS variables would grow the box in the opposite directions that they are currently growing in?
That is, --width
is currently growing front to back, but should grow back to front (such that increasing it makes it extrude towards you instead of away from you)
Similarly, increasing the --height
should make it grow up, not down.
And lastly, increasing the --length
should make it grow towards the "camera", and not away from it, as it currently does.
2
Answers
I used translation in the
transform
line, which allowed me to create a counter-movement and realign the faces of the cube. I also noticed that you were using 3 separate divs in your example so I replaced 2 of them with the before and after pseudo-elements.I think you can simply change the value of the rotate to
value - 180
and add some tweak to thetranslate
transformation, and you will have it grow in the other direction accordingly:Note: the translate has
0.864
constant, you can adjust it according to the scale value (which is also0.864
).