How Does CSS transform-style preserve-3d Work?
The transform-style: preserve-3d CSS property
establishes a shared three-dimensional rendering context, allowing
nested elements to retain their individual 3D positions rather than
flattening into a single 2D plane. By default, browsers collapse
transformed child elements into the plane of their parent using
transform-style: flat. Applying preserve-3d
overrides this behavior, enabling complex spatial hierarchies such as 3D
cubes, rotating cards, and layered parallax scenes where elements can
intersect and interact across the X, Y, and Z axes.
The Default 2D Flattening Context
In standard CSS rendering, the browser treats the DOM as a series of
2D stacking layers. When you apply 3D transformations like
rotateY() or translateZ() to child elements,
the browser computes their 3D orientation, flattens the result into the
2D surface of the immediate parent container, and then applies the
parent's own transforms.
This flattening destroys genuine depth. If a parent is rotated, all
nested children are rotated as a single, flat texture. The default value
responsible for this behavior is transform-style: flat.
Creating a True 3D Coordinate Space
When you declare transform-style: preserve-3d on a
parent container, the browser creates a persistent 3D coordinate system
for all of its direct children.
Instead of flattening each layer sequentially:
- Shared Depth: Direct children position themselves
along the Z-axis relative to the parent’s origin, allowing elements
behind other elements to be naturally obscured according to 3D depth
rather than standard
z-index. - Spatial Intersection: Elements can physically pass through one another in virtual space when rotated or translated along intersecting planes.
- Cumulative Transforms: Transformations applied to the parent cascade naturally through the 3D space, moving children while preserving their relative spatial offsets.
Key Prerequisites for 3D Rendering
For transform-style: preserve-3d to function correctly,
it must work in tandem with specific properties across the DOM tree:
1. Defining Perspective
The preserve-3d property does not generate depth
perspective by itself; it only preserves spatial relationships. A
higher-level ancestor must define the viewing distance using the
perspective property (e.g.,
perspective: 1000px). Without perspective, 3D
transformations render using isometric projection, which lacks realistic
vanishing points.
2. Multi-Level Propagation
The transform-style property is not automatically
inherited by grandchildren. If you build a deeply nested structure (such
as a 3D scene containing objects made of multiple 3D faces), every
intermediate container between the root perspective element and the
final transformed child must have
transform-style: preserve-3d explicitly declared.
Common Property Conflicts
Certain CSS properties force the browser to establish a new 2D
clipping or grouping context, which immediately breaks
preserve-3d and flattens the 3D hierarchy:
overflow: hidden,overflow: scroll, oroverflow: autofilterproperties (such asblur()ordrop-shadow())clip-pathormaskmix-blend-modevalues other thannormalopacityvalues less than 1 (in some rendering engines)
When creating complex 3D interfaces, avoid placing these properties
directly on containers designated as preserve-3d
elements.