The CSS perspective: property is part of theIndividual Transform Properties specification. It defines **how far the viewer is from a 3D element**, giving depth and realism to transforms likerotateX(), rotateY(), or translateZ().
Note
β Smaller values = stronger depth
β Larger values = flatter 3D look
β Can be applied directly to an element or its parent
π¦ Syntax
perspective syntax
perspective: <length>;
/* Example */
perspective: 600px;The value must be a length (px, em, rem). Smaller numbers create dramatic depth; large numbers create subtle depth.
π¨ 1. Basic Example
Basic perspective
.box {
perspective: 500px;
rotateY: 30deg;
}The element tilts in 3D space because the viewer is 500px away.
π¨ 2. Strong 3D Depth (Lower Perspective)
Strong depth
.card {
perspective: 200px;
rotateX: 45deg;
}Lower values make the 3D effect stronger and more dramatic.
π¨ 3. Subtle 3D Effect (Higher Perspective)
Soft depth
.panel {
perspective: 1200px;
rotateY: 15deg;
}Higher values produce gentle, realistic movement.
π¨ 4. Using perspective in a Parent (Most Common)
Setting perspective on a parent applies depth to all children. This is the recommended method.
Perspective on parent
.scene {
perspective: 700px;
}
.card {
rotateY: 30deg;
}π¨ 5. Combine perspective: with translateZ()
translateZ demo
.popup {
perspective: 600px;
translate: 0 0 40px;
}Moving elements toward the viewer creates futuristic effects.
π¨ 6. 3D Flip Card (Real-world example)
flip card
.scene {
perspective: 800px;
}
.card {
rotateY: 0deg;
transition: rotate 0.6s;
transform-style: preserve-3d;
}
.card:hover {
rotateY: 180deg;
}Adding perspective to the parent creates a believable 3D card flip.
π perspective: vs perspective()
| Feature | perspective: | perspective() (transform) |
|---|---|---|
| Type | Individual property | Function inside transform |
| Applies to | Element itself or its children | Only the element being transformed |
| Better readability | β Yes | β No (mixed inside transform) |
| Combine with rotate/scale/translate | β Easy | β Must rewrite entire transform |
π¨ 7. Animation with perspective:
3D entrance animation
.animate {
animation: popIn 0.6s ease-out forwards;
}
@keyframes popIn {
0% {
perspective: 300px;
rotateX: -45deg;
opacity: 0;
}
100% {
perspective: 800px;
rotateX: 0deg;
opacity: 1;
}
}β οΈ Common Mistakes
- β Expecting 3D effect without rotateX/rotateY/translateZ
- β Setting perspective too small β extreme distortion
- β Using perspective on inline elements (convert to block/inline-block)
- β Forgetting transform-style: preserve-3d; on 3D children
Note
π₯ Summary
perspective: is essential for realistic 3D effects in CSS. It controls how strong (or subtle) the depth appears and works beautifully withrotateX, rotateY, and translateZ.
- perspective: sets the viewer distance
- Lower value β stronger 3D depth
- Best used on a parent for true 3D scenes