Thursday, June 25, 2026

translateZ() | CSS-Methods


The CSS translateZ() perform provides depth to a component, drawing it nearer or farther in house. In different phrases, it shifts a component alongside the Z-axis in a 3D house.

.field:hover {
  rework: translateZ(100px);
}

.field.perspective:hover {
  rework: perspective(500px) translateZ(100px);
}

Both the perspective() perform or perspective property is important for translateZ() to work. With out both one, there’s no impact.

Activate the swap within the following demo, then hover over the field to see it seem nearer:

Whereas it seems to be just like the .field factor is getting larger” on hover, that’s not what is occurring. If you hover over the field, it really strikes nearer to you a size of 100 pixels, making it seem bigger. We’ll get extra into that in only a bit so it’s extra apparent what’s taking place there.

translateZ() shouldn’t be mistaken for an alternative choice to the scale() perform or scale property. Perspective and scale are two completely different ideas.

The translateZ() perform is outlined within the CSS Remodel Module Degree 2 specification

Syntax

translateZ() = translateZ()

The translateZ() perform takes a single  argument that defines how far the factor is from the entrance of the display screen. It’s used with the rework property

Arguments

/* Optimistic lengths */
rework: translateZ(100px);
rework: translateZ(5rem);

/* Adverse lengths */
rework: translateZ(-50px);
rework: translateZ(-8em);

The translateZ() perform takes a single argument:

  • : the gap of the factor from the entrance of the display screen. When it’s optimistic, the factor strikes nearer to the consumer, and additional away when it’s unfavorable.

The way it works

The translateZ() perform is difficult as a result of it strikes a component alongside the Z-axis, which isn’t visually perceptible in a browser by default. Since browsers solely render parts by their top and width, not their depth, the translateZ() perform could seem to do nothing.

To point out its depth and projection, we have to use both the perspective property or perspective() perform. Moreover, we are able to set transform-style to preserve-3d worth on its guardian, which lets CSS know that little one parts must be positioned in 3D.

Projection and perspective

On an internet site, there isn’t a way of depth — that’s, “closeness” or “furtherness” — since parts are flattened on a 2D display screen. Whether or not it’s translate(200px) or translate(20px), we understand the factor on the similar distance, so there isn’t any perspective in any respect until we explicitly allow it. To point out that, take for instance the next HTML:

Firstly, we’ll allow some perspective in the entire scene:

.scene {
  perspective: 800px;
}

Then, we’ll set transform-style to preserve-3d within the guardian, so youngsters may even be remodeled in 3D:

.guardian {
  transform-style: preserve-3d;
}

Now we transfer the .field 100px nearer to the consumer utilizing translateZ().

.field {
  rework: translateZ(100px);
}

Though it seems to be just like the field grew 100px in measurement, if you rotate the guardian to the aspect, you’ll see that the .field measurement didn’t improve, however it’s the gap between the .guardian and the .field that did.

A tall blue rectangle rotated left along the Y-axis adding perspective.

perspective vs. perspective()

Each do the identical work: outline the factor’s projection. The perspective property is utilized to the guardian for all 3D parts, whereas the perspective() perform can solely be utilized to a single 3D factor, and should be declared earlier than the 3D rework perform.

The perspective property

.guardian {
  perspective: 800px;
}

.child-1 {
  rework: translateZ(200px); /* Outlined inside a projection of 800px */
}

.child-2 {
  rework: translate3D(100px, 200px, 150px); /* Outlined inside a projection of 800px */
}

The perspective() perform

.factor {
  rework: translateZ(100px) perspective(800px); /* Nope ❌ */
}

.factor {
  rework: perspective(800px) translateZ(100px); /* Yep ✅ */
}

It may also be used to optimize internet efficiency

Do you know that you should use the translateZ() perform to spice up our web site’s efficiency? CSS 3D rework capabilities use the GPU, which is quicker and extra superior to the CPU for factor rendering. This efficiency hack prevents flickering throughout animations and makes transitions smoother.

Builders add translateZ(0) to shift rendering from the CPU to the GPU, enhancing efficiency. For those who’re battling a glitching animation, now you’ve s strategy to repair it!

Demo

Specification

The translateZ() perform is outlined within the CSS Remodel Module Degree 2 specification.

Browser assist

The translateZ() perform is obtainable on all trendy browsers.

References

Extra on 3D transforms!

Related Articles

Latest Articles