1. 布局
  2. Z 索引

Quick reference

Class
Properties
z-0z-index: 0;
z-10z-index: 10;
z-20z-index: 20;
z-30z-index: 30;
z-40z-index: 40;
z-50z-index: 50;
z-autoz-index: auto;

基本用法

¥Basic usage

设置 z 索引

¥Setting the z-index

使用 z-* 工具来控制元素的堆叠顺序(或三维定位),无论其显示顺序如何。

¥Use the z-* utilities to control the stack order (or three-dimensional positioning) of an element, regardless of order it has been displayed.

05
04
03
02
01
<div class="z-40 ...">05</div>
<div class="z-30 ...">04</div>
<div class="z-20 ...">03</div>
<div class="z-10 ...">02</div>
<div class="z-0 ...">01</div>

使用负值

¥Using negative values

要使用负 z-index 值,请在类名前加上破折号以将其转换为负值。

¥To use a negative z-index value, prefix the class name with a dash to convert it to a negative value.

<div class="-z-50">
  <!-- ... -->
</div>

有条件地应用

悬停、聚焦和其他状态

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

<div class="z-0 hover:z-50">
  <!-- ... -->
</div>

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

断点和媒体查询

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

<div class="z-0 md:z-50">
  <!-- ... -->
</div>

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


使用自定义值

¥Using custom values

自定义主题

¥Customizing your theme

默认情况下,Tailwind 提供六个数字 z-index 工具和一个 auto 工具。你可以通过编辑 tailwind.config.js 文件中的 theme.zIndextheme.extend.zIndex 来自定义这些值。

¥By default, Tailwind provides six numeric z-index utilities and an auto utility. You can customize these values by editing theme.zIndex or theme.extend.zIndex in your tailwind.config.js file.

tailwind.config.js
module.exports = {
  theme: {
    extend: {
      zIndex: {
        '100': '100',
      }
    }
  }
}

主题定制 文档中了解有关自定义默认主题的更多信息。

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

任意值

¥Arbitrary values

If you need to use a one-off z-index 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="z-[100]">
  <!-- ... -->
</div>

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