translateZ()
Example
.card:hover {
transform: translateZ(50px);
}.scene {
perspective: 800px;
}
.card:hover {
transform: translateZ(50px);
}With perspective on the parent, moving along the Z-axis makes the card look closer instead of having no visible effect.
Common mistakes
- Using
translateZ()without any perspective and seeing no change. - Adding
translateZ(0)to many elements, wasting memory on extra layers.
Key points
- A
perspectiveis set on the parent or withperspective()whentranslateZ()should be visible. will-change: transformis used to hint a layer instead of thetranslateZ(0)hack.translateZ()gets a length, never a percentage.
Interview questions
Why do people use translateZ(0)?
It was a hack to push an element onto its own compositor layer. will-change: transform expresses the same intent more clearly.
Can translateZ() take a percentage?
No. It accepts only a length, because an element has no depth to take a percentage of.