---
title: "Why are use z-index in CSS explain with example?"  
description: "Why are use z-index in CSS explain with example?"  
author: "Ravi Vishwakarma"  
published: 2021-07-19  
canonical: https://answers.mindstick.com/qa/93783/why-are-use-z-index-in-css-explain-with-example  
category: "web application"  
tags: ["javascript", "html5", "css cascading style sheets", "web application development"]  
reading_time: 6 minutes  

---

# Why are use z-index in CSS explain with example?

Why are use z-[index](https://www.mindstick.com/articles/13152/why-google-s-mobile-first-index-makes-total-sense-in-2018) in [CSS](https://www.mindstick.com/forum/514/background-gradient-ie7-css-problem) [explain](https://yourviews.mindstick.com/view/86025/content-created-for-humans-not-for-bots-explain-this-statement) with example?

## Answers

### Answer by user

The z-index property specifies the stack order of an element. An element with greater stack order is always in front of an element with a lower stack order. 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).

```
<!DOCTYPE html>
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title></title>
    <style>
        body {
            box-sizing: border-box;
            margin: 0px;
            padding: 0px;
        }
        div {
            border-radius:50px;
        }
        .center {
            display:flex;
            justify-content: center;
            width: 100%;
            align-items: center;
            height: 100vh;
            position:relative;
            text-align:center;
            font-weight:bold;
            font-size:25px;
        }
        #first {
            width: 80%;
            height: 80%;
            background-color: black;
            color: white;
            border: 1px solid black;
            z-index: 1;
            position:absolute;
        }
        #middle {
            z-index: 3;
            position:absolute;
            width: 60%;
            height: 60%;
            background-color: red;
            color: white;
            border: 1px solid red;
        }
        #last {
            z-index: 5;
            position:absolute;
            width: 40%;
            height: 40%;
            background-color: blue;
            color: white;
            border: 1px solid blue;
        }
    </style>
</head>
<body>
    <div class='center'>
        <div id='last'>Last div</div>
        <div id='middle'>middle div </div>
        <div id='first'>first div</div>
    </div>
</body>
</html>
```

\

### Answer by Ethan Karla

The z-index property stores any element in the order of the stack. Whichever element has higher stack order is shown first and whichever has a lower stack is shown below the higher element.

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

Example

```
<!DOCTYPE html>
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title></title>
    <style>
        body {
            box-sizing: border-box;
            margin: 0px;
            padding: 0px;
        }
        div {
            border-radius:10px;
        }
        .center {
            display:flex;
            justify-content: center;
            width: 100%;
            align-items: center;
            height: 100vh;
            position:relative;
            text-align:center;
            font-weight:bold;
            font-size:25px;
        }
        #first {
            width: 80%;
            height: 80%;
            background-color: black;
            color: white;
            border: 1px solid black;
            z-index: 1;
            position:absolute;
        }
        #middle {
            z-index: 3;
            position:absolute;
            width: 60%;
            height: 60%;
            background-color: red;
            color: white;
            border: 1px solid red;
        }
        #last {
            z-index: 5;
            position:absolute;
            width: 40%;
            height: 40%;
            background-color: blue;
            color: white;
            border: 1px solid blue;
        }
    </style>
</head>
<body>
    <div class='center'>
        <div id='last'>Last div</div>
        <div id='middle'>middle div </div>
        <div id='first'>first div</div>
    </div>
</body>
</html>
```

![Why are use z-index in CSS explain with example?](https://answers.mindstick.com/questionanswer/e41fc8d0-d9e4-4301-ae49-671a66517871/images/72ae7eae-d5a3-419c-b555-dc907d54be9d.png)

\

### Answer by Sanjay Goenka

## What is a Z Index?

Z Index (`z-index`) is a CSS property that defines the order of overlapping HTML elements. Elements with a higher index will be placed on top of elements with a lower index.

**Note**: Z index only works on positioned elements (`position:absolute`, `position:relative`, or `position:fixed`).

#### Possible Values

```css
/* Default value if not specified */
z-index: auto;

/* Integer values */
z-index: 1;
z-index: 100;
z-index: 9999;
z-index: -1;

/* Global values */
z-index: inherit;
z-index: initial;
z-index: unset;
```

## How to use the Z Index

In this example, you can see three boxes displayed on top of each other in different orders using `z-index`.

*HTML*

```html
<div class='container'>
  <div class='box' id='blue'></div>
  <div class='box' id='red'></div>
  <div class='box' id='green'></div>
</div>
```

*CSS*

```css
#blue {
  background-color: blue;
}

#red {
  background-color: red;
}

#green {
  background-color: green;
}
```


---

Original Source: https://answers.mindstick.com/qa/93783/why-are-use-z-index-in-css-explain-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
