Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

translateZ(): depth becomes visible through perspective

Move a flat element nearer or farther in three-dimensional space without changing its layout dimensions.

Version: CSS Modules

Syntax

transform: translateZ(<length>);

translateZ() changes the local Z coordinate. Positive Z points toward the viewer in the ordinary CSS coordinate system; negative Z points away. The argument is a length, with unitless zero allowed; a percentage such as 50% is invalid. Moving on Z does not give a flat CSS box physical thickness and is not a replacement for z-index.

Without perspective or another transformation that turns depth into a visible displacement, an unrotated plane can look unchanged after moving on Z. With perspective, approaching the viewer enlarges the projected plane and moving away reduces it. Keep depth safely in front of the viewing plane; crossing it causes clipping rather than unlimited ordinary enlargement. Layout dimensions remain unchanged, and transform order changes the projection.

The example first moves an 80×40px box by 60px in Z without perspective: its visible size stays 80×40, while matrix m43 records 60. With perspective(240px) before translateZ(60px), the size becomes 106.67×53.33; with -60px it becomes 64×32. Reversing the positive-depth function order leaves the measured size 80×40. Values are rounded to two decimals; layout remains 80×40 and the percentage syntax test returns false.

Examples

const box=document.createElement('div');
box.style.cssText='position:fixed;left:20px;top:20px;width:80px;height:40px;transform-origin:0 0';
document.body.append(box);
const measure=value=>{box.style.transform=value;const r=box.getBoundingClientRect();return [r.width,r.height].map(v=>v.toFixed(2)).join(',')};
const plain=measure('translateZ(60px)');const depth=new DOMMatrix(getComputedStyle(box).transform).m43;
const near=measure('perspective(240px) translateZ(60px)');
const far=measure('perspective(240px) translateZ(-60px)');
const reversed=measure('translateZ(60px) perspective(240px)');
console.log('plain='+plain+'; depth='+depth+'; near='+near+'; far='+far+'; reversed='+reversed+'; layout='+box.offsetWidth+','+box.offsetHeight+'; percent='+CSS.supports('transform','translateZ(50%)'));

Result

plain=80.00,40.00; depth=60; near=106.67,53.33; far=64.00,32.00; reversed=80.00,40.00; layout=80,40; percent=false

The example uses a flat box and origin 0 0, with no transformed ancestors. The perspective-order result concerns this geometry; it is not a claim that Z translation is always invisible.

Sources