CSS3 3D Transforms
CSS 3D transforms let you rotate, move and scale elements along three axes instead of two, so a card can flip over, a panel can tilt away from the viewer or a cube can spin. They extend the 2D transform property with a depth axis, called Z, and rely on perspective to make that depth visible on a flat screen.
The third axis and perspective
X runs left to right, Y runs top to bottom, and Z points out of the screen toward you. Without perspective, a rotation around the Y axis just makes an element look narrower, because the browser draws it flat. Perspective tells the browser how far the viewer is from the screen. A small value such as 300px gives a strong, dramatic effect; a large value such as 1500px looks subtle.
You can set perspective in two ways:
perspective: 800px;on the parent, which gives all children one shared vanishing point.transform: perspective(800px) rotateY(45deg);on the element itself, which gives it its own viewpoint.
3D transform functions
| Function | What it does |
|---|---|
rotateX(angle) | Tilts forward or back, like a garage door. |
rotateY(angle) | Turns left or right, like a revolving door. |
rotateZ(angle) | Spins in the screen plane, same as 2D rotate. |
translateZ(length) | Moves toward or away from the viewer. |
translate3d(x, y, z) | Moves on all three axes at once. |
scaleZ(n) | Scales depth, visible only on children with Z offsets. |
Modern browsers also accept the separate properties rotate, translate and scale, which can be animated independently.
Building a flip card
The classic 3D example is a card that shows a front face and flips to reveal a back face on hover.
.scene {
width: 220px;
height: 300px;
perspective: 900px;
}
.card {
position: relative;
width: 100%;
height: 100%;
transform-style: preserve-3d;
transition: transform 0.6s ease;
}
.scene:hover .card {
transform: rotateY(180deg);
}
.face {
position: absolute;
inset: 0;
backface-visibility: hidden;
display: grid;
place-items: center;
border-radius: 12px;
}
.front { background: #2563eb; color: #fff; }
.back { background: #f59e0b; transform: rotateY(180deg); }
Three properties make this work. transform-style: preserve-3d on the card keeps its children in real 3D space instead of flattening them. backface-visibility: hidden hides each face when it is turned away. The back face starts already rotated 180 degrees, so when the card turns, the back comes around to face you.
Where 3D effects break
The most common surprise is that preserve-3d silently stops working. Certain properties on the same element force it to flatten, including overflow set to anything other than visible, filter, opacity below 1 in some browsers, and clip-path. If your faces suddenly overlap or the back shows through, check for these first.
Another mistake is putting perspective on the element that rotates instead of its parent. When the perspective rotates along with the element, the effect looks wrong. Keep the perspective on a still container. Also, the transformed element's hover area moves with it, so hovering the card itself can make it flicker; hovering a static parent, as above, avoids that.
Using 3D with care
Transforms are handled efficiently by most browsers because they do not trigger layout changes. Still, large spinning elements can make some people feel dizzy. Wrap strong motion in a @media (prefers-reduced-motion: reduce) query and turn it off or shorten it there.
The essentials
Give a parent a perspective, keep children in 3D with preserve-3d, rotate or translate on the X, Y and Z axes, and hide back faces when building flip effects. When something flattens unexpectedly, look for overflow or filter on the same element.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.