CKTIS / CSS
z-index — stacking order
The z-index property sets a box’s level within a stacking context. Larger numbers place it closer to the viewer, but only within the same context; a high value cannot let a descendant escape its ancestor’s stacking context.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
z-index: auto | <integer>;z-index controls overlapping positioned boxes and flex and grid items. It accepts auto or an <integer>, including negative values. Its initial value is auto; it is not inherited. Fractions and length units are invalid.
Within the same stacking context, a higher level is painted in front of a lower one. A child context is treated as a single unit: even an enormous descendant z-index cannot escape the ancestor’s context.
For position: relative and position: absolute, auto means level zero without creating a context through z-index; an integer creates a context. Thus, auto and 0 are not equivalent. Boxes with position: fixed or position: sticky create a context even with auto. Flex and grid items with a value other than auto create a context even with position: static.
In the example, #front covers #back despite occurring earlier in the document. If changing the number does not help, inspect ancestor contexts instead of repeatedly increasing z-index.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>z-index</title>
<style>
.stage {
position: relative;
z-index: 0;
width: 200px;
height: 140px;
}
.box {
position: absolute;
width: 120px;
height: 80px;
color: black;
}
#front {
top: 0;
left: 0;
background: orange;
z-index: 2;
}
#back {
top: 40px;
left: 60px;
background: lightblue;
z-index: 1;
}
</style>
<div class="stage">
<div class="box" id="front">FRONT</div>
<div class="box" id="back">BACK</div>
</div>
</html>Result
FRONT (orange) overlaps BACK (lightblue). No console output.
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.