3D Transform & Perspective — CSS

perspective goes on the parent and sets how far the viewer's eye is from the elements inside it. A small value makes the 3D effect dramatic, and a large value…

A screen is flat, but CSS can turn elements in three-dimensional space, like a card you tilt or flip in your hand. For the turn to show depth (the part that moves away looks smaller), you have to set how far the viewer's eye is from the element. You write that distance in the perspective property.

A stage and a card

The card in this example is inside a stage:

<div class="stage">
  <div class="card">Hello</div>
</div>

<style>
.stage {
  perspective: 800px;
}
.card {
  transform: rotateY(40deg);
}
</style>

The result: the side of the card that moves away from the viewer looks smaller, so the card seems to tilt into the screen. Try removing the perspective: the same card only looks narrower, and stays flat.

Three axes to turn around

An axis is the line an element turns around. There are three:

For two turns at once, write both in one transform, separated by a space: transform: rotateX(20deg) rotateY(30deg);

Eye distance and viewpoint

Don't confuse perspective-origin with transform-origin, which is the point the element turns around, not where the viewer stands.

A card you can flip

A two-sided card needs three properties: perspective, transform-style and backface-visibility. In the HTML, .card-container holds .card, and .card holds two faces: .card-front and .card-back. This CSS uses all three properties:

.card-container { width: 200px; height: 200px; perspective: 1000px; }
.card { position: relative; height: 100%; transform-style: preserve-3d; transition: transform 0.6s; }
.card-container:hover .card { transform: rotateY(180deg); }
.card-front, .card-back {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
}
.card-back { transform: rotateY(180deg); }

⚠️ What stops a 3D card from flipping correctly

TL;DR: perspective goes on the parent and sets how far the viewer's eye is from the elements inside it. A small value makes the 3D effect dramatic, and a large value makes it gentle. The child then turns with rotateX(), rotateY() or rotateZ() in transform. perspective-origin sets the point the viewer looks from. For a card that flips, write transform-style: preserve-3d on the card, so both faces stay in 3D space. Also write backface-visibility: hidden on both faces, so the face with its back to the viewer is hidden.

⚡ A stage and a card

Write:

.stage {
  perspective: 800px;
}
.card {
  transform: rotateY(40deg);
}
Guess first: perspective sets how far the viewer's eye is. If you remove the perspective, how does the turned card look?

Only narrower, and flat. The side that moves away from the viewer no longer looks smaller.

Meaning: perspective: 800px means the viewer's eye is 800px in front of .stage. perspective goes on the parent (.stage) and works on its children (.card). transform goes on the child that turns: rotateY(40deg) turns .card 40 degrees, like a door that opens.

⚡ Eye distance

Meaning: A small perspective, such as 300px, means the eye is very close, so the 3D effect is dramatic. A large perspective, such as 2000px, means the eye is far away, so the effect is gentle, almost flat.

⚡ Where the viewer looks from

Guess first: perspective-origin sets where the viewer's eye is. Is it the same as transform-origin?

No. transform-origin is the point the element turns around, not where the viewer stands.

Meaning: perspective-origin sets the point the viewer looks from. The default is the middle, and perspective-origin: left top shows what is on the stage as seen from the top left.

What you will learn