z-index
The CSS z-index
property controls the stacking order of overlapping elements. Elements with a higher z-index
value appear on top of elements with a lower value.
z-index
Worksz-index
applies only to elements with a position
of relative
, absolute
, fixed
, or sticky
.z-index
values are placed in front of lower z-index
elements.z-index
is auto
(follows the document order).
z-index
in Actionz-index
.box1, .box2 {
position: absolute;
width: 100px;
height: 100px;
}
.box1 {
background: blue;
top: 50px;
left: 50px;
}
.box2 {
background: red;
top: 70px;
left: 70px;
}
✔ Result:
Since .box2
is placed after .box1
, it appears on top.
z-index
to Control Layering