返回介绍

Translate

发布于 2024-07-30 22:15:33 字数 42019 浏览 0 评论 0 收藏 0

Utilities for translating elements with transform.

Quick reference

ClassProperties
translate-x-0transform: translateX(0px);
translate-y-0transform: translateY(0px);
translate-x-pxtransform: translateX(1px);
translate-y-pxtransform: translateY(1px);
translate-x-0.5transform: translateX(0.125rem);
translate-y-0.5transform: translateY(0.125rem);
translate-x-1transform: translateX(0.25rem);
translate-y-1transform: translateY(0.25rem);
translate-x-1.5transform: translateX(0.375rem);
translate-y-1.5transform: translateY(0.375rem);
translate-x-2transform: translateX(0.5rem);
translate-y-2transform: translateY(0.5rem);
translate-x-2.5transform: translateX(0.625rem);
translate-y-2.5transform: translateY(0.625rem);
translate-x-3transform: translateX(0.75rem);
translate-y-3transform: translateY(0.75rem);
translate-x-3.5transform: translateX(0.875rem);
translate-y-3.5transform: translateY(0.875rem);
translate-x-4transform: translateX(1rem);
translate-y-4transform: translateY(1rem);
translate-x-5transform: translateX(1.25rem);
translate-y-5transform: translateY(1.25rem);
translate-x-6transform: translateX(1.5rem);
translate-y-6transform: translateY(1.5rem);
translate-x-7transform: translateX(1.75rem);
translate-y-7transform: translateY(1.75rem);
translate-x-8transform: translateX(2rem);
translate-y-8transform: translateY(2rem);
translate-x-9transform: translateX(2.25rem);
translate-y-9transform: translateY(2.25rem);
translate-x-10transform: translateX(2.5rem);
translate-y-10transform: translateY(2.5rem);
translate-x-11transform: translateX(2.75rem);
translate-y-11transform: translateY(2.75rem);
translate-x-12transform: translateX(3rem);
translate-y-12transform: translateY(3rem);
translate-x-14transform: translateX(3.5rem);
translate-y-14transform: translateY(3.5rem);
translate-x-16transform: translateX(4rem);
translate-y-16transform: translateY(4rem);
translate-x-20transform: translateX(5rem);
translate-y-20transform: translateY(5rem);
translate-x-24transform: translateX(6rem);
translate-y-24transform: translateY(6rem);
translate-x-28transform: translateX(7rem);
translate-y-28transform: translateY(7rem);
translate-x-32transform: translateX(8rem);
translate-y-32transform: translateY(8rem);
translate-x-36transform: translateX(9rem);
translate-y-36transform: translateY(9rem);
translate-x-40transform: translateX(10rem);
translate-y-40transform: translateY(10rem);
translate-x-44transform: translateX(11rem);
translate-y-44transform: translateY(11rem);
translate-x-48transform: translateX(12rem);
translate-y-48transform: translateY(12rem);
translate-x-52transform: translateX(13rem);
translate-y-52transform: translateY(13rem);
translate-x-56transform: translateX(14rem);
translate-y-56transform: translateY(14rem);
translate-x-60transform: translateX(15rem);
translate-y-60transform: translateY(15rem);
translate-x-64transform: translateX(16rem);
translate-y-64transform: translateY(16rem);
translate-x-72transform: translateX(18rem);
translate-y-72transform: translateY(18rem);
translate-x-80transform: translateX(20rem);
translate-y-80transform: translateY(20rem);
translate-x-96transform: translateX(24rem);
translate-y-96transform: translateY(24rem);
translate-x-1/2transform: translateX(50%);
translate-x-1/3transform: translateX(33.333333%);
translate-x-2/3transform: translateX(66.666667%);
translate-x-1/4transform: translateX(25%);
translate-x-2/4transform: translateX(50%);
translate-x-3/4transform: translateX(75%);
translate-x-fulltransform: translateX(100%);
translate-y-1/2transform: translateY(50%);
translate-y-1/3transform: translateY(33.333333%);
translate-y-2/3transform: translateY(66.666667%);
translate-y-1/4transform: translateY(25%);
translate-y-2/4transform: translateY(50%);
translate-y-3/4transform: translateY(75%);
translate-y-fulltransform: translateY(100%);

Basic usage

Translating an element

Use the translate-x-* and translate-y-* utilities to translate an element.

translate-y-6

-translate-y-6

translate-x-6

<img class="translate-y-6 ...">
<img class="-translate-y-6 ...">
<img class="translate-x-6 ...">

Using negative values

To use a negative translate value, prefix the class name with a dash to convert it to a negative value.

<img class="-translate-y-6 ...">

Removing transforms

To remove all of the transforms on an element at once, use the transform-none utility:

<div class="scale-75 translate-x-4 skew-y-3 md:transform-none">
  <!-- ... -->
</div>

This can be useful when you want to remove transforms conditionally, such as on hover or at a particular breakpoint.

Hardware acceleration

If your transition performs better when rendered by the GPU instead of the CPU, you can force hardware acceleration by adding the transform-gpu utility:

<div class="translate-y-6 transform-gpu">
  <!-- ... -->
</div>

Use transform-cpu to force things back to the CPU if you need to undo this conditionally.


Applying conditionally

Hover, focus, and other states

Tailwind lets you conditionally apply utility classes in different states using variant modifiers. For example, use hover:translate-y-12 to only apply the translate-y-12 utility on hover.

<div class="hover:translate-y-12">
  <!-- ... -->
</div>

For a complete list of all available state modifiers, check out the Hover, Focus, & Other States documentation.

Breakpoints and media queries

You can also use variant modifiers to target media queries like responsive breakpoints, dark mode, prefers-reduced-motion, and more. For example, use md:translate-y-12 to apply the translate-y-12 utility at only medium screen sizes and above.

<div class="md:translate-y-12">
  <!-- ... -->
</div>

To learn more, check out the documentation on Responsive Design, Dark Mode and other media query modifiers.


Using custom values

Customizing your theme

By default, Tailwind provides fixed value translate utilities that match the default spacing scale, as well as 50% and 100% variations for translating relative to the element’s size. You can customize your spacing scale by editing theme.spacing or theme.extend.spacing in your tailwind.config.js file.

tailwind.config.js
module.exports = {
  theme: {
    extend: {
      spacing: {
        '4.25': '17rem',
      }
    }
  }
}

Alternatively, you can customize just the translate scale by editing theme.translate or theme.extend.translate in your tailwind.config.js file.

tailwind.config.js
module.exports = {
  theme: {
    extend: {
      translate: {
        '4.25': '17rem',
      }
    }
  }
}

Learn more about customizing the default theme in the theme customization documentation.

Arbitrary values

If you need to use a one-off translate value that doesn’t make sense to include in your theme, use square brackets to generate a property on the fly using any arbitrary value.

<div class="translate-y-[17rem]">
  <!-- ... -->
</div>

Learn more about arbitrary value support in the arbitrary values documentation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文