1. 变换
  2. 平移

变换

平移

用于平移元素的工具。

ClassStyles
translate-<number>
translate: calc(var(--spacing) * <number>) calc(var(--spacing) * <number>);
-translate-<number>
translate: calc(var(--spacing) * -<number>) calc(var(--spacing) * -<number>);
translate-<fraction>
translate: calc(<fraction> * 100%) calc(<fraction> * 100%);
-translate-<fraction>
translate: calc(<fraction> * -100%) calc(<fraction> * -100%);
translate-full
translate: 100% 100%;
-translate-full
translate: -100% -100%;
translate-px
translate: 1px 1px;
-translate-px
translate: -1px -1px;
translate-(<custom-property>)
translate: var(<custom-property>) var(<custom-property>);
translate-[<value>]
translate: <value> <value>;

示例

使用间距比例

使用 translate-<number> 工具,例如 translate-2-translate-4,根据间距比例平移元素:

-translate-6

translate-2

translate-8

<img class="-translate-6 ..." src="/img/mountains.jpg" />
<img class="translate-2 ..." src="/img/mountains.jpg" />
<img class="translate-8 ..." src="/img/mountains.jpg" />

使用百分比

使用 translate-<fraction> 工具,例如 translate-1/4-translate-full,以元素大小的百分比平移元素:

-translate-1/4

translate-1/6

translate-1/2

<img class="-translate-1/4 ..." src="/img/mountains.jpg" />
<img class="translate-1/6 ..." src="/img/mountains.jpg" />
<img class="translate-1/2 ..." src="/img/mountains.jpg" />

沿 x 轴平移

使用 translate-x-<number>translate-x-<fraction> 工具,例如 translate-x-4translate-x-1/4 沿 x 轴平移元素:

-translate-x-4

translate-x-2

translate-x-1/2

<img class="-translate-x-4 ..." src="/img/mountains.jpg" />
<img class="translate-x-2 ..." src="/img/mountains.jpg" />
<img class="translate-x-1/2 ..." src="/img/mountains.jpg" />

沿 y 轴平移

使用 translate-y-<number>translate-y-<fraction> 工具,例如 translate-y-6translate-y-1/3 沿 y 轴平移元素:

-translate-y-4

translate-y-2

translate-y-1/2

<img class="-translate-y-4 ..." src="/img/mountains.jpg" />
<img class="translate-y-2 ..." src="/img/mountains.jpg" />
<img class="translate-y-1/2 ..." src="/img/mountains.jpg" />

沿 z 轴平移

使用 translate-z-<number> 工具,例如 translate-z-6-translate-z-12 沿 z 轴平移元素:

-translate-z-8

translate-z-4

translate-z-12

<div class="transform-3d">
<img class="-translate-z-8 rotate-x-50 rotate-z-45 ..." src="/img/mountains.jpg" />
<img class="translate-z-2 rotate-x-50 rotate-z-45 ..." src="/img/mountains.jpg" />
<img class="translate-z-1/2 rotate-x-50 rotate-z-45 ..." src="/img/mountains.jpg" />
</div>

注意,translate-z-<number> 工具需要将 transform-3d 工具应用于父元素。

使用自定义值

Use the translate-[<value>] syntax to set the 平移 based on a completely custom value:

<img class="translate-[3.142rad] ..." src="/img/mountains.jpg" />

For CSS variables, you can also use the translate-(<custom-property>) syntax:

<img class="translate-(--my-translate) ..." src="/img/mountains.jpg" />

This is just a shorthand for translate-[var(<custom-property>)] that adds the var() function for you automatically.

响应式设计

Prefix a translate utility with a breakpoint variant like md: to only apply the utility at medium screen sizes and above:

<img class="translate-45 md:translate-60 ..." src="/img/mountains.jpg" />

Learn more about using variants in the variants documentation.