When two elements take up the same place, one of them covers the other. Think of a few sheets of paper stacked on a desk: you see the whole top sheet, and the sheets below it are partly hidden. Elements on a page are stacked like those sheets. z-index is the layer number of each element in the stack: the bigger the number, the higher the element is in the stack.
Big numbers on top, small numbers below
These two boxes are made to overlap (one covers part of the other) on purpose:
<div class="red">red</div>
<div class="green">green</div>
<style>
div {
width: 120px;
height: 80px;
}
.red {
position: relative;
z-index: 4;
background: red;
}
.green {
position: absolute;
top: 60px;
left: 80px;
z-index: 3;
background: green;
}
</style>
divgives both boxes the same size, 120 by 80 pixels..greenis moved withposition: absolute,topandleftuntil it overlaps the bottom right corner of the red box.z-index: 3is the layer number of the green box.z-index: 4is the layer number of the red box (.red). 4 is bigger than 3, so red is on top.
The result: where the boxes overlap, you see the red box, and that part of the green box is hidden. If these two boxes had no z-index, or the same number, the box written later in the HTML would show on top: the green box. So to be sure an element is on top, its number has to be bigger.
The condition: position
z-index only works on an element whose position is not static, for example relative or absolute. That is why .red in the example above has position: relative. Every element starts with position: static, so div { z-index: 999; } does nothing, no matter how big the number is. The fix: add a position.
div {
position: relative;
z-index: 999;
}
In the exercise, the box you need to raise already has position: absolute. You only add one z-index line to that same rule, with a bigger number than the box that covers it now.
⚠️ The number is big, so why is it still covered?
- The same number. When two elements both have
z-index: 4, the element written later shows on top. Give a bigger number to the element that must be on top. - A child can't break out of its parent. If a parent has its own
positionandz-index, the child'sz-indexis only compared with its siblings, the other elements inside the same parent. However big the child's number is, the child can't show on top of an element that covers its parent. That separate stack inside the parent is called a stacking context. A parent doesn't needz-indexto make a stacking context.transformandfilter(both covered later),opacitybelow 1, andposition: fixedorstickymake one too. For example: every card usestransformfor a hover effect (when the mouse pointer is over it). The dropdown (a menu that opens downward) in the first card is hidden under the second card, even though the dropdown hasz-index: 100. The reason: each card makes its own stacking context, so the 100 only counts inside the first card. Raise the card, not the dropdown: give the first cardposition: relative; z-index: 1. If you do want thez-indexnumbers inside one part of the page kept apart from the rest, writeisolation: isolateon its parent. That parent then makes a stacking context withoutpositionorz-index. - Giant numbers. After
z-index: 99999, the next element that must be on top needs an even bigger number. Use small numbers in regular steps, like 1, 2, 3 or 10, 20, 30.
💡 One exception. A direct child of a parent that uses flexbox or grid can use
z-index, even when the child'spositionis stillstatic.
TL;DR: z-index is a layer number for elements that overlap: the element with the bigger number shows on top. If the numbers are equal, the element written later in the HTML shows on top. z-index only works on elements whose position is not static, so pair it with position: relative or absolute. If a parent has its own position and z-index, its child's number is only compared with the other elements inside that parent.
⚡ Big numbers on top
Write:
.red {
position: relative;
z-index: 4;
}
.green {
position: absolute;
z-index: 3;
}
Meaning: z-index is a layer number for elements that overlap, here the red box (.red) and the green box (.green). 4 is bigger than 3, so the red box shows on top of the green box. If the numbers are equal, the element written later in the HTML shows on top.
⚡ The condition: position
Guess first: a box gets div { z-index: 999; } with no position. Does the box move to the top layer?
No. Every element starts with position: static, and z-index doesn't work on a static element, no matter how big the number is.
Meaning: z-index only works on an element whose position is not static. So pair z-index with position: relative or absolute.
In the exercise, the box you need to raise already has position: absolute. You only add one z-index line, with a bigger number than the box that covers it.
⚡ A child can't break out of its parent
Meaning: If a parent has its own position and z-index, the child's z-index is only compared with its siblings, the other elements inside that parent. So the child can't show on top of an element that covers its parent, however big the child's number is.