Thanks to visit codestin.com
Credit goes to www.cssportal.com

CSS Portal

perspective() CSS Function

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The perspective() CSS function defines a 3D perspective view for elements, simulating depth by affecting how 3D transformed elements appear in space. When you apply perspective() to a parent element, it determines the vanishing point for its child elements that are transformed with transform functions such as rotateY(), rotateX(), or translateZ(). The smaller the perspective value, the more pronounced the 3D effect, making elements appear as if they are closer to the viewer. Conversely, a larger value creates a subtler 3D effect, making the element appear farther away.

The perspective() function only affects elements in 3D space and has no visual effect on elements without 3D transformations. Unlike the transform property, which directly modifies the element’s shape or position, perspective() controls the viewing angle and depth perception. It is often used together with transform-style set to preserve-3d to ensure child elements retain their 3D transformations in the context of the parent.

For example, consider a div with a cube inside it:

.parent {
  perspective: 800px;
}

.child {
  transform: rotateY(45deg) translateZ(50px);
  transform-style: preserve-3d;
}

Here, the perspective value of 800px makes the 3D rotation and translation of the child element visible, giving the illusion that it is angled and positioned in 3D space. Without perspective(), the rotation would appear flat and lose the depth effect.

The function can be combined with animations for dynamic 3D effects, such as spinning cards or interactive 3D galleries. It is commonly applied to the parent container rather than individual elements to maintain consistent depth perception across multiple children.

If desired, you can also use the shorthand property perspective-origin to adjust the origin point from which the perspective is calculated, altering the apparent viewpoint.

Syntax

perspective() = perspective( <size>)

Values

  • sizeIs a <length> representing the distance from the user to the z=0 plane. If it is 0 or a negative value, no perspective transform is applied.

Example

<div class="container">
<div class="cube">3D Card</div>
</div>
/* The parent container centers the item */
.container {
width: 100%;
height: 300px;
display: flex;
justify-content: center;
align-items: center;
background-color: #f0f0f0;
}

/* The element being transformed */
.cube {
width: 150px;
height: 150px;
background-color: #3498db;
color: white;
display: flex;
justify-content: center;
align-items: center;
font-weight: bold;
border-radius: 8px;
box-shadow: 0 10px 20px rgba(0,0,0,0.2);

/* 1. Establish perspective (distance from viewer)
2. Rotate the element to see the depth
*/
transform: perspective(500px) rotateY(45deg);

/* Smooth transition for hovering */
transition: transform 0.5s ease;
}

/* Change the rotation on hover to see the 3D effect in motion */
.cube:hover {
transform: perspective(500px) rotateY(-20deg) rotateX(20deg);
background-color: #2980b9;
}

Browser Support

The following information will show you the current browser support for the CSS perspective() function. Hover over a browser icon to see the version that first introduced support for this CSS function.

This function is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 31st December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!