3D Transforms: Perspective, preserve-3d and Backfaces
Adding depth with CSS: the 3D transform functions, the perspective property versus the perspective() function, perspective-origin, keeping children in 3D with preserve-3d and what flattens it, backface-visibility, and a flip card.
kvi5hnuCode Sprout team member8 min readOn this page
CSS can place elements in three dimensions: tilt them toward or away from the viewer, push them back along the z-axis, and arrange them into cards, cubes and carousels. The page is still flat, so the illusion of depth comes from perspective, which makes far things smaller.
The 3D functions
The z-axis points toward the viewer. The 3D functions extend the 2D ones from article 103:
| Function | Effect |
|---|---|
rotateX(a), rotateY(a) | tilt around the horizontal or vertical axis |
rotateZ(a) | the same as rotate(a) |
rotate3d(x, y, z, a) | rotate around any axis vector |
translateZ(z), translate3d(x, y, z) | move toward (positive) or away from the viewer |
scaleZ(z), scale3d(x, y, z) | scale z coordinates: visible on 3D children, or on later z translations in the same list |
perspective(d) | apply perspective inside the transform list |
matrix3d() | any 3D transform (article 104) |
The individual properties take 3D values too: translate: 0 0 50px, rotate: y 45deg or rotate: 1 1 0 30deg, and scale: 1 1 2.
Z values must be lengths. Percentages aren't allowed, since an element has no depth to be a percentage of. Tested in Chrome: transform: translateZ(10%) was invalid and computed to none. translate3d() also needs all three values.
Perspective

Without perspective, a 3D rotation looks like a squashed 2D shape: parallel edges stay parallel. Tested in Chrome: a 100 × 100px element with rotateY(45deg) was 71 × 100px without perspective, and 71 × 108px with a perspective: 500px parent, because its near edge was drawn larger.
The value is the distance from the viewer to the z = 0 plane:
- Small values (100 to 300px) are like looking from close up: dramatic distortion.
- Large values (800 to 2000px) are subtle, like looking from far away.
- An element at
translateZ(d)in front of the plane is enlarged by d' ÷ (d' − d), where d' is the perspective. Tested in Chrome: withperspective: 500px, a 100px child movedtranslateZ(100px)was 125px wide (500 ÷ 400). - An element translated to the perspective distance or beyond is behind the viewer and isn't drawn.
The property versus the function
There are two ways to set perspective, and they behave differently:
.scene { perspective: 800px; } /* on the parent: one shared viewpoint */
.card { transform: perspective(800px) rotateY(30deg); } /* on the element: its own viewpoint */
- The
perspectiveproperty goes on the parent and applies to its children. All children share one viewpoint, the vanishing point at the parent's center, so a row of rotated cards looks like one scene. - The
perspective()function goes in the element's own transform, and gives each element its own vanishing point, at its own transform origin (the center by default). It must come first in the list, since it only affects the functions after it. Tested in Chrome:perspective(600px) rotateY(40deg)made a 100px square 106px tall at its near edge, whilerotateY(40deg) perspective(600px)left it flat at 100px.
Use the property for scenes with several elements, and the function for a single element, or when you can't change the parent.

Only the second row looks like three copies of one tile, because each tile is its own scene. Moving perspective-origin changes where the viewer stands, and so how much of each tile they see.
perspective-origin
perspective-origin moves the viewpoint, the vanishing point, on the parent that has the perspective property. It takes positions like background-position, and defaults to the center: tested in Chrome, a 100px square computed 50px 50px. perspective-origin: top is like looking down on the scene from above. It has no effect without perspective.
transform-style: preserve-3d
By default, an element with 3D-transformed children is flattened: its children are drawn into its own plane, like a picture on a card, and rotating it rotates the picture. With transform-style: preserve-3d, the children keep their own positions in 3D space, so a parent can be the container for a cube or a flip card whose faces are positioned in depth:
.cube { transform-style: preserve-3d; rotate: 1 1 0 30deg; }
.cube > .front { translate: 0 0 50px; }
.cube > .back { transform: rotateY(180deg) translateZ(50px); }
preserve-3d must be set on every level that should stay 3D; it doesn't inherit.
Some properties force flattening, because they need to draw the element and its children as one flat image first. Tested in Chrome, with a child at translateZ(100px) under a perspective: 500px scene, the child was 125px wide with preserve-3d alone, and 100px, flattened, when the preserve-3d element also had any of:
overflowother thanvisible, includingclip;opacitybelow 1;filter;clip-path;isolation: isolate;mix-blend-modeother thannormal.
mask-image and backdrop-filter flattened it too.
The spec's list differs slightly from what Chrome does: it exempts overflow: clip and includes paint containment (contain: paint), while Chrome flattened with overflow: clip and not with contain: paint. Avoid both on preserve-3d containers to be safe. The computed value of transform-style stays preserve-3d even when it's flattened, so check visually. Apply opacity or filters to the children instead.
backface-visibility
The back of an element is drawn as its mirror image by default, so text shows reversed when it's rotated past 90°. backface-visibility: hidden hides an element while its back faces the viewer. That's what makes a two-sided flip card:
<div class="flip">
<div class="inner">
<div class="face front">Front</div>
<div class="face back">Back</div>
</div>
</div>
.flip { perspective: 900px; }
.inner { display: grid; transform-style: preserve-3d; transition: rotate 0.6s; }
.flip.flipped .inner { rotate: y 180deg; }
.face { grid-area: 1 / 1; backface-visibility: hidden; } /* stack both faces */
.back { rotate: y 180deg; } /* starts facing away */
Both faces occupy the same grid cell. The back starts turned around and hidden; when .inner turns 180°, the front faces away and the back faces the viewer.

The first and third panels look the same for a reason: opacity below 1 needs the children drawn as one flat image, which undoes preserve-3d.
Accessibility and practicalities
- Large 3D rotations and zooms can cause motion sickness. Reduce or replace them under
prefers-reduced-motion: reduce(article 112), for example with a crossfade. - A flipped card's hidden face is still in the accessibility tree, and its links are still focusable. Use
inerton the hidden face, orvisibility: hiddentoggled at the end of the transition, so keyboard users don't tab into invisible content. - Trigger flips with a
<button>, not hover alone, so touch and keyboard users can flip it. - 3D transforms can blur text during animation in some browsers; text usually renders sharply again when the element is at rest.
body { font: 0.8rem system-ui, sans-serif; margin: 1rem; }
label { display: flex; align-items: center; gap: 0.3rem; margin-block-end: 0.3rem; }
#p { inline-size: 8rem; }
.row {
display: flex; gap: 0.5rem; justify-content: center; margin: 0.8rem 0;
perspective: var(--p, 500px); /* one shared viewpoint for all tiles */
}
.tile {
inline-size: 4.5rem; block-size: 3.5rem; display: grid; place-items: center;
font: 800 1.2rem system-ui, sans-serif; color: white; background: #1d3557; border-radius: 6px;
rotate: y 45deg;
}
.flip { perspective: var(--p, 500px); inline-size: 12rem; margin: 0 auto 0.6rem; }
.inner { display: grid; transform-style: preserve-3d; transition: rotate 0.6s; }
.flip.flipped .inner { rotate: y 180deg; }
.face {
grid-area: 1 / 1; block-size: 6rem; display: grid; place-content: center; text-align: center;
border-radius: 10px; font: 700 1rem system-ui, sans-serif; color: white;
}
.flip.hide-back .face { backface-visibility: hidden; }
.front { background: #8a3b12; }
.back { background: #2f7d32; rotate: y 180deg; }
button { font: inherit; padding: 0.3rem 0.8rem; display: block; margin: 0 auto; }
@media (prefers-reduced-motion: reduce) { .inner { transition-duration: 0.01s; } }
The three tiles share one vanishing point, so each is angled differently relative to the viewer; lower the perspective for a stronger effect. Flip the card, then uncheck backface-visibility and flip again: the front's mirrored back shows through during the turn.
What's current
- 3D transforms,
perspective,transform-styleandbackface-visibility: all browsers, unprefixed. Safari dropped the need for-webkit-backface-visibilityin version 15.4. - The individual
rotateandtranslateproperties accept 3D values in all browsers.
Common questions
Why does my 3D rotation look flat?
There's no perspective. Add perspective on the parent, or perspective() first in the transform.
Why do my cube faces collapse into one plane?
The container lacks transform-style: preserve-3d, or it has overflow, opacity, filter or another property that forces flattening.
Property or function? The property on a parent for scenes with several elements; the function for one element.
Why can I see mirrored text on the back of my card?
Backfaces are visible by default. Set backface-visibility: hidden on the faces.
Summary
- The z-axis points toward the viewer; z values must be lengths.
perspectiveon a parent gives its children one viewpoint;perspective()first in a transform gives an element its own.- Smaller perspective means stronger distortion;
perspective-originmoves the vanishing point. transform-style: preserve-3dkeeps children in 3D;overflow,opacity,filter,clip-path,isolationand blend modes flatten it.backface-visibility: hiddenand a 180° rotation make two-sided cards; keep the hidden face out of the tab order.
Practice questions
Three images in a row should all tilt back 30° around the x-axis and look like one scene. Write the CSS.
Answer
.gallery { perspective: 1000px; }
.gallery img { rotate: x 30deg; }
The perspective property on the parent gives all three one shared vanishing point.
Why does this element look flat?
.card { transform: rotateY(40deg) perspective(600px); }
Answer
perspective() only affects the functions after it, and nothing follows it. Put it first: transform: perspective(600px) rotateY(40deg).
The faces of a 3D cube appear flattened after a designer adds opacity: 0.9 to the cube container. Why, and how do you fix it?
Answer
opacity below 1 forces transform-style: preserve-3d to flatten, because the container has to be drawn as one image to make it translucent. Apply the opacity to each face instead.
How far is a child with translateZ(200px) enlarged, inside a parent with perspective: 400px?
Answer
Twice its size: 400 ÷ (400 − 200) = 2. Tested in Chrome, a 100px child was drawn 200px wide.
Review quiz
Test yourself on Part 15 with the CSS Part 15 review quiz.
Previous: Transform Origin, Order of Transforms and the Matrix Functions

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.