3D Animations with CSS — Web Animation

CSS 3D turns flat elements in three-dimensional space. The perspective property (the eye's distance from the screen) on the parent = one eye for all the…

The room card on Mrs. Sri's front desk has two sides: the front is the "Morning terrace" photo, the back is the list of facilities. You can flip it over with your hand. How "dramatic" it looks depends on how far your eye is from the card. Close up, its edges look huge. Far away, the card is almost flat. In CSS, this eye distance is set with perspective. The two sides of the card are glued back to back, and you only see the side facing you. In CSS, the side facing away from the eye is hidden with backface-visibility. CSS 3D imitates this card: flat elements are turned in three-dimensional space, then drawn as an eye would see them from a certain distance.

perspective: the eye's distance from the screen

Here are the two basic rules. The parent .scene sets the eye distance, and its child .flip is turned with rotateY(60deg), which is 60° around the upright axis, like a door opening:

.scene { perspective: 800px; perspective-origin: 50% 50%; } /* the eye 800 px in front of the screen, centred */
.flip  { transform: rotateY(60deg); }                       /* its children turn in 3D space */

A card you can flip

Mrs. Sri's card is made of .scene (the parent that sets the eye distance), .flip (the part that turns) and two .face elements (the front and back sides). The "See facilities" button flips it:

<div class="scene">
  <div class="flip">
    <div class="face front">Morning terrace</div>
    <div class="face back">Wi-Fi · Breakfast · Hot water · Motorbike parking</div>
  </div>
</div>
<button type="button" class="turn" aria-pressed="false">See facilities</button>

The CSS:

.scene { perspective: 800px; }
.flip {
  position: relative; height: 200px;
  transform-style: preserve-3d;              /* its children live in the same 3D space */
  transition: transform 400ms ease-in-out;
}
.flip.flipped { transform: rotateY(180deg); }
.face { position: absolute; inset: 0; border-radius: 12px; overflow: hidden;
        backface-visibility: hidden; }       /* the side turned away from the eye is hidden */
.back { transform: rotateY(180deg); }        /* back to back with the front */
const button = document.querySelector('.turn');
const flip = document.querySelector('.flip');
const front = flip.querySelector('.front');
const back = flip.querySelector('.back');
back.inert = true;                                   // it starts facing away

button.addEventListener('click', () => {
  const flipped = flip.classList.toggle('flipped');
  button.setAttribute('aria-pressed', flipped);
  front.inert = flipped;                             // the side facing away: inert
  back.inert = !flipped;
});

classList.toggle('flipped') adds or removes the class flipped, then gives true if the class is now on.

The maths of rotateY

Without perspective, a point x away from the rotation axis shows at x × cos θ. θ (theta) is the angle of the turn. cos θ (the cosine) is 1 at 0°, then gets smaller until it is -1 at 180°. For a 320 px card, each edge is 160 px from the axis:

Angle cos θ Visible width
0° 1 320 px
60° 0.5 160 px
90° 0 0 px: the card becomes a line, as the front changes to the back
180° -1 320 px again, mirrored

Now take a guess: the same Terrace Room card, 320 px wide, is turned rotateY(60deg) inside a parent with perspective: 800px. Does it look wider than 160 px, narrower, or exactly that?

With perspective d (the eye distance), a point that comes z px toward the eye looks bigger: its distance from the centre is multiplied by d / (d - z). At 60°, one edge comes 160 × sin 60° = 138.6 px toward the eye, and the other edge goes back as far. Without perspective, each edge shows 80 px from the centre (160 × cos 60°). With perspective:

The result is 165 px, a little wider than 160 px, because the near edge grows more than the far edge shrinks. getBoundingClientRect() (the size of the element's box on screen) gives 165 px in Chromium 147, Firefox 146 and WebKit 26.4.

The trap: preserve-3d quietly going flat

The CSS Transforms 2 spec (the standard document for transforms) lists values that force the browser to draw an element's children as one flat picture first. When an element with preserve-3d has one of these values, preserve-3d on that element is used as flat (drawn flat). The values:

We put a price tag with translateZ(60px) (60 px toward the eye) inside the card. Normally that 100 px tag looks 108.1 px wide (800 / 740). With overflow: hidden, filter: drop-shadow(...) or opacity: 0.99 on .flip, it is 100 px in all three engines: the depth is gone. A card flipped 180° even shows its front, mirrored, instead of the facilities. The trap: getComputedStyle (the CSS values the browser calculated) still says preserve-3d, because only the used value changed (the value really used for drawing). Put overflow, rounded corners and shadows on .face or .scene, never on .flip.

Performance and "Reduce motion"

@media (prefers-reduced-motion: reduce) {
  .flip, .flip.flipped { transform: none; transition: none; }
  .back { transform: none; opacity: 0; }
  .face { transition: opacity 150ms; }
  .flip.flipped .front { opacity: 0; }
  .flip.flipped .back { opacity: 1; }
}

The exercise: building transform strings

In the exercise, you complete three functions that build and read transform text. Here is one way to write them:

const UNITS = { perspective: 'px', translateX: 'px', translateY: 'px', translateZ: 'px',
                rotateX: 'deg', rotateY: 'deg', rotateZ: 'deg', scale: '' };

function buildTransform(transforms) {
  return transforms.map((t) => t.type + '(' + t.value + (UNITS[t.type] ?? '') + ')').join(' ');
}
function calculateFlipState(isFlipped, axis) {
  return 'rotate' + axis + '(' + (isFlipped ? 180 : 0) + 'deg)';
}
function parseTransformDeg(text, type) {
  const match = text.match(new RegExp(type + '\\((-?[\\d.]+)deg\\)'));
  return match ? Number(match[1]) : 0;
}

⚠️ Why is the card flat or mirrored?

TL;DR: CSS 3D turns flat elements in three-dimensional space. The perspective property (the eye's distance from the screen) on the parent = one eye for all the children (one vanishing point: the far point where the lines of depth meet; perspective-origin moves the eye). The perspective() function at the start of transform = its own eye for each element. transform-style: preserve-3d keeps the children in the parent's 3D space. On a two-sided card, backface-visibility: hidden on both sides hides the side facing away. The back gets rotateY(180deg) (a 180° turn around the upright axis), and the card flips with rotateY(180deg) from a button, in 400 ms. Visible width = width × cos θ (θ = the angle of the turn; a 320 px card: 160 px at 60°, a line at 90°). With perspective d, the distance from the centre of a point that comes z toward the eye is multiplied by d / (d - z), so a 320 px card at 60° with 800px looks 165 px. overflow (other than visible/clip), opacity < 1, filter, clip-path, mask, isolation, mix-blend-mode and contain: paint force preserve-3d flat (a translateZ(60px) tag drops from 108.1 px to 100 px, a card at 180° shows its front mirrored), even though getComputedStyle still says preserve-3d. When "Reduce motion" is on, swap the rotation for a 150 ms crossfade (one side fades out as the other fades in). In the exercise: perspective(1000px) rotateY(30deg), rotateX(0deg), and numbers read back with the regex (a text search pattern) -?[\d.]+.

⚡ The eye's distance from the screen

Write:

.scene { perspective: 800px; perspective-origin: 50% 50%; } /* the eye 800 px in front of the screen, centred */
.flip  { transform: rotateY(60deg); }                       /* its children turn in 3D space */

Meaning: perspective: 800px means the eye is 800 px in front of the screen. This property goes on the parent (.scene), so all its children are seen by one eye and share one vanishing point (a far point where the lines of depth meet). rotateY(60deg) turns .flip 60° around the upright axis. The smaller the perspective number, the closer the eye and the stronger the distortion.

⚡ A card you can flip

Write:

.flip {
  position: relative; height: 200px;
  transform-style: preserve-3d;              /* its children live in the same 3D space */
  transition: transform 400ms ease-in-out;
}
.flip.flipped { transform: rotateY(180deg); }
.face { position: absolute; inset: 0; border-radius: 12px; overflow: hidden;
        backface-visibility: hidden; }       /* the side turned away from the eye is hidden */
.back { transform: rotateY(180deg); }        /* back to back with the front */

Meaning: .flip holds the two sides of the card (.face). The class flipped turns .flip 180°, and .back is turned 180° from the start, so the two sides are back to back. transform-style: preserve-3d on .flip makes both sides render in the same 3D space. backface-visibility: hidden goes on both sides, so the side facing away from the eye can't be seen.

⚡ A little wider

Guess first: without perspective, a 320 px card turned rotateY(60deg) looks 160 px wide. Inside a parent with perspective: 800px, does it look wider than 160 px, narrower, or exactly that?

A little wider, 165 px. The near edge grows more than the far edge shrinks.

Meaning: Without perspective, the visible width = width × cos θ (θ = the angle of the turn): 160 px at 60°, and only a line at 90°.

⚡ preserve-3d quietly going flat

Meaning: overflow: hidden, opacity below 1, or a filter on .flip force preserve-3d flat: the depth is gone, and a flipped card shows its front, mirrored. Yet getComputedStyle (the CSS values the browser calculated) still says preserve-3d. Put overflow, rounded corners and shadows on .face or .scene, never on the element that turns.

What you will learn