CSS 3D Transforms
CSS can also transform elements in three dimensions. A third axis, z, points out of the screen toward you. With 3D transforms you can flip cards over, tilt panels and even build simple cubes. All of it stays pure CSS.
3D functions
| Function | Effect |
|---|---|
rotateX(angle) |
Tilt forward or back around the horizontal axis |
rotateY(angle) |
Turn left or right around the vertical axis |
rotateZ(angle) |
Spin in the screen plane (same as 2D rotate) |
translateZ(z) |
Move toward (+) or away from (−) the viewer |
translate3d(x, y, z) |
Move on all three axes |
scale3d(x, y, z) / scaleZ(z) |
Scale, including depth |
rotate3d(x, y, z, angle) |
Rotate around any axis |
perspective(n) |
Perspective for this one element (as a transform function) |
Perspective makes it 3D
Without perspective, a rotateY just looks like the element got narrower. Perspective sets how far away the viewer is: a small value such as 200px gives dramatic depth, while a large one such as 1500px is subtle. Put it on the parent with the perspective property, or inside the transform list with perspective().
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; gap: 30px; padding: 30px; }
.stage { width: 120px; height: 120px; outline: 2px dashed #b0bec5; }
.p { perspective: 300px; }
.box { width: 120px; height: 120px; background: linear-gradient(135deg, #42a5f5, #7e57c2); color: white;
display: grid; place-items: center; transform: rotateY(50deg); font-size: 13px; text-align: center; }
.x { transform: rotateX(55deg); }
</style>
</head>
<body>
<div class="stage"><div class="box">rotateY, no perspective</div></div>
<div class="stage p"><div class="box">rotateY + perspective</div></div>
<div class="stage p"><div class="box x">rotateX + perspective</div></div>
</body>
</html>| Property | Set on | Purpose |
|---|---|---|
perspective |
Parent | Distance of the viewer from the z=0 plane |
perspective-origin |
Parent | Where the viewer is looking from (default centre) |
transform-style: preserve-3d |
Parent | Children keep their own 3D positions instead of being flattened |
backface-visibility: hidden |
Element | Hide the element when it's turned away from the viewer |
Tip: A parent's
perspectivegives all its children one shared vanishing point, which looks realistic for a group. Theperspective()function gives each element its own.
A flip card
The classic 3D effect: a card with a front and a back that flips on hover. The key ingredients are preserve-3d on the rotating element, backface-visibility: hidden on each face, and a back face that starts already rotated by 180°.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: grid; place-items: center; height: 240px; margin: 0; }
.scene { width: 180px; height: 200px; perspective: 800px; }
.card { position: relative; width: 100%; height: 100%; transform-style: preserve-3d; transition: transform .7s; cursor: pointer; }
.scene:hover .card { transform: rotateY(180deg); }
.face { position: absolute; inset: 0; border-radius: 14px; display: grid; place-items: center; font-size: 22px; font-weight: bold;
backface-visibility: hidden; box-shadow: 0 6px 16px rgba(0,0,0,.25); }
.front { background: linear-gradient(135deg, #ff9a9e, #fad0c4); color: #880e4f; }
.back { background: linear-gradient(135deg, #a1c4fd, #c2e9fb); color: #0d47a1; transform: rotateY(180deg); }
</style>
</head>
<body>
<div class="scene">
<div class="card">
<div class="face front">Hover me 🂠</div>
<div class="face back">Hello! 👋</div>
</div>
</div>
</body>
</html>Building a cube
Six faces, each rotated to face a different direction and then pushed out by half the cube's size with translateZ. Rotating the parent shows off the 3D structure.
<!DOCTYPE html>
<html>
<head>
<style>
body { display: grid; place-items: center; height: 250px; margin: 0; font-family: system-ui, sans-serif; }
.scene { width: 100px; height: 100px; perspective: 500px; }
.cube { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transform: rotateX(-25deg) rotateY(35deg); transition: transform 1s; }
.scene:hover .cube { transform: rotateX(-25deg) rotateY(215deg); }
.cube div { position: absolute; width: 100px; height: 100px; display: grid; place-items: center; color: white; font-weight: bold; opacity: .9; border: 2px solid white; box-sizing: border-box; }
.f1 { background: #e53935; transform: rotateY(0deg) translateZ(50px); }
.f2 { background: #fb8c00; transform: rotateY(90deg) translateZ(50px); }
.f3 { background: #fdd835; transform: rotateY(180deg) translateZ(50px); color: #333; }
.f4 { background: #43a047; transform: rotateY(-90deg) translateZ(50px); }
.f5 { background: #1e88e5; transform: rotateX(90deg) translateZ(50px); }
.f6 { background: #8e24aa; transform: rotateX(-90deg) translateZ(50px); }
</style>
</head>
<body>
<div class="scene">
<div class="cube">
<div class="f1">1</div><div class="f2">2</div><div class="f3">3</div>
<div class="f4">4</div><div class="f5">5</div><div class="f6">6</div>
</div>
</div>
</body>
</html>Hover over the cube to spin it half a turn.
Subtle 3D tilt
3D transforms don't have to be dramatic. A small rotateX or rotateY on hover gives cards a tactile feel:
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; gap: 24px; padding: 30px; perspective: 600px; }
.tile { width: 130px; height: 150px; border-radius: 14px; background: #263238; color: #80deea; padding: 14px; box-sizing: border-box;
transition: transform .3s, box-shadow .3s; box-shadow: 0 4px 10px rgba(0,0,0,.2); }
.left:hover { transform: rotateY(15deg); box-shadow: -10px 10px 20px rgba(0,0,0,.3); }
.up:hover { transform: rotateX(15deg); box-shadow: 0 16px 20px rgba(0,0,0,.3); }
</style>
</head>
<body>
<div class="tile left">Hover: tilt sideways</div>
<div class="tile up">Hover: tilt back</div>
</body>
</html>Note: Respect users who get motion sick. Wrap big 3D animations in
@media (prefers-reduced-motion: no-preference) { … }so they play only when the user hasn't asked for reduced motion.
Test yourself
Why does rotateY(60deg) look flat without perspective?
- rotateY is 2D only
- Without perspective there is no depth, so the element just looks narrower
- It needs z-index
- Browsers don't support it
Which property keeps children in 3D space instead of flattening them into the parent?
- perspective: 3d
- transform-style: preserve-3d
- backface-visibility: visible
- display: 3d
In a flip card, what hides the front face while the back is showing?
- opacity: 0
- display: none
- backface-visibility: hidden
- z-index: -1