CSS Z-index

Control which element appears on top.

✍️ 3 exercises + code challenge in this lesson

When positioned elements overlap, z-index decides which one is in front. A higher value is closer to the viewer.

  • z-index only works on positioned elements (relative, absolute, fixed, sticky) and on flex/grid items.
  • Without z-index, elements later in the HTML appear on top.

Key Takeaways: CSS Z-index

Mastering CSS Z-index helps you write clean and reliable CSS code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

CSS Z-index Code Example

CSS
<style>
  .card { position: absolute; width: 120px; height: 80px; color: white; padding: 8px; border-radius: 8px; }
  .one   { background: #2563EB; top: 20px;  left: 20px;  z-index: 3; }
  .two   { background: #14B8A6; top: 50px;  left: 70px;  z-index: 2; }
  .three { background: #E34F26; top: 80px;  left: 120px; z-index: 1; }
</style>

<div style="position: relative; height: 190px;">
  <div class="card one">z-index: 3</div>
  <div class="card two">z-index: 2</div>
  <div class="card three">z-index: 1</div>
</div>
Free sign-in required · Google login available

CSS Z-index Practice Exercises

Check what you learned in “CSS Z-index”.

/ 3 solved
Fill in the blank

Put the modal in front of other positioned elements.

.modal {
  position: fixed;
  : 100;
}
✓ Correct! Not quite. Try again.
Multiple choice

z-index works on…

✓ Correct! Not quite. Try again.
Code challenge

Give .top a z-index of 10 so it appears above .bottom.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services