Layout

z-index

用於控制元素堆疊順序的通用類別。

ClassStyles
z-<number>
z-index: <number>;
z-auto
z-index: auto;
z-[<value>]
z-index: <value>;
z-(<custom-property>)
z-index: var(<custom-property>);

範例(Examples)

基本範例(Basic example)

使用 z-<number> 等通用類別(如 z-10z-50)來控制元素的堆疊順序(或三維定位),不受其顯示順序的影響:

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 值,請在類別名稱前加上破折號將其轉換為負值:

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)

使用 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)

z-index 通用類別前面加上像 md: 這樣的中斷點變體,讓該通用類別只在中型螢幕尺寸及以上時套用:

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

變體文件中了解更多關於使用變體的資訊。

Copyright © 2026 Tailwind Labs Inc.·商標政策(Brand)