Introduction
Z-index helps decide which item appears on top when things overlap on a webpage.
Jump into concepts and practice - no test required
Z-index helps decide which item appears on top when things overlap on a webpage.
selector {
position: relative | absolute | fixed | sticky;
z-index: number;
}Z-index only works if the element has a position other than static.
Higher z-index numbers appear on top of lower ones.
div {
position: relative;
z-index: 10;
}.popup { position: fixed; z-index: 1000; }
.background { position: absolute; z-index: 1; }
The blue box overlaps the red box and appears on top because it has a higher z-index.
<style> .box1 { position: relative; width: 100px; height: 100px; background-color: red; z-index: 1; } .box2 { position: relative; width: 100px; height: 100px; background-color: blue; margin-top: -50px; margin-left: 50px; z-index: 2; } </style> <div class="box1"></div> <div class="box2"></div>
If two elements have the same z-index, the one later in the HTML code appears on top.
Negative z-index values place elements behind the page background.
Z-index controls stacking order of overlapping elements.
Only works on positioned elements (not static).
Higher numbers appear on top.
z-index control?z-indexz-index property is used to control which elements appear on top when they overlap.z-index to an element?position: relative with z-index works.div.a { position: relative; z-index: 5; }
div.b { position: relative; z-index: 10; }position: relative, so z-index applies.z-index property not work on this element?.box { z-index: 100; }position: static, which ignores z-index..one { position: relative; z-index: 1; }
.two { position: absolute; z-index: 3; }
.three { position: relative; z-index: 2; }