CSS propertyTransforms
perspective
Adds depth to the 3D transforms of an element’s children.
- Initial value
none- Inherited
- No
- Animatable
- yes, as a length
- Applies to
- transformable elements (the effect applies to their children)
- JavaScript
element.style.perspective = "600px"
Example
#CSS
.scene {
perspective: 700px;
}
.card {
transform: rotateX(20deg) rotateY(-20deg);
}Example HTML
<div class="scene" style="perspective: 700px; display: grid; place-items: center; padding: 24px;">
<div class="box card" style="width: 180px; height: 110px; transform: rotateX(20deg) rotateY(-20deg);">3D card</div>
</div>Preview
Definition and usage
#The perspective property is set on the parent. It defines how far from the screen the viewer’s eye is. Children rotated in 3D then look realistic: the nearer edge is larger and the farther one smaller.
A small value, for example 200px, gives a strong, dramatic perspective, as if viewing the element up close. A large value, for example 1500px, gives a subtle effect. none turns perspective off and 3D rotations look flat.
You can also set perspective for a single element with the perspective() function in its own transform.
Syntax
#Syntax
perspective: none | <length>;Values
#Values in practice
#none
No depth: the rotated element looks flat.
.scene { perspective: none; }800px
Moderate perspective.
.scene { perspective: 800px; }250px
Strong perspective, as if seen up close.
.scene { perspective: 250px; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- You set perspective on the parent and the rotation on the child. The other way round it won’t work.
- Start with a value of 600-1000px, which looks natural in most interfaces.
Related entries
#- perspective-originSets the point from which the viewer looks at a 3D scene.
- transform-styleDecides whether an element’s children live in a shared 3D space or are flattened.
- backface-visibilityDecides whether the back of an element rotated in 3D is visible.
- rotateRotates an element by a given angle, independently of the transform property.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.