CSS z-index: Why auto and 0 stack differently
CSS z-index controls the front-to-back order of overlapping elements. Larger values win when compared at the same stacking level within a stacking context. A child with a high value can still sit behind another element if the context created by its parent is painted behind that element.
Find which elements z-index applies to
An integer z-index on a relatively or absolutely positioned element creates a stacking context. That includes 0. An auto value does not create one by itself. Fixed and sticky boxes create a context even with auto. Flex and grid items also accept an integer z-index with position: static. See CSS Positioned Layout · Painting Order and CSS Flexbox · Painting Order · CSS Grid · Z-axis Ordering.
Run the overlapping-box example
Save the code as z-index.html and open it. isolation: isolate keeps the two test areas independent. Compare the areas where the blue and teal boxes overlap.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>z-index and parent stacking contexts</title>
<style>
body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
* { box-sizing: border-box; }
h1 { font-size: 24px; }
h2 { font-size: 19px; }
.stage { position: relative; isolation: isolate; max-width: 500px; height: 180px;
border: 1px solid #9aafc2; margin-bottom: 24px; }
.box { position: absolute; width: 150px; height: 100px; padding: 10px; color: white; }
.simple .a { left: 10px; top: 10px; background: #1768a5; z-index: 1; }
.simple .b { left: 100px; top: 60px; background: #087b76; z-index: 2; }
.parent-a { position: absolute; left: 10px; top: 10px; width: 180px; height: 120px;
background: #dcebf7; z-index: 1; }
.child { left: 50px; top: 40px; background: #1768a5; z-index: 9999; }
.parent-b { left: 120px; top: 70px; background: #087b76; z-index: 2; }
</style>
</head>
<body>
<h1>How z-index values are compared</h1>
<h2>Inside one stacking context</h2>
<div class="stage simple">
<div class="box a">A: 1</div>
<div class="box b">B: 2</div>
</div>
<h2>Separate parent stacking contexts</h2>
<div class="stage nested">
<div class="parent-a">Parent A: 1
<div class="box child">Child<br>9999</div>
</div>
<div class="box parent-b">Parent B: 2</div>
</div>
</body>
</html>
In the first test, teal B at 2 covers blue A at 1. In the second, the blue child has 9999 but belongs to parent A at 1. Teal B at 2 remains in front where they overlap.
Compare auto and 0 on the parent
Change only the second test’s .parent-a value to 0. The child stays behind B because the parent still forms a context. Then change the same value to auto. In this example, the parent stops forming a context. The child’s 9999 is now compared with B’s 2, so blue comes to the front.
Restore the value to 1, then try 3. This brings the parent and its child forward together. If auto makes no difference in a project, inspect the parent and other ancestors for another context trigger, such as transform, opacity below 1, or isolation: isolate.
Check clipping and the top layer
Negative values are valid. If a decoration disappears, check which backgrounds paint in front of it. If a menu or shadow is cut off at the edge of a box, check ancestor overflow. Changing z-index does not remove clipping. A modal dialog and a shown popover use the browser’s top layer. Increasing an ordinary document element’s value cannot paint it above that layer. See CSS Positioned Layout · Top Layer.




