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>
.stageis the parent, the stage the card stands on.perspective: 800pxon.stagemeans the viewer's eye is 800px in front of the stage..cardis the child that turns.transform: rotateY(40deg)turns it 40 degrees, like a door that opens.perspectiveworks on the children of the element that has it, not on that element itself. Soperspectivegoes on the parent, andtransformon the child.
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:
rotateX(45deg)turns the element around a horizontal line, like a laptop lid that opens or closes.rotateY(45deg)turns the element around a vertical line, like a door, or a card flipped sideways.rotateZ(45deg)turns the element around a line that points out of the screen towards you, like a clock hand. The result is the same as plainrotate(), so the element always looks flat, even withperspective.
For two turns at once, write both in one transform, separated by a space: transform: rotateX(20deg) rotateY(30deg);
Eye distance and viewpoint
- A small
perspective, such as300px, means the eye is very close, so the 3D effect is dramatic. - A large
perspective, such as2000px, means the eye is far away, so the effect is gentle, almost flat. perspective-originsets the point the viewer looks from. The default is the middle.perspective-origin: left topshows what is on the stage as seen from the top left.
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); }
.card-containergets a size (widthandheight), and.cardfills its whole height (height: 100%).perspective: 1000pxon.card-containergives the depth.transform-style: preserve-3don.cardkeeps both faces in 3D space with the card. Without this property, both faces are pressed into one flat picture.backface-visibility: hiddenon both faces hides the face that has its back to you..card-backis turned around from the start (rotateY(180deg)), so the back face is turned towards you after the card flips..cardhasposition: relative, so it is the reference point for both faces.position: absoluteandinset: 0(a distance of 0 from all four edges) then stack both faces so they cover.cardexactly.- While the cursor is over
.card-container(:hover),.cardturns 180 degrees.transitionmakes that turn take 0.6 seconds.
⚠️ What stops a 3D card from flipping correctly
- Two
transformlines in one rule. Fromtransform: rotateX(45deg); transform: rotateY(45deg);the browser uses only the last line. Put both functions in onetransform. overflow: hiddenon apreserve-3delement. Anyoverflowvalue other thanvisiblemakes the content of that element flat again.- Faces that aren't stacked. Without
position: absolute, the back face appears below the front face, not behind it. - A card with no size. A face with
position: absolutedoesn't count towards its parent's height. Withoutwidthandheighton.card-container, the card is 0 pixels tall, and the text after it is drawn on top of the card.
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.