translateZ()

Term side of the Card.
Topic: Transformations
Unit title: translateZ()
Categories: Function
Explanation side of the Card

Explanation: The function that moves an element along the Z-axis in 3D space. It adjusts the depth of the element, making it appear closer or farther away from the viewer, adding a sense of 3D perspective to the design.

Browser compatibility:
  • The chrome browser supports this feature starting from version 12.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 10.
  • The safari browser supports this feature starting from version 4.
Availability: 

Example

Wrongcss
.card:hover {
  transform: translateZ(50px);
}
Rightcss
.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 perspective is set on the parent or with perspective() when translateZ() should be visible.
  • will-change: transform is used to hint a layer instead of the translateZ(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.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy