Skip to content
tezvyn:

CSS 3D Transforms: Adding Depth to the DOM

Source: w3.orgMediumHow cards are made

CSS 3D Transforms: Adding Depth to the DOM

CSS 3D transforms treat flat elements like objects in a 3D space, adding depth with properties like rotateY and translateZ. Use them for card-flip animations or product carousels. The footgun: forgetting to set perspective on a parent makes 3D look flat.

Why it exists

Web interfaces were traditionally flat. CSS 3D Transforms were introduced to allow developers to create more dynamic, engaging, and spatially intuitive UIs without resorting to complex JavaScript libraries or WebGL. They provide a declarative way to add depth and perspective directly in CSS.

The mental model

Imagine your webpage is a stage, and every HTML element is a flat cardboard cutout. CSS 3D transforms give you the tools to move these cutouts forward and backward (translateZ), rotate them in 3D space (rotateX, rotateY), and scale them. The perspective property is like setting up a camera; it defines how "deep" the stage appears to the viewer. Without it, all 3D movement looks like a flat 2D skew.

How it works

You apply 3D transforms using the transform property with functions like rotateX(angle), rotateY(angle), or translateZ(distance). For these to render correctly, a parent element must establish a 3D rendering context. This is done by setting the perspective property on the parent, which defines the distance from the viewer to the Z=0 plane. Another key property is transform-style: preserve-3d on a transformed element, which tells its children to be positioned in the same 3D space, rather than being flattened into their parent's plane.

When to use it

Use 3D transforms for UI elements that benefit from a sense of physical space. Good examples include card-flip animations (showing the front and back of an element), cover-flow style image galleries, creating parallax scrolling effects, or adding subtle "lifting" effects on hover to draw attention.

When not to use it

Avoid using 3D transforms for entire page layouts or for critical content that must remain perfectly legible. Complex 3D scenes can be computationally expensive, leading to performance issues on less powerful devices. They can also introduce accessibility challenges if not handled carefully, as text can become distorted. It is not a replacement for WebGL for building true 3D games or models.

One canonical example

A classic card-flip effect. You have a container with two children: a front face and a back face. The back face is initially rotated 180 degrees (transform: rotateY(180deg)) and hidden with backface-visibility: hidden. On hover, you rotate the container itself by 180 degrees. This spins the front face away and brings the back face into view, creating a seamless flip. The container's parent must have a perspective value set for the effect to look 3D.

Interview question

A developer applies `transform: rotateY(45deg);` to an element, but it still appears to skew in 2D rather than rotate in 3D. What is the most likely missing CSS property?

  • a.backface-visibility: hidden on the transformed element
  • b.perspective on a parent elementCorrect
  • c.A specific z-index value on the transformed element
  • d.transform-style: preserve-3d on the transformed element
Why?

The card states that forgetting to set `perspective` on a parent makes 3D movement look like a flat 2D skew. Without `perspective`, the browser cannot establish the necessary depth for true 3D rendering. `transform-style: preserve-3d` is for children to participate in the parent's 3D space, not for the parent's initial 3D appearance.

Just read this? Test yourself on what you have been reading.

Read the original → w3.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.

See open roles