translate()

Term side of the Card.
Topic: Transformations
Unit title: translate()
Categories: Function
Explanation side of the Card

Explanation: The function that moves an element from its current position along the X and Y axes. It can be used to adjust an element's position relative to its normal flow, creating effects like shifting or sliding.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 3.5.
  • The safari browser supports this feature starting from version 3.1.
Availability: 

Example

Wrongcss
.toast {
  position: relative;
  left: -100%;
  transition: left 0.3s;
}
.toast.open {
  left: 0;
}
Rightcss
.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 changing top, left or margin.
  • 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 transform list, or use the translate property.

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'.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy