π Introduction
The transform-style property in CSS controls how nested (child) elements behave in 3D space when the parent element is transformed. It determines whether children are rendered as flat (merged into the plane of the parent) or preserved in their own 3D space. This property is essential for creating advanced 3D UI effects like flipping cards, rotating cubes, and layered parallax. β¨
π― What Does transform-style Do?
- Defines 3D rendering behavior for child elements
- Controls whether children stay flat or keep 3D depth
- Used with
transform: rotateX(),rotateY(), and 3D scenes - Works only on elements that are parents of transformed children
β¨ Syntax
Syntax
selector {
transform-style: flat | preserve-3d;
}Note
π§© Available Values
1. flat (default)
Children are flattened into the plane of the parent. 3D transforms applied to child elements will NOT appear in real 3D.
flat
transform-style: flat;2. preserve-3d
Children maintain their 3D position relative to the parent. Essential for realistic 3D effects like rotating cubes or flip cards.
preserve-3d
transform-style: preserve-3d;π₯ Practical Examples
1. 3D Flip Card (requires preserve-3d)
Flip Card
.card {
width: 200px;
height: 300px;
perspective: 1000px;
}
.card-inner {
width: 100%;
height: 100%;
transform-style: preserve-3d; /* important */
transition: transform 0.6s;
}
.card:hover .card-inner {
transform: rotateY(180deg);
}
.front, .back {
position: absolute;
backface-visibility: hidden;
}
.back {
transform: rotateY(180deg);
}2. 3D Cube Example
Cube
.cube {
width: 200px;
height: 200px;
position: relative;
transform-style: preserve-3d;
}
.cube div {
position: absolute;
width: 100%;
height: 100%;
}3. Flat Mode Removes 3D Effect
Flat Example
.flat {
transform-style: flat; /* children lose 3D depth */
}π Comparison Table
| Value | Behavior | Use Case |
|---|---|---|
| flat | Children appear 2D even if transformed | Normal UI (no 3D needed) |
| preserve-3d | Children maintain 3D effects | Flipping cards, cubes, parallax |
π§ Important Notes
- Requires perspective on a parent to visualize 3D depth
- Must be paired with child transforms like rotateX/rotateY
- backface-visibility is often needed for clean 3D flipping
- Not all browsers support complex 3D without GPU acceleration
π Conclusion
The transform-style property is essential for controlling 3D rendering of child elements. Use flat for normal UI and preserve-3d when building advanced 3D animations like flip cards or cubes. Mastering this property opens the door to beautiful, immersive 3D interfaces. π¨β¨