translate
Moves an element horizontally, vertically, or in depth, independently of the transform property.
- Initial value
none- Inherited
- No
- Animatable
- yes, as a length or percentage
- Applies to
- transformable elements
- JavaScript
element.style.translate = "20px 10px"
Example
#.toast {
translate: 0 20px;
opacity: 0;
transition: translate 0.3s, opacity 0.3s;
}
.toast.visible {
translate: 0 0;
opacity: 1;
}<div class="box toast" style="transition: translate .3s, opacity .3s; translate: 0 0;">Progress saved ✓</div>
<script>
const toast = document.querySelector('.toast')
setInterval(() => {
const hidden = toast.style.opacity === '0'
toast.style.opacity = hidden ? '1' : '0'
toast.style.translate = hidden ? '0 0' : '0 20px'
}, 1500)
</script>Definition and usage
#The translate property is a standalone version of the translate() function. It lets you set a translation without overwriting other transforms written in transform.
One value moves horizontally, two move horizontally and vertically, and the third moves along the Z axis. Percentages are relative to the element’s own size, so translate: -50% -50% moves the element by half its width and height.
The browser applies the individual properties in a fixed order: first translate, then rotate, then scale, and finally transform.
Syntax
#translate: none | <x> <y>? <z>?;Values
#- No translation.
- Moves 60px to the right.
- 40px to the left and 20px down.
percentages
Moves by a fraction of the element’s own size.
Values in practice
#none
.demo { translate: none; }60px
.demo { translate: 60px; }-40px 20px
.demo { translate: -40px 20px; }50% -30%
.demo { translate: 50% -30%; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Separate
translate,rotate, andscalelet you animate one transform without rewriting the wholetransform. - Centring an absolutely positioned element:
left: 50%; top: 50%; translate: -50% -50%;.
Related entries
#- transformMoves, rotates, scales, or skews an element without affecting the page layout.
- rotateRotates an element by a given angle, independently of the transform property.
- scaleEnlarges or shrinks an element, independently of the transform property.
- positionChooses how an element is positioned: in the flow, relative, absolute, fixed, or sticky.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.