Learn Z-Index in CSS

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…

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>

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?

💡 One exception. A direct child of a parent that uses flexbox or grid can use z-index, even when the child's position is still static.

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.

What you will learn