🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to CSS Notes
Topic #72

CSS Z-index


The CSS z-index Property

The z-index property specifies the stack order of positioned elements.

The stack order defines which element should be placed in front or behind other elements.

When elements are positioned, they can overlap other elements.

An element can have a positive or negative stack order (z-index):

image

This is a heading

Because the image has a z-index of -1, it will be placed behind the text.

Example

img
{

position: absolute;

  left: 0px;

top: 0px;

z-index: -1;

}

Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky) and flex items (elements that are direct children of display: flex elements).


Another z-index Example

A positioned element with a greater stack order is always above an element with a lower stack order.

Example

  <html>
<head>
<style>
.container {
  position: relative;
}

.black-box {
  position: relative;
  z-index: 1;

  border: 2px solid black;
  height: 100px;
  margin: 30px;
}

.gray-box {

  position: absolute;
  z-index: 3;
  background: lightgray;
  height: 60px;

  width: 70%;
  left: 50px;
  top: 50px;
}

.green-box {
  position: absolute;
  z-index: 2;
  background: lightgreen;

  width: 35%;
  left: 270px;
  top: -15px;
  height:
  100px;
}
</style>
</head>
<body>

<div class="container">
  <div
  class="black-box">Black box</div>
  <div class="gray-box">Gray
  box</div>
  <div class="green-box">Green box</div>
</div>

</body>
</html>

Without z-index

If several positioned elements overlap each other without a z-index specified, the elements render in the order they are defined in the HTML source code.

Example

  <html>
<head>
<style>
.container {
  position: relative;
}

.black-box {
  position: relative;

  border: 2px solid black;
  height: 100px;
  margin: 30px;
}

.gray-box {

  position: absolute;
  background: lightgray;
  height: 60px;

  width: 70%;
  left: 50px;
  top: 50px;
}

.green-box {
  position: absolute;
  background: lightgreen;

  width: 35%;
  left: 270px;
  top: -15px;
  height:
  100px;
}
</style>
</head>
<body>

<div class="container">
  <div
  class="black-box">Black box</div>
  <div class="gray-box">Gray
  box</div>
  <div class="green-box">Green box</div>
</div>

</body>
</html>

CSS Property

Property Description
z-index Sets the stack order of an element

Want to go beyond the notes?

Join CodingNow 2.0's CSS course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

CSS Z-index – FAQs

Quick answers about learning CSS Z-index in CSS.

This free note from CodingNow 2.0 explains CSS Z-index in CSS — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every CSS topic on CodingNow 2.0, including CSS Z-index, is 100% free with no signup required.
With focused practice, most students grasp CSS Z-index in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now