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 */
- The
perspectiveproperty goes on the parent. All its children are seen by the same eye, so they share one vanishing point (a far point where the lines of depth seem to meet).perspective-originmoves that eye. Its default value is50% 50%, the parent's centre. - The
perspective()function goes on the element itself, at the start of its transform list:transform: perspective(800px) rotateY(60deg). Each element gets its own eye and its own vanishing point. - For one card, the two give the same result (we measured 165 px for both; the calculation is below). For three cards in a row, the
perspectiveproperty tilts all three toward one point, as one person would see them. Theperspective()function makes each card look as if seen from straight in front of it. - The smaller the number, the closer the eye, and the stronger the distortion (the change of shape caused by depth).
perspective: none= no depth, everything looks flat.
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 */
transform-style: preserve-3don the parent (.flip): its children live in the same 3D space as the parent. Withoutpreserve-3d, the children are drawn flat (flattened) onto the parent's surface, so the back side turned 180° is never really behind.backface-visibility: hiddenon both sides: a side whose back faces the eye is hidden. At 0° you see the front. Past 90°, the front turns its back to the eye and disappears, and the back appears. Without this rule, the back side covers the front even at 0°, mirrored, because the back side comes later in the DOM. We checked withelementFromPoint(a function that gives the top element at one point of the screen) in all three browser engines.- The card is flipped with a button, not just
:hover, so it works on phones and with a keyboard. Thearia-pressedattribute tells a screen reader whether the button is switched on. The duration is 400 ms (the Material 3medium4token), because a full-width card turns a long way. backface-visibilityonly hides the picture. In Chromium 156, a screen reader still reads the text of the side facing away, and a link inside it (if there is one) can still be reached with Tab. Putinerton that side. Aninertelement and its content can't be focused, clicked or read by a screen reader (Chrome 102, Firefox 112, Safari 15.5). The button can flip the card and setinertat the same time:
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:
- near edge: 80 × 800 / (800 - 138.6) = 80 × 1.21 = 96.8 px
- far edge: 80 × 800 / (800 + 138.6) = 80 × 0.85 = 68.2 px
- total: 165 px
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:
overflowother thanvisibleorclip,opacitybelow 1,filterother thannone,clip-pathandclip,isolation: isolate,mask-imageandmask-border-source,mix-blend-modeother thannormal,contain: paint(includingcontent-visibility: hidden).
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"
rotateYis a transform, so its transition can run on the compositor, like a 2D transform (the Transform & Composite lesson). Each 3D element usually becomes its own layer. Three cards are fine, but hundreds of 3D cards use up GPU memory.- A big rotation is movement that can trigger dizziness. When "Reduce motion" is on, swap the rotation for a 150 ms crossfade (the front fades out while the back fades in):
@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;
}
buildTransform([{ type: 'perspective', value: 1000 }, { type: 'rotateY', value: 30 }])=perspective(1000px) rotateY(30deg): distances takepx, angles takedeg, and everything is joined with spaces.calculateFlipState(false, 'X')=rotateX(0deg): zero is still written in full.parseTransformDeg('perspective(800px) rotateY(90deg)', 'rotateY')=90. The regex (a text search pattern) finds that angle number inside a longer text, and-?keeps the minus sign (-30).
⚠️ Why is the card flat or mirrored?
- Taking
transform: perspective()on the parent for theperspectiveproperty. The function works on the parent itself. Its children get no shared vanishing point. - Forgetting
preserve-3d, or switching it off without noticing throughoverflow: hidden,filteroropacityon the turning element. backface-visibilityonly on the back side. Put it on both sides. Otherwise the side facing away still draws, mirrored.- The wrong units in the exercise. In the exercise,
translateZandperspectivetakepx,rotate*takesdeg. Negative numbers and 0 are written as they are.
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.