布局
z-index
用于控制元素堆栈顺序的工具。
| 类 | 样式 |
|---|---|
z-<number> | z-index: <number>; |
z-auto | z-index: auto; |
z-[<value>] | z-index: <value>; |
z-(<custom-property>) | z-index: var(<custom-property>); |
示例(Examples)
¥Examples
基本示例(Basic example)
¥Basic example
使用 z-<number> 工具(如 z-10 和 z-50)控制元素的堆叠顺序(或三维定位),而不管其显示顺序如何:
¥Use the z-<number> utilities like z-10 and z-50 to control the stack order (or three-dimensional positioning) of an element, regardless of the 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)
¥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:
01
02
03
04
05
<div class="...">05</div><div class="...">04</div><div class="-z-10 ...">03</div><div class="...">02</div><div class="...">01</div>使用自定义值(Using a custom value)
¥Using a custom value
使用 z-[<value>] 语法 根据完全自定义的值设置 stack order:
<div class="z-[calc(var(--index)+1)] ..."> <!-- ... --></div>对于 CSS 变量,还可以使用 z-(<custom-property>) 语法:
<div class="z-(--my-z) ..."> <!-- ... --></div>这只是简写,用于 z-[var(<custom-property>)] 它会自动为你添加 var() 函数。
响应式设计(Responsive design)
¥Responsive design
在 z-index 工具前面使用断点变体如 md: 仅在 medium 屏幕尺寸及以上时应用工具:
<div class="z-0 md:z-50 ..."> <!-- ... --></div>请参阅 变体文档 详细了解如何使用变体。