Support0

CSS z-index playground

Change stacking levels and see which box paints in front. Compare siblings, nested contexts, and flex and grid items with position: static.

Ask AI

CSS z-index playground

Response generated with openai/gpt-5.4-nano. AI can make mistakes. Always review the result.
A
B

B follows A in the HTML and paints in front when both share a level and painting phase.

In front at the overlap
B
HTML
<div class="stacking-example">
  <div class="stack-a">A</div>
  <div class="stack-b">B</div>
</div>
Tailwind CSS
Examples for Tailwind CSS 4.3 with the default theme. Custom values keep arbitrary classes when there is no equivalent utility. Changing the theme can change utility values.
<div class="isolate flow-root">
  <div class="relative box-border min-inline-0 min-block-[5em] p-[0.5em] border wrap-anywhere z-auto [background:var(--stack-a,_#dce8ff)] text-[color:var(--stack-on-a,_#172335)] inline-3/5">A</div>
  <div class="relative box-border min-inline-0 min-block-[5em] p-[0.5em] border wrap-anywhere z-auto [background:var(--stack-b,_#ffdfc6)] text-[color:var(--stack-on-b,_#2a211b)] text-end inline-3/5 ms-auto mbs-[-2.5em]">B</div>
</div>
CSS
.stacking-example {
  isolation: isolate;
  display: flow-root;
}

.stacking-example :is(.stack-a, .stack-b) {
  position: relative;
  box-sizing: border-box;
  min-inline-size: 0;
  min-block-size: 5em;
  padding: 0.5em;
  border: 1px solid;
  overflow-wrap: anywhere;
}

.stacking-example .stack-a {
  z-index: auto;
  background: var(--stack-a, #dce8ff);
  color: var(--stack-on-a, #172335);
  inline-size: 60%;
}

.stacking-example .stack-b {
  z-index: auto;
  background: var(--stack-b, #ffdfc6);
  color: var(--stack-on-b, #2a211b);
  text-align: end;
  inline-size: 60%;
  margin-inline-start: auto;
  margin-block-start: -2.5em;
}

Educational guide to z-index

How to tell which box paints in front

Follow the stacking context tree and see which levels the browser compares when two boxes overlap.

Mental model

Find the stacking context the boxes share. Compare the level of each branch in that context and use the painting phase and document order to resolve ties.

Shared context
  • Parent A z-index: 1
    • Child A z-index: 999
  • B z-index: 2
Diagram of the example with parent A at level 1 and B at level 2. B paints in front of the whole A branch. Child A’s level 999 applies within its parent’s branch.

Painting order among siblings

These positioned boxes share a context. Negative levels paint first, followed by level zero (including auto for these boxes), then positive levels. When levels and painting phases match, the box that comes later in the HTML paints in front.

Both set to auto

A
B
B paints in front. Both boxes are in the level-zero phase and B comes later in the HTML.
Both set to auto
<div class="stacking-example">
  <div class="stack-a">A</div>
  <div class="stack-b">B</div>
</div>
.stacking-example {
  isolation: isolate;
  display: flow-root;
}

.stacking-example :is(.stack-a, .stack-b) {
  position: relative;
  box-sizing: border-box;
  min-inline-size: 0;
  min-block-size: 5em;
  padding: 0.5em;
  border: 1px solid;
  overflow-wrap: anywhere;
}

.stacking-example .stack-a {
  z-index: auto;
  background: var(--stack-a, #dce8ff);
  color: var(--stack-on-a, #172335);
  inline-size: 60%;
}

.stacking-example .stack-b {
  z-index: auto;
  background: var(--stack-b, #ffdfc6);
  color: var(--stack-on-b, #2a211b);
  text-align: end;
  inline-size: 60%;
  margin-inline-start: auto;
  margin-block-start: -2.5em;
}

Zero and negative levels

A
B
A paints in front. Its level 0 paints after B’s level −1.
Zero and negative levels
<div class="stacking-example">
  <div class="stack-a">A</div>
  <div class="stack-b">B</div>
</div>
.stacking-example {
  isolation: isolate;
  display: flow-root;
}

.stacking-example :is(.stack-a, .stack-b) {
  position: relative;
  box-sizing: border-box;
  min-inline-size: 0;
  min-block-size: 5em;
  padding: 0.5em;
  border: 1px solid;
  overflow-wrap: anywhere;
}

.stacking-example .stack-a {
  z-index: 0;
  background: var(--stack-a, #dce8ff);
  color: var(--stack-on-a, #172335);
  inline-size: 60%;
}

.stacking-example .stack-b {
  z-index: -1;
  background: var(--stack-b, #ffdfc6);
  color: var(--stack-on-b, #2a211b);
  text-align: end;
  inline-size: 60%;
  margin-inline-start: auto;
  margin-block-start: -2.5em;
}

Tied at 2

A
B
B paints in front. Both boxes are at level 2 and B comes later in the HTML.
Tied at 2
<div class="stacking-example">
  <div class="stack-a">A</div>
  <div class="stack-b">B</div>
</div>
.stacking-example {
  isolation: isolate;
  display: flow-root;
}

.stacking-example :is(.stack-a, .stack-b) {
  position: relative;
  box-sizing: border-box;
  min-inline-size: 0;
  min-block-size: 5em;
  padding: 0.5em;
  border: 1px solid;
  overflow-wrap: anywhere;
}

.stacking-example .stack-a {
  z-index: 2;
  background: var(--stack-a, #dce8ff);
  color: var(--stack-on-a, #172335);
  inline-size: 60%;
}

.stacking-example .stack-b {
  z-index: 2;
  background: var(--stack-b, #ffdfc6);
  color: var(--stack-on-b, #2a211b);
  text-align: end;
  inline-size: 60%;
  margin-inline-start: auto;
  margin-block-start: -2.5em;
}

The child at level 999

B stays at level 2 in all five comparisons. The dashed border marks parent A and the child stays at level 999.

With position: relative and z-index: auto, the child participates in the shared context. With z-index: 0, the parent stays at level zero and creates its own context containing the child.

Child at 999 in parent at 1

Parent A
Child A
B
B paints in front of the child. The shared context compares parent A’s level 1 with B’s level 2.
Child at 999 in parent at 1
<div class="stacking-example">
  <div class="stack-parent">Parent A
    <div class="stack-a">Child A</div>
  </div>
  <div class="stack-b">B</div>
</div>
.stacking-example {
  isolation: isolate;
  display: flow-root;
}

.stacking-example .stack-parent {
  position: relative;
  z-index: 1;
  isolation: auto;
  box-sizing: border-box;
  inline-size: 60%;
  padding: 0.5em;
  border: 1px dashed;
  overflow-wrap: anywhere;
}

.stacking-example :is(.stack-a, .stack-b) {
  position: relative;
  box-sizing: border-box;
  min-inline-size: 0;
  min-block-size: 5em;
  padding: 0.5em;
  border: 1px solid;
  overflow-wrap: anywhere;
}

.stacking-example .stack-a {
  z-index: 999;
  background: var(--stack-a, #dce8ff);
  color: var(--stack-on-a, #172335);
  margin-block-start: 0.5em;
}

.stacking-example .stack-b {
  z-index: 2;
  background: var(--stack-b, #ffdfc6);
  color: var(--stack-on-b, #2a211b);
  text-align: end;
  inline-size: 60%;
  margin-inline-start: auto;
  margin-block-start: -2.5em;
}

Parent at 3

Parent A
Child A
B
Child A paints in front. The parent’s level 3 places its whole branch above B.
Parent at 3
<div class="stacking-example">
  <div class="stack-parent">Parent A
    <div class="stack-a">Child A</div>
  </div>
  <div class="stack-b">B</div>
</div>
.stacking-example {
  isolation: isolate;
  display: flow-root;
}

.stacking-example .stack-parent {
  position: relative;
  z-index: 3;
  isolation: auto;
  box-sizing: border-box;
  inline-size: 60%;
  padding: 0.5em;
  border: 1px dashed;
  overflow-wrap: anywhere;
}

.stacking-example :is(.stack-a, .stack-b) {
  position: relative;
  box-sizing: border-box;
  min-inline-size: 0;
  min-block-size: 5em;
  padding: 0.5em;
  border: 1px solid;
  overflow-wrap: anywhere;
}

.stacking-example .stack-a {
  z-index: 999;
  background: var(--stack-a, #dce8ff);
  color: var(--stack-on-a, #172335);
  margin-block-start: 0.5em;
}

.stacking-example .stack-b {
  z-index: 2;
  background: var(--stack-b, #ffdfc6);
  color: var(--stack-on-b, #2a211b);
  text-align: end;
  inline-size: 60%;
  margin-inline-start: auto;
  margin-block-start: -2.5em;
}

Parent set to auto

Parent A
Child A
B
Child A paints in front. Its level 999 is compared with B’s level 2 in the shared context.
Parent set to auto
<div class="stacking-example">
  <div class="stack-parent">Parent A
    <div class="stack-a">Child A</div>
  </div>
  <div class="stack-b">B</div>
</div>
.stacking-example {
  isolation: isolate;
  display: flow-root;
}

.stacking-example .stack-parent {
  position: relative;
  z-index: auto;
  isolation: auto;
  box-sizing: border-box;
  inline-size: 60%;
  padding: 0.5em;
  border: 1px dashed;
  overflow-wrap: anywhere;
}

.stacking-example :is(.stack-a, .stack-b) {
  position: relative;
  box-sizing: border-box;
  min-inline-size: 0;
  min-block-size: 5em;
  padding: 0.5em;
  border: 1px solid;
  overflow-wrap: anywhere;
}

.stacking-example .stack-a {
  z-index: 999;
  background: var(--stack-a, #dce8ff);
  color: var(--stack-on-a, #172335);
  margin-block-start: 0.5em;
}

.stacking-example .stack-b {
  z-index: 2;
  background: var(--stack-b, #ffdfc6);
  color: var(--stack-on-b, #2a211b);
  text-align: end;
  inline-size: 60%;
  margin-inline-start: auto;
  margin-block-start: -2.5em;
}

Parent at 0

Parent A
Child A
B
B paints in front. Level 0 creates a context on parent A and keeps the child’s level 999 inside it.
Parent at 0
<div class="stacking-example">
  <div class="stack-parent">Parent A
    <div class="stack-a">Child A</div>
  </div>
  <div class="stack-b">B</div>
</div>
.stacking-example {
  isolation: isolate;
  display: flow-root;
}

.stacking-example .stack-parent {
  position: relative;
  z-index: 0;
  isolation: auto;
  box-sizing: border-box;
  inline-size: 60%;
  padding: 0.5em;
  border: 1px dashed;
  overflow-wrap: anywhere;
}

.stacking-example :is(.stack-a, .stack-b) {
  position: relative;
  box-sizing: border-box;
  min-inline-size: 0;
  min-block-size: 5em;
  padding: 0.5em;
  border: 1px solid;
  overflow-wrap: anywhere;
}

.stacking-example .stack-a {
  z-index: 999;
  background: var(--stack-a, #dce8ff);
  color: var(--stack-on-a, #172335);
  margin-block-start: 0.5em;
}

.stacking-example .stack-b {
  z-index: 2;
  background: var(--stack-b, #ffdfc6);
  color: var(--stack-on-b, #2a211b);
  text-align: end;
  inline-size: 60%;
  margin-inline-start: auto;
  margin-block-start: -2.5em;
}

Parent set to auto and isolated

Parent A
Child A
B
B paints in front. isolation: isolate puts parent A and its child in their own context.
Parent set to auto and isolated
<div class="stacking-example">
  <div class="stack-parent">Parent A
    <div class="stack-a">Child A</div>
  </div>
  <div class="stack-b">B</div>
</div>
.stacking-example {
  isolation: isolate;
  display: flow-root;
}

.stacking-example .stack-parent {
  position: relative;
  z-index: auto;
  isolation: isolate;
  box-sizing: border-box;
  inline-size: 60%;
  padding: 0.5em;
  border: 1px dashed;
  overflow-wrap: anywhere;
}

.stacking-example :is(.stack-a, .stack-b) {
  position: relative;
  box-sizing: border-box;
  min-inline-size: 0;
  min-block-size: 5em;
  padding: 0.5em;
  border: 1px solid;
  overflow-wrap: anywhere;
}

.stacking-example .stack-a {
  z-index: 999;
  background: var(--stack-a, #dce8ff);
  color: var(--stack-on-a, #172335);
  margin-block-start: 0.5em;
}

.stacking-example .stack-b {
  z-index: 2;
  background: var(--stack-b, #ffdfc6);
  color: var(--stack-on-b, #2a211b);
  text-align: end;
  inline-size: 60%;
  margin-inline-start: auto;
  margin-block-start: -2.5em;
}

Flex and grid items

An integer z-index also creates a stacking context on a flex or grid item with position: static. When level and painting phase match, HTML order decides the tie, with any changes made by order.

Flexbox with B in front

A
B
B paints in front at level 2. A and B are flex items and both remain position: static.
Flexbox with B in front
<div class="stacking-example">
  <div class="stack-a">A</div>
  <div class="stack-b">B</div>
</div>
.stacking-example {
  isolation: isolate;
  display: flex;
  align-items: flex-start;
}

.stacking-example :is(.stack-a, .stack-b) {
  position: static;
  box-sizing: border-box;
  min-inline-size: 0;
  min-block-size: 5em;
  padding: 0.5em;
  border: 1px solid;
  overflow-wrap: anywhere;
}

.stacking-example .stack-a {
  z-index: 1;
  background: var(--stack-a, #dce8ff);
  color: var(--stack-on-a, #172335);
  flex: 0 0 60%;
}

.stacking-example .stack-b {
  z-index: 2;
  background: var(--stack-b, #ffdfc6);
  color: var(--stack-on-b, #2a211b);
  text-align: end;
  flex: 0 0 60%;
  margin-inline-start: -20%;
  margin-block-start: 2.5em;
}

Grid with A in front

A
B
A paints in front at level 2. A and B are grid items and both remain position: static.
Grid with A in front
<div class="stacking-example">
  <div class="stack-a">A</div>
  <div class="stack-b">B</div>
</div>
.stacking-example {
  isolation: isolate;
  display: grid;
  grid-template-columns: 2fr 1fr 2fr;
  align-items: start;
}

.stacking-example :is(.stack-a, .stack-b) {
  position: static;
  box-sizing: border-box;
  min-inline-size: 0;
  min-block-size: 5em;
  padding: 0.5em;
  border: 1px solid;
  overflow-wrap: anywhere;
}

.stacking-example .stack-a {
  z-index: 2;
  background: var(--stack-a, #dce8ff);
  color: var(--stack-on-a, #172335);
  grid-area: 1 / 1 / 2 / 3;
}

.stacking-example .stack-b {
  z-index: 1;
  background: var(--stack-b, #ffdfc6);
  color: var(--stack-on-b, #2a211b);
  text-align: end;
  grid-area: 1 / 2 / 2 / 4;
  margin-block-start: 2.5em;
}

What creates a stacking context

When inspecting an overlap, check the computed styles of the ancestors too. These declarations create a stacking context on the boxes described.

position
An integer level on a positioned box creates a stacking context. Fixed and sticky boxes create one even with z-index: auto.
transform
Any transform value other than none creates a stacking context on a CSS box, including an identity transform such as translate(0).
opacity
An opacity value below 1 creates a stacking context and applies transparency to the box and its contents as a group.
isolation
isolation: isolate explicitly creates a stacking context. In the comparison, the parent creates one with position: relative and z-index: auto.

The browser’s top layer

The browser puts a dialog opened as a modal and a popover while it is shown in the top layer. This layer paints above the document’s content.

Use showModal() on a dialog when the interaction requires modality and use popover for non-modal content. Each API manages when the element enters and leaves the top layer.

Common pitfalls

Follow these checks from the box to the stacking context that decides the overlap.

The child at level 999 is covered

Expected result
Child A should paint in front of B.
Cause
The context containing the child is at level 1 and B participates in the shared context at level 2.
Solution
Raise the parent to level 3 if its whole branch should paint in front. If the parent is relative and its context comes solely from the integer level, try auto so the child participates in the shared context.

A static box seems to ignore its level

Expected result
The static box should paint in front when its z-index increases.
Cause
The box is in its container’s block flow with position: static.
Solution
Check the parent’s display. For a box in block flow, use position: relative to assign it a level and keep it in place.

Two boxes overlap at the same level

Expected result
A should paint in front of B.
Cause
A and B share a level and painting phase. B comes later in this scene’s HTML.
Solution
Give A a higher level in the same context. Keep the HTML order that reading and focus require.