CSS translateX, translateY, and translateZ property


The translateX, translateY, and translateZ properties in CSS are part of the transform function family, specifically used for moving elements along the X, Y, and Z axes, respectively. These properties are often used to create animations, transitions, or simply reposition elements within their container.

1. translateX()

The translateX() function moves an element horizontally along the X-axis.

  • Syntax:

    transform: translateX(value);
    • value: The distance to move the element. It can be in units like pixels (px), percentages (%), em, rem, etc. Positive values move the element to the right, and negative values move it to the left.
  • Example:

    .box { transform: translateX(50px); }
    • This will move the element 50 pixels to the right.

2. translateY()

The translateY() function moves an element vertically along the Y-axis.

  • Syntax:

    transform: translateY(value);
    • value: The distance to move the element. Positive values move the element downward, and negative values move it upward.
  • Example:

    .box { transform: translateY(100px); }
    • This will move the element 100 pixels down.

3. translateZ()

The translateZ() function moves an element along the Z-axis, which represents the depth (forward or backward) in 3D space. This function is particularly useful in 3D transformations.

  • Syntax:

    transform: translateZ(value);
    • value: The distance to move the element along the Z-axis. Positive values bring the element closer to the viewer, while negative values push it further away.
  • Example:

    .box { transform: translateZ(200px); }
    • This will move the element 200 pixels closer to the viewer.

Practical Example with translateX, translateY, and translateZ

HTML and CSS Example

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Translate Example</title> <style> .box { width: 100px; height: 100px; background-color: lightblue; margin: 50px; transition: transform 0.5s ease; } .translate-x { transform: translateX(100px); } .translate-y { transform: translateY(50px); } .translate-z { perspective: 500px; transform: translateZ(100px); } </style> </head> <body> <div class="box translate-x">Translate X</div> <div class="box translate-y">Translate Y</div> <div class="box translate-z">Translate Z</div> </body> </html>

Explanation

  1. Translate X:

    • The first box is moved 100 pixels to the right along the X-axis.
  2. Translate Y:

    • The second box is moved 50 pixels down along the Y-axis.
  3. Translate Z:

    • The third box is moved 100 pixels towards the viewer along the Z-axis, creating a 3D effect. Note that to see the Z-axis effect properly, you usually need to include a perspective value on the parent element or directly on the element itself, which determines the depth effect.

Combining Translations

You can combine multiple translations to move an element in two or three dimensions simultaneously:

transform: translateX(50px) translateY(100px);
  • This will move the element 50 pixels to the right and 100 pixels down.