Course contentsShow
← Web Development
Lesson 529 of 2,9829. CSS Transitions and AnimationsFree lesson

2D Translation with translate()

Moving elements along the X and Y axes using translate(), translateX(), and translateY() functions.

What You'll Learn

You'll learn how to move HTML elements horizontally and vertically on the page using CSS transform functions—without affecting the document flow.

What Is 2D Translation?

Translation means shifting an element from its original position along the X-axis (horizontal) and/or Y-axis (vertical). Think of it like sliding a piece of paper across a desk—the paper doesn't change size or orientation, it just moves to a new spot.

The key difference from using margin or position? The element moves visually but still occupies its original space in the document flow—other elements don't rearrange.

The translate() Function Family

CSS provides three functions for 2D translation:

translate(x, y)

Moves an element along both axes at once:

.box {
  transform: translate(50px, 100px);
  /* Moves 50px right, 100px down */
}

translateX(value)

Moves only horizontally:

.box {
  transform: translateX(50px);
  /* Moves 50px right (positive) or left (negative) */
}

translateY(value)

Moves only vertically:

.box {
  transform: translateY(-30px);
  /* Moves 30px up (negative) or down (positive) */
}

How Values Work

  • Positive X: moves right
  • Negative X: moves left
  • Positive Y: moves down
  • Negative Y: moves up
  • You can use any length unit: px, %, rem, em, etc.
  • Percentages are relative to the element's own dimensions
.centered {
  transform: translate(-50%, -50%);
  /* Common centering technique */
}

Key Takeaway: Use translate(), translateX(), and translateY() to move elements visually without disrupting page layout—perfect for animations, hover effects, and positioning tricks.