Skip to content
Tutorials Center
My learning

Topics

Lesson 99 of 139 ยท Transforms and Animation

3D Transforms

Add depth with CSS 3D transforms: perspective, rotateX and rotateY, translateZ, preserve-3d and backface-visibility, then build an accessible flip card.

The 2D transforms you've used so far move elements around a flat page. CSS can also place elements in three-dimensional space. A card can tilt away from you, a panel can swing open like a door, and a tile can flip over to reveal its back.

The page is still flat, of course. The browser just projects 3D shapes onto your screen, the way a drawing on paper can look deep. In this lesson you will learn the extra axis that makes this possible, the perspective that gives it realism, the two properties that control how nested 3D elements behave, and then you'll build a classic flip card.

The z axis

In 2D, you work with two axes: x runs left to right and y runs top to bottom. 3D adds a z axis that points straight out of the screen, toward you. Positive z values come closer; negative values move away.

That gives you new transform functions:

Function What it does
rotateX(angle) Tilts around the horizontal axis, like a garage door
rotateY(angle) Turns around the vertical axis, like a revolving door
rotateZ(angle) Spins flat on the screen; the same as rotate()
translateZ(length) Moves toward or away from the viewer
translate3d(x, y, z) Moves along all three axes at once
scaleZ(n), scale3d(x, y, z) Scales along z (only visible on elements that have depth)
rotate3d(x, y, z, angle) Rotates around any axis you describe with a vector

The individual properties from the previous lesson accept 3D values too: translate: 0 0 50px, rotate: y 45deg or rotate: 1 1 0 60deg.

Perspective makes it look 3D

Try transform: rotateY(50deg) on its own and you'll be disappointed: the element just looks narrower. Without perspective, the browser uses a flat, orthographic projection, so the near edge and the far edge are drawn the same size.

Perspective fixes that by placing a virtual viewer at some distance from the screen. Parts of the element nearer to that viewer are drawn larger, and parts farther away smaller. The value is the distance from the viewer to the z = 0 plane:

  • A small value, like 200px, puts your eye very close. The effect is dramatic and a bit distorted, like a wide-angle lens.
  • A large value, like 1500px, puts your eye far away. The effect is subtle.
  • Values from about 600px to 1200px look natural for most interface elements.

There are two ways to add it.

CSS
/* 1. On the parent: one shared viewpoint for all children */
.scene {
  perspective: 800px;
}

/* 2. As a function: each element gets its own viewpoint */
.card {
  transform: perspective(800px) rotateY(40deg);
}

With the perspective property on a container, all its children share one vanishing point, so a row of tilted cards looks like part of one scene. The perspective() function must come first in the transform list, and each element gets its own vanishing point at its own center.

Perspective property vs perspective() function

In the shared row, the cards on the left and right of the scene are seen from slightly different angles, just like real objects in a row. In the bottom row, every card looks identical.

Moving the viewpoint

perspective-origin sets where the viewer stands, relative to the container. The default is the center, 50% 50%. Setting perspective-origin: top makes you look down on the scene from above; left makes you look from the left side.

Building in 3D: transform-style

By default, each element is flattened into its parent's plane. If a child is rotated in 3D inside a parent that is also rotated in 3D, the child is first drawn flat onto the parent, like a sticker, and then the parent is rotated as a whole.

To build real 3D objects from several faces, you need the children to keep their own position in space. Set this on the parent:

CSS
.cube {
  transform-style: preserve-3d;
}

Now each child lives in the same 3D space as its parent, so rotating the parent rotates the whole object with its faces at their true angles. A few properties force flattening even when you ask for preserve-3d, including overflow other than visible, opacity below 1, filter and clip-path. If your 3D object suddenly looks flat, check for those on the container.

A rotating cube with preserve-3d

Each face follows the same recipe: rotate it to face the right direction, then translateZ half the cube's width outward. Here, order matters exactly as in 2D: rotating first means the translation goes along the face's own, rotated z axis.

backface-visibility

Every element has a front and a back. When you rotate an element more than 90 degrees around x or y, you see its back, which by default looks like the front mirrored. That's sometimes what you want, but for a two-sided card you want each side to disappear when it faces away:

CSS
.side {
  backface-visibility: hidden;
}

Safari still needs the prefixed -webkit-backface-visibility in some situations, so production code often includes both.

Project: a flip card

Now combine everything into a card that flips over to show its back. The structure is a scene with perspective, an inner element that rotates with preserve-3d, and two absolutely positioned sides, with the back pre-rotated by 180 degrees:

CSS
.flip       { perspective: 1000px; }
.flip-inner { transform-style: preserve-3d; transition: transform 600ms; }
.flip.is-flipped .flip-inner { transform: rotateY(180deg); }
.side       { position: absolute; inset: 0; backface-visibility: hidden; }
.side.back  { transform: rotateY(180deg); }

When the inner element rotates 180 degrees, the front turns away (and hides) while the back, which was already turned around, now faces you.

An accessible flip card

A few details make this card friendly for everyone:

  • The flip is triggered by a real <button>, so it works with the keyboard and screen readers, not only with hover. Hover-only flips are unreachable on touch screens.
  • The hidden side gets the inert attribute, so its button can't be focused while it faces away. Without that, a keyboard user could Tab to an invisible button. You can read more about inert in Keyboard Access and Focus.
  • With reduced motion, the transition is removed and the sides swap instantly.

Debugging 3D

3D bugs usually come from one of a handful of causes:

  • Nothing looks 3D: there is no perspective on the parent or in the transform.
  • The object looks flat: transform-style: preserve-3d is missing on an intermediate element, or the container has overflow: hidden, opacity or a filter that forces flattening.
  • The back shows mirrored text: add backface-visibility: hidden to both sides.
  • Faces flicker or cut through each other: two faces sit at exactly the same depth. Nudge one with translateZ(1px).

DevTools in Chromium browsers also has a 3D view panel (under "More tools") that shows layers in space, which can help with tricky scenes.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which direction does the z axis point?

    Positive z values come toward the viewer, which is why translateZ(50px) makes an element look closer.
  2. 2. What happens with a smaller perspective value, such as 200px?

    A small perspective puts the virtual viewer close to the screen, so near parts look much larger than far parts.
  3. 3. What does transform-style: preserve-3d on a parent do?

    Without preserve-3d, children are flattened onto their parent's plane before the parent itself is transformed.
  4. 4. In a flip card, why is the back side given rotateY(180deg) from the start?

    The back starts turned away. When the inner element rotates 180 degrees, the two rotations add up to 360 and the back faces forward.
  5. 5. Which of these can force a preserve-3d container to flatten?

    Properties such as overflow other than visible, opacity below 1 and filter force flattening, so the 3D object loses its depth.

Summary

  • 3D transforms add a z axis: rotateX(), rotateY(), translateZ(), translate3d() and rotate3d().
  • Without perspective, 3D rotations look flat; add perspective to a parent (shared viewpoint) or perspective() first in a transform.
  • perspective-origin moves the viewer's position.
  • transform-style: preserve-3d lets nested elements form real 3D objects; some properties force flattening.
  • backface-visibility: hidden hides the side of an element that faces away, which is the key to flip cards.
  • Trigger flips with buttons, keep hidden sides inert, and swap instantly when reduced motion is requested.

Next: Keyframe Animations โ€” create motion that runs on its own, loops and has many steps.