translateZ()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
ThetranslateZ()CSSfunction repositions an element along the z-axis in 3D space, i.e.,closer to or farther away from the viewer. Its result is a<transform-function> data type.
In this article
Try it
transform: translateZ(0);transform: translateZ(42px);transform: translateZ(-9.7rem);transform: translateZ(-3ch);<section> <div> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> </div></section>#default-example { background: linear-gradient(skyblue, khaki); perspective: 800px; perspective-origin: 150% 150%;}#example-element { width: 100px; height: 100px; perspective: 550px; transform-style: preserve-3d;}.face { display: flex; align-items: center; justify-content: center; width: 100%; height: 100%; position: absolute; backface-visibility: inherit; font-size: 60px; color: white;}.front { background: rgb(90 90 90 / 0.7); transform: translateZ(50px);}.back { background: rgb(0 210 0 / 0.7); transform: rotateY(180deg) translateZ(50px);}.right { background: rgb(210 0 0 / 0.7); transform: rotateY(90deg) translateZ(50px);}.left { background: rgb(0 0 210 / 0.7); transform: rotateY(-90deg) translateZ(50px);}.top { background: rgb(210 210 0 / 0.7); transform: rotateX(90deg) translateZ(50px);}.bottom { background: rgb(210 0 210 / 0.7); transform: rotateX(-90deg) translateZ(50px);}This transformation is defined by a<length> which specifies how far inward or outward theelement or elements move.
In the above interactive examples,perspective: 550px; (tocreate a 3D space) andtransform-style: preserve-3d;(so the children, the 6 sides of the cube, are also positioned in the 3D space), have been set on the cube.
Note:translateZ(tz) is equivalent totranslate3d(0, 0, tz).
Syntax
translateZ(tz)Values
tzA
<length>representing the z-component of the translating vector [0, 0, tz]. InCartesian coordinate system it represents shift along z-axis. A positive value moves theelement towards the viewer, and a negative value farther away.
| Cartesian coordinates onℝ^2 | Homogeneous coordinates onℝℙ^2 | Cartesian coordinates onℝ^3 | Homogeneous coordinates onℝℙ^3 |
|---|---|---|---|
| This transformation applies to the 3D space and can't be represented on the plane. | A translation is not a linear transformation in ℝ^3 and can't be represented using a Cartesian-coordinate matrix. | ||
Formal syntax
<translateZ()> =
translateZ(<length>)
Examples
In this example, two boxes are created. One is positioned normally on the page, without being translated at all. Thesecond is altered by applying perspective to create a 3D space, then moved towards the user.
HTML
<div>Static</div><div>Moved</div>CSS
div { position: relative; width: 60px; height: 60px; left: 100px; background-color: skyblue;}.moved { transform: perspective(500px) translateZ(200px); background-color: pink;}What really matters here is the class "moved"; let's take a look at what it does. First, theperspective() function positions theviewer relative to the plane that lies where z=0 (in essence, the surface of the screen). A value of500px means the user is 500 pixels "in front of" the imagery located at z=0.
Then, thetranslateZ() function moves the element 200 pixels "outward" from the screen, toward the user.This has the effect of making the element appear larger when viewed on a 2D display, or closer when viewed using a VRheadset or other 3D display device.
Note if theperspective() value is less than thetranslateZ() value, such astransform: perspective(200px) translateZ(300px); the transformed element will not be visible as it isfurther than the user's viewport. The smaller the difference between the perspective and translateZ values, the closerthe user is to the element and the larger the translated element will seem.
Note:As the composition of transforms isn't commutative, the order you write the different functions is significant. In particular, in general, you wantperspective() to be placed beforetranslateZ().
Result
Specifications
| Specification |
|---|
| CSS Transforms Module Level 2> # funcdef-translatez> |