3D Translation and Rotation
You'll learn how to move and spin elements in three-dimensional space using CSS transforms.
Now that you've explored 3D transforms, let's dive into the specific functions that move and rotate elements along the X, Y, and Z axes.
Moving in 3D Space with translate3d()
The translate3d() function moves an element in all three dimensions at once. Unlike the 2D translate(), it takes three values:
.box {
transform: translate3d(50px, 20px, 100px);
/* Moves right 50px, down 20px, and toward viewer 100px */
}
Think of it like giving GPS coordinates: X (left/right), Y (up/down), and Z (forward/backward).
Rotating in 3D Space
You can spin elements around each axis individually:
rotateX()
Rotates around the horizontal X-axis, like a door hinge:
.card {
transform: rotateX(45deg);
/* Tips the top backward */
}
rotateY()
Rotates around the vertical Y-axis, like a revolving door:
.card {
transform: rotateY(45deg);
/* Swings the left side forward */
}
rotateZ()
Rotates around the Z-axis (same as 2D rotate()):
.card {
transform: rotateZ(45deg);
/* Spins clockwise in place */
}
Combining 3D Transforms
You can chain these together for complex effects:
.element {
transform: translate3d(50px, 0, 100px) rotateY(30deg) rotateX(15deg);
}
Key Takeaway: Use translate3d() to move elements in 3D space and rotateX(), rotateY(), rotateZ() to spin them around different axes—essential for creating depth and dimension in your designs.