Back to Content

translate

files/en-us/web/css/reference/properties/translate/index.md

latest4.5 KB
Original Source

The translate CSS property allows you to specify translation transforms individually and independently of the {{CSSxRef("transform")}} property. This maps better to typical user interface usage, and saves having to remember the exact order of transform functions to specify in the transform value.

{{InteractiveExample("CSS Demo: translate")}}

css
translate: none;
css
translate: 40px;
css
translate: 50% -40%;
css
translate: 20px 4rem;
css
translate: 20px 4rem 150px;
html
<section class="default-example" id="default-example">
  <div class="transition-all" id="example-element">
    <div class="face front">1</div>
    <div class="face back">2</div>
    <div class="face right">3</div>
    <div class="face left">4</div>
    <div class="face top">5</div>
    <div class="face bottom">6</div>
  </div>
</section>
css
#default-example {
  background: linear-gradient(skyblue, khaki);
  perspective: 800px;
  perspective-origin: 150% 150%;
}

#example-element {
  width: 100px;
  height: 100px;
  perspective: 550px;
  transform-style: preserve-3d;
}

.face {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  position: absolute;
  backface-visibility: inherit;
  font-size: 60px;
  color: white;
}

.front {
  background: rgb(90 90 90 / 0.7);
  transform: translateZ(50px);
}

.back {
  background: rgb(0 210 0 / 0.7);
  transform: rotateY(180deg) translateZ(50px);
}

.right {
  background: rgb(210 0 0 / 0.7);
  transform: rotateY(90deg) translateZ(50px);
}

.left {
  background: rgb(0 0 210 / 0.7);
  transform: rotateY(-90deg) translateZ(50px);
}

.top {
  background: rgb(210 210 0 / 0.7);
  transform: rotateX(90deg) translateZ(50px);
}

.bottom {
  background: rgb(210 0 210 / 0.7);
  transform: rotateX(-90deg) translateZ(50px);
}

Syntax

css
/* Keyword values */
translate: none;

/* Single values */
translate: 100px;
translate: 50%;

/* Two values */
translate: 100px 200px;
translate: 50% 105px;

/* Three values */
translate: 50% 105px 5rem;

/* Global values */
translate: inherit;
translate: initial;
translate: revert;
translate: revert-layer;
translate: unset;

Values

  • Single {{cssxref("<length-percentage>")}} value
    • : A {{cssxref("<length>")}} or {{cssxref("<percentage>")}} that specifies a translation along the X-axis. Equivalent to a translate() (2D translation) function with a single value specified.
  • Two {{cssxref("<length-percentage>")}} values
    • : Two {{cssxref("<length>")}} or {{cssxref("<percentage>")}} that specify the X and Y axis translation values (respectively) of a 2D translation. Equivalent to a translate() (2D translation) function with two values specified.
  • Three values
    • : Two {{cssxref("<length-percentage>")}} and single {{cssxref("<length>")}} values that specify the X, Y, and Z axis translation values (respectively) of a 3D translation. Equivalent to a translate3d() (3D translation) function.
  • none
    • : Specifies that no translation should be applied.

Formal definition

{{CSSInfo}}

Formal syntax

{{CSSSyntax}}

Examples

Translating an element on hover

This example shows how to use the translate property to move an element in three axes. The first box is moved along the X axis and the second box is moved along the X and Y axes. The third box is moved along the X, Y and Z axes and has the appearance of moving toward the viewer because of the addition of {{cssxref('perspective')}} to the parent element.

HTML

html
<div class="wrapper">
  <div id="box1">translate X</div>
  <div id="box2">translate X,Y</div>
  <div id="box3">translate X,Y,Z</div>
</div>

CSS

css
.wrapper {
  perspective: 100px;
  display: inline-flex;
  gap: 1em;
}
.wrapper > div {
  width: 7em;
  line-height: 7em;
  text-align: center;
  transition: 0.5s ease-in-out;
  border: 3px dotted;
}
#box1:hover {
  translate: 20px;
}

#box2:hover {
  translate: 20px 20px;
}

#box3:hover {
  translate: 5px 5px 30px;
}

Result

{{EmbedLiveSample("Translating_an_element_on_hover", "100%", 175)}}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • {{cssxref('scale')}}
  • {{cssxref('rotate')}}
  • {{cssxref('transform')}}

Note: skew is not an independent transform value