</> Code Corner

CSS · CSS Tutorial · Lesson 23

CSS Z-index

Control the stacking order of elements.

When elements overlap, z-index decides which one is on top. An element with a greater z-index is in front of one with a lower value.

Example

<style>
.box { position: absolute; width: 120px; height: 80px; color: white; padding: 6px; }
.a { background: tomato; top: 10px; left: 10px; z-index: 1; }
.b { background: dodgerblue; top: 40px; left: 60px; z-index: 3; }
.c { background: #04AA6D; top: 70px; left: 110px; z-index: 2; }
</style>
<div style="position:relative; height:180px;">
  <div class="box a">z-index 1</div>
  <div class="box b">z-index 3</div>
  <div class="box c">z-index 2</div>
</div>
Tip: z-index only works on positioned elements (relative, absolute, fixed, sticky) and flex/grid items.

Keep reading “CSS Z-index”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋