How to Make the CSS z-index Property Work
In this snippet, you can find out how to make the CSS z-index property work. See which values of the position property can be used with the z-index property.
Solutions with the CSS position property
The CSS z-index property works with elements that have a non-static position value (i.e., absolute, relative, fixed, or sticky). It also works on direct children of flex and grid containers without requiring a position value.
Let’s see some examples.
For standard positioning, use relative, absolute, fixed, or sticky. Alternatively, z-index works on direct children of flex and grid containers without needing a position value.
Example of using the z-index property with the "relative" value of the position property:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
div {
width: 150px;
height: 150px;
padding: 15px;
}
.one,
.two,
.three {
position: relative;
}
.one {
background: #a7b5aa;
outline: 2px solid #000;
top: 100px;
left: 200px;
z-index: 10;
}
.two {
background: #4b48db;
outline: 2px solid #000;
top: 50px;
left: 75px;
z-index: 100;
}
.three {
background: #1ba13f;
outline: 2px solid #000;
top: 30px;
left: 25px;
z-index: 150;
}
</style>
</head>
<body>
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
</body>
</html>Result
<div class="demo mb-20 not-prose"> XFI4 </div> XFI5 </div> XFI6 </div> </div>
Example of using the z-index property with the "absolute" value of the position property:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
img {
position: absolute;
left: 0;
top: 20px;
z-index: -1;
}
</style>
</head>
<body>
<h2>Example</h2>
<img src="/build/images/w3docs-logo-black.png" alt="W3docs logo" width="195" height="45" />
</body>
</html>Now, let’s see how these boxes will behave with the "fixed" value.
Example of using the z-index property with the "fixed" value of the position property:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
div {
width: 150px;
height: 150px;
padding: 15px;
}
.one,
.two,
.three {
position: fixed;
}
.one {
background: #a7b5aa;
outline: 2px solid #000;
top: 100px;
left: 200px;
z-index: 10;
}
.two {
background: #4b48db;
outline: 2px solid #000;
top: 50px;
left: 75px;
z-index: 100;
}
.three {
background: #1ba13f;
outline: 2px solid #000;
top: 30px;
left: 25px;
z-index: 150;
}
</style>
</head>
<body>
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
</body>
</html>Example of using the z-index property with the "sticky" value of the position property:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
div {
width: 150px;
height: 150px;
padding: 15px;
}
.one,
.two,
.three {
position: sticky;
top: 0;
}
.one {
background: #a7b5aa;
outline: 2px solid #000;
left: 200px;
z-index: 10;
}
.two {
background: #4b48db;
outline: 2px solid #000;
left: 75px;
z-index: 100;
}
.three {
background: #1ba13f;
outline: 2px solid #000;
left: 25px;
z-index: 150;
}
</style>
</head>
<body>
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
</body>
</html>When the z-index property is not applied, the browser stacks elements in the following default order:
- root element,
- non-positioned elements in the order they are specified,
- positioned elements in the order they are specified.
z-index only affects stacking within the same stacking context. Elements in different stacking contexts are stacked according to their parent's position in the document flow.