Skip to content
tezvyn:

CSS transform-style: Let Children Live in 3D Space

Source: developer.mozilla.orgHardHow cards are made

CSS transform-style: Let Children Live in 3D Space

The transform-style property decides if an element's children are flattened onto its 2D plane or share a 3D space. Use preserve-3d to create nested 3D objects. Beware: properties like opacity < 1 or overflow will break the 3D context and flatten it.

Why it exists

Without a shared 3D space, every element with a 3D transform would exist in its own isolated world, making it impossible to build complex objects from multiple parts. transform-style was created to allow multiple elements to share a single, unified 3D coordinate system, enabling the construction of composite 3D scenes.

The mental model

Think of transform-style as setting the stage for a play. With flat (the default), each actor performs on their own tiny, separate platform, and their 3D movements are squashed flat before being shown to the audience. With preserve-3d, you build one large stage where all actors can move around and interact with each other in a shared three-dimensional space. The parent element becomes the stage itself.

How it works

The property has two values. flat renders an element's children onto its own 2D plane before applying its own transforms. preserve-3d tells the browser that the children should be positioned in a 3D space defined by the parent. This creates a new stacking context and a shared perspective for all children. Critically, this property is not inherited; you must apply it to every element that acts as a container for other 3D-transformed elements in your scene.

When to use it

Use transform-style: preserve-3d on a container element when you want its children to be transformed relative to each other in a shared 3D space. This is the key to building composite 3D objects, such as a cube made of six div faces or a card that flips over to reveal a back side. You are essentially creating a mini-scene.

When not to use it

Avoid using preserve-3d on elements that also need properties that require flattening. The browser will force transform-style to flat if the element has opacity less than 1, a filter, clip-path, mask-image, or an overflow value other than visible. This is the most common source of bugs with 3D transforms; your scene will unexpectedly collapse into a 2D plane. If you need these properties, apply them to a wrapper element outside the 3D scene hierarchy.

One canonical example

To create a 3D cube, you need a container element and six child elements for the faces. The container must have transform-style: preserve-3d. Each face is then translated and rotated into its correct position in 3D space. Without preserve-3d on the container, the faces would not form a cube; they would each be transformed in isolation and then flattened onto the container's plane.

Interview question

A developer applies transform-style: preserve-3d to a container, but its children still appear flattened. What is the most likely reason?

  • a.The transform-style property is not inherited by the child elements.
  • b.The container element has an opacity value less than 1.Correct
  • c.The perspective property is missing from the container element.
  • d.The child elements do not have individual transform properties applied.
Why?

The card states that properties like opacity less than 1 on the container element will force transform-style to flat, breaking the 3D context. While perspective is crucial for 3D appearance, its absence does not force flattening of the 3D context itself.

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

Read the original → developer.mozilla.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