The CSS translate3d() rework operate strikes a component in a three-dimensional house alongside the X, Y, and Z axes. It’s the 3D counterpart to the translate() operate, and the easiest way to maneuver a component once you need depth.
.card {
rework: translate3d(40px, -20px, 100px);
}
The translate3d() operate is used with the rework property and is outlined within the CSS Transforms Module Stage 2 specification.
Syntax
= translate3d(, , )
In easier phrases:
rework: translate3d(tx, ty, tz);
In plain English: Rework this factor by sliding it over horizontally, vertically, and by a sure depth in 3D house.
Arguments
The translate3d() operate takes three arguments:
/* px values */
rework: translate3d(50px, -30px, 200px);
/* percentages for x and y (relative to the factor itself) */
rework: translate3d(50%, -50%, 0);
/* mixture of models */
rework: translate3d(2rem, 1em, 100px);
/* no motion in any respect */
rework: translate3d(0, 0, 0);
tx: How a lot to translate the factor alongside the x-axis (horizontal). Optimistic values transfer it to the best, whereas unfavourable values transfer it to the left. It accepts, orvalues, relative to the factor’s width.ty: How a lot to translate the factor alongside the y-axis (vertical). Optimistic values transfer it down, whereas unfavourable values transfer it up. It accepts, orvalues, relative to the factor’s peak.tz: How a lot to translate the factor alongside the z-axis (depth). Optimistic values convey the factor nearer to the viewer, whereas unfavourable values push it additional away. This argument solely accepts; percentages will not be legitimate right here as there’s no depth to calculate a proportion of.
In comparison with different translate features
If you solely want to maneuver alongside one or two axes, there are shorthand features obtainable:
/* These are all equal */
rework: translate3d(50px, 0, 0);
rework: translateX(50px);
/* These are all equal */
rework: translate3d(0, 30px, 0);
rework: translateY(30px);
/* These are all equal */
rework: translate3d(0, 0, 100px);
rework: translateZ(100px);
translate3d() is the express three-axis model. It’s typically most popular even when one or two arguments are 0 as a result of it forces the browser to composite the factor on the GPU, which when obtainable, helps enhance animation efficiency.
What does “transferring alongside the Z axis” really imply?
The X and Y axes are fairly easy, proper? They’re the horizontal and vertical instructions in your display screen. The Z axis, then again, a bit wonky. It factors straight at you, transferring backwards and forwards in depth like a straight line and strikes towards and away from you.
When transferring a component alongside the Z axis in a constructive route (e.g., 50px), it seems to maneuver ahead, in the direction of you. A unfavourable worth (e.g., -50px) pushes the factor backwards, away from you in house.
However discover that, by itself, transferring a component alongside the Z axis has completely no impact. Now we have to make use of it with the perspective property utilized on the factor’s dad or mum factor. In different phrases, the factor’s dad or mum sorta acts as a container that the factor transforms in.
/* This alone will not work */
.factor {
rework: translate3d(0, 0, 100px);
}
/* As an alternative... */
/* Add perspective to a dad or mum to create depth */
.dad or mum {
perspective: 600px;
.factor {
rework: translate3d(0, 0, 100px); /* Visibly nearer */
}
}
Consider perspective as the gap between your eye and the display screen. A small worth (like 200px) creates an intense, fisheye-like depth impact. A big worth (like 1200px) creates delicate, refined depth. Usually a variety of 400px–900px works greatest, however you typically must mess around with the values to get issues excellent.
Fundamental utilization
Probably the most direct use of translate3d() is to animate parts alongside the Z axis to create a way of depth, usually seen in parallax results and card flips.
For instance, let’s say we have now three playing cards that sit at totally different Z positions in order that they really feel staggered in depth:
.dad or mum {
perspective: 800px;
.card-front {
rework: translate3d(0, 0, 80px);
}
.card-middle {
rework: translate3d(0, 0, 0px);
}
.card-back {
rework: translate3d(0, 0, -100px);
}
}
translate3d(0, 0, 0) may help efficiency
You’ll typically see this in older CSS codebases, and it’d look pointless:
.animated-element {
rework: translate3d(0, 0, 0);
}
It’s not a mistake. Although nothing is definitely moved, this promotes the factor to its personal composite layer, making animation considerably smoother. It’s a method of claiming, “Hey, browser, put this factor by itself separate layer.”
That stated, trendy browsers are good sufficient that this trick is basically essential anymore. In truth, will-change: rework typically suffices. Additionally, overusing translate3D() can waste reminiscence. Possibly think about using it for smoother transitions once you’re really experiencing a efficiency problem that janks your animation.
Combining translate3d() with different 3D transforms
The CSS translate3d() operate is strongest when it’s chained with rotate3d() or scale3d() for full 3D compositions.
It’s vital to know that rework features are rendered so as, right-to-left, so the order by which the properties are declared within the rework shorthand issues.
/* Rotates first, then interprets within the rotated house */
rework: translate3d(0, 0, 100px) rotateY(45deg);
/* Interprets first, then rotates across the unique origin */
rework: rotateY(45deg) translate3d(0, 0, 100px);
These two declarations produce totally different outcomes. Consider it like bodily motion: turning after which stepping ahead isn’t the identical as stepping ahead after which turning.
Specification
The translate3d() operate is outlined within the CSS Transforms Module Stage 2 specification, which is at present in Editor’s Draft. Meaning the data can change between now and when it formally turns into a Candidate Advice.
Browser help
The translate3d() operate is totally supported throughout all trendy browsers.








