translate()
Example
.toast {
position: relative;
left: -100%;
transition: left 0.3s;
}
.toast.open {
left: 0;
}.toast {
transform: translate(-100%, 0);
transition: transform 0.3s;
}
.toast.open {
transform: translate(0, 0);
}Animating transform skips layout on each frame, so the slide stays smooth.
Common mistakes
- Expecting
translate(50%)to move by half the parent's width, while it uses the element's own width. - Expecting neighbors to fill the freed space, while translation doesn't affect layout.
Key points
- Animated movement uses
translate()instead of changingtop,leftormargin. - Percentages in
translate()are used as a share of the element's own size, not its parent. - Hover states keep the other functions in the same
transformlist, or use thetranslateproperty.
Interview questions
What is the difference between translate() and the translate property?
translate() is a function inside transform; the translate property sets the same move on its own, so it doesn't override other transforms.
What does translate() do with one value?
It moves the element only along the X axis; the Y offset is '0'.