translateX()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.

Die translateX() CSS Funktion verschiebt ein Element horizontal auf der 2D-Ebene. Das Ergebnis ist ein <transform-function> Datentyp.

Probieren Sie es aus

transform: translateX(0);
transform: translateX(42px);
transform: translateX(-2.1rem);
transform: translateX(3ch);
<section id="default-example">
  <img
    class="transition-all"
    id="static-element"
    src="/shared-assets/images/examples/firefox-logo.svg"
    width="200" />
  <img
    class="transition-all"
    id="example-element"
    src="/shared-assets/images/examples/firefox-logo.svg"
    width="200" />
</section>
#static-element {
  opacity: 0.4;
  position: absolute;
}

#example-element {
  position: absolute;
}

Note: translateX(tx) entspricht translate(tx, 0) oder translate3d(tx, 0, 0).

Syntax

css
/* <length-percentage> values */
transform: translateX(200px);
transform: translateX(50%);

Werte

<length-percentage>

Ist ein <length> oder <percentage>, das die Abszisse (horizontal, x-Komponente) des Translationsvektors [tx, 0] repräsentiert. Im kartesischen Koordinatensystem stellt es eine Verschiebung entlang der x-Achse dar. Ein Prozentwert bezieht sich auf die Breite des Referenzrahmens, der durch die transform-box Eigenschaft definiert wird.

Kartesische Koordinaten auf ℝ^2 Homogene Koordinaten auf ℝℙ^2 Kartesische Koordinaten auf ℝ^3 Homogene Koordinaten auf ℝℙ^3

Eine Translation ist keine lineare Transformation in ℝ^2 und kann nicht mit Hilfe einer kartesischen Koordinatenmatrix dargestellt werden.

(10t010001)\left( \begin{array}{ccc} 1 & 0 & t \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array} \right)
(10t010001)\left( \begin{array}{ccc} 1 & 0 & t \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array} \right)
(100t010000100001)\left( \begin{array}{cccc} 1 & 0 & 0 & t \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{array} \right)
[1 0 0 1 t 0]

Formale Syntax

<translateX()> = 
translateX( <length-percentage> )

<length-percentage> =
<length> |
<percentage>

Beispiele

HTML

html
<div>Static</div>
<div class="moved">Moved</div>
<div>Static</div>

CSS

css
div {
  width: 60px;
  height: 60px;
  background-color: skyblue;
}

.moved {
  transform: translateX(10px); /* Equal to translate(10px) */
  background-color: pink;
}

Ergebnis

Spezifikationen

Specification
CSS Transforms Module Level 1
# funcdef-transform-translatex

Browser-Kompatibilität

BCD tables only load in the browser

Siehe auch