How CSS Perspective Works

Shapes on their own have no depth, so the perspective property can't produce 3D effects by itself. Used together with transform, however, it lets you move and rotate elements along the X, Y, and Z axes and control the depth they appear to have.

What perspective adds to transforms

Move a square along the X or Y axis and the effect is obvious. Animate it along Z, though, and nothing seems to happen: the element travels closer and then farther from the viewer, but its size and position stay the same. Adding perspective is what changes that. It leaves X and Y movement untouched, but gives Z movement a sense of distance, so the square grows as it approaches and shrinks as it recedes.

Rotation shows the same pattern. Rotating around Z looks like ordinary spin. Rotate around X or Y without perspective and the square merely appears to narrow. With perspective in place, the nearer edge looks larger than the far edge and the rotation reads as genuine 3D. One caveat: at 90° on the X or Y axis (and at 270°, 450°, 630°, and so on) the element disappears from view, because without depth its width or height is genuinely 0 at that point.

Setting the value and the viewing origin

The perspective value expresses the distance between the viewer and the object's plane, and it also sets the strength of the effect. Large values push you farther away; small values make the perspective more pronounced.

The perspective-origin property selects the point from which you look at the object. It is centered by default. Move the object to the right with a centered origin and it looks as though you are viewing it from the left; the reverse holds too. The alternative is to keep the object centered and shift the origin itself — setting it to the side is like looking in from that side, and larger offsets exaggerate the effect.

The division of labor matters: perspective and perspective-origin are declared on the parent container and fix the vanishing point, while the child's own position and rotation come from transform.

Transform functions and why their order matters

A movement like translateX() is assigned to the transform property on the element being transformed, yet it behaves according to the perspective rules of its parent. Multiple functions can be chained, and three rules govern the result:

  1. Rotating an element transforms its coordinate system along with it.
  2. Translating an element moves it relative to its own coordinate system, not its parent's.
  3. The order of the functions changes the outcome.

To translate a square and then rotate it, the translate must come first; reversing them produces a different result. Two squares sharing identical transform values but declaring them in a different order will not look the same, whether the rotation is 2D or applied around the Y axis.

Reordering is not the only remedy — changing the values themselves can produce the same visual outcome:

transform: translateX(100px) rotateY(90deg);

That line is equivalent to:

transform: rotateY(90deg) translate<strong>Z(100px);

The first translates along X and then rotates. The second rotates first, altering the coordinate system, and then translates along Z. Different values, same result.

Building a cube

Markup and base styles

A .container wraps a .cube, which in turn contains six elements for the cube's sides.

<div class="container">
  <div class="cube">
    <div class="side front"></div>
    <div class="side back"></div>
    <div class="side left"></div>
    <div class="side right"></div>
    <div class="side top"></div>
    <div class="side bottom"></div>
  </div>
</div>

The .container receives the perspective. The .cube gets 200px sides and must be told to respect 3D transformations. Some presentational styles follow, but the key properties are the ones highlighted:

/* The parent container, with perspective */
.container {
  width: 400px;
  height: 400px;
  border: 2px solid white;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  perspective: 800px;
  perspective-origin: top right;
}

/* The child element, with 3D tranforms preserved */
.cube {
  position: relative;
  width: 200px;
  height: 200px;
  transform-style: preserve-3d;
}

/* The sides of the cube, absolutely positioned */
.side {
  position: absolute;
  width: 100%;
  height: 100%;
  opacity: 0.9;
  border: 2px solid white;
}

/* Background colors for the cube's sides to help visualize the work */
.front { background-color: #d50000; }
.back { background-color: #aa00ff; }

.left { background-color: #304ffe; }
.right { background-color: #0091ea; }

.top { background-color: #00bfa5; }
.bottom { background-color: #64dd17; }

Placing the six sides

The front face is simply pushed forward by 100px:

.front {
  background-color: #d50000;
  transform: translateZ(100px);
}

The back face can be sent backward with translateZ(-100px), or alternatively rotated 180deg and then moved forward:

.back {
  background-color: #aa00ff;
  transform: translateZ(-100px);


  /* or */
  /* transform: rotateY(180deg) translateZ(100px); */
}

The left and right sides likewise each allow more than one approach:

.left {
  background-color: #304ffe;
  transform: rotateY(90deg) translateZ(100px);


  /* or */
  /* transform: translateX(100px) rotateY(90deg); */
}

.right {
  background-color: #0091ea;
  transform: rotateY(-90deg) translateZ(100px);


  /* or */
  /* transform: translateX(-100px) rotateY(90deg); */
}

The top and bottom rotate on the X axis rather than the Y axis. Again there are multiple valid formulations:

.top {
  background-color: #00Bfa5;
  transform: rotateX(90deg) translateZ(100px);


  /* or */
  /* transform: translateY(-100px) rotateX(90deg); */
}
 
.bottom {
  background-color: #64dd17;
  transform: rotateX(-90deg) translateZ(100px);


  /* or */
  /* transform: translateY(100px) rotateX(90deg); */
}

At this point the cube is complete, and experimenting with different perspective and perspective-origin values will show how each one reshapes it.

The role of transform-style

The property accepts two values: flat (the default) and preserve-3d. Setting the latter does two things. First, the child sides are positioned in the same 3D space as the cube — under flat they are collapsed into the cube's plane. Effectively, preserve-3d passes the parent's perspective down to its children, so only the cube has to be animated rather than each side independently. Second, children render according to their location in 3D space, not their order in the DOM.

Three squares demonstrate the second point. The green one has a translateZ of 100px and sits in front; the blue one has translateZ of -100px and sits behind. The DOM order is green, red, blue. Under flat (or with no value set), blue paints on top and green ends up in back, following the DOM. Under preserve-3d, green comes forward and blue falls behind, following position in space.

Animating and tuning

The animation property goes on the .cube, ready for keyframes that rotate it on all three axes so it appears to roll through space:

animation: cubeRotate 10s linear infinite;
@keyframes cubeRotate {
  from { transform: rotateY(0deg) rotateX(720deg) rotateZ(0deg); }
  to { transform: rotateY(360deg) rotateX(0deg) rotateZ(360deg); }
}

Perspective is what supplies the depth that makes the roll read as movement left and right as well as back and forth.

So far the perspective values have stayed fixed. Three sliders in the original demo explore what happens when they don't:

  • The left slider sets perspective — smaller values make the effect more noticeable.
  • The right slider sets perspective-origin on the vertical axis, top to bottom.
  • The bottom slider sets perspective-origin on the horizontal axis, right to left.

Adjustments can be hard to perceive while the cube spins, so the demo includes a "Run animation" button to pause it. There is no single correct set of values; the right numbers depend on the animation, the object's size, and the intended effect, and they differ from project to project.