Live editor for all the examples will be added soon.
The CSS z-index
property controls the stacking order of elements on a webpage. In other words, it determines which elements appear on top of or behind other elements when they overlap. Mastering z-index
is essential for creating layouts with layered effects, such as modals, dropdown menus, and image overlays.
In this guide, we’ll explore the basics of the z-index
property, how it works, and common scenarios where it is useful. We’ll also discuss some tips for avoiding common pitfalls when using z-index
.
1. What is CSS Z-Index?
The z-index
property determines the order of elements along the z-axis (which controls depth). By default, elements are stacked in the order they appear in the HTML, but you can change this behavior by assigning a z-index
value to specific elements. Elements with a higher z-index
will appear in front of elements with a lower z-index
.
How the Z-Axis Works:
- X-axis: Controls horizontal positioning.
- Y-axis: Controls vertical positioning.
- Z-axis: Controls depth (stacking order) or which element appears in front.
Syntax:
element {
z-index: value;
}
The z-index
value can be:
- Positive (e.g.,
z-index: 10;
): Puts the element in front of other elements. - Negative (e.g.,
z-index: -1;
): Places the element behind other elements. - Auto (default): The stacking order follows the HTML document flow.
Important Note: z-index
only works on elements with a positioned value (relative
, absolute
, fixed
, or sticky
). It does not apply to elements with position: static
(the default).
2. How Z-Index Works with Positioned Elements
For the z-index
property to take effect, the element must have a non-static positioning, such as relative
, absolute
, fixed
, or sticky
. If an element’s position
is static
, the z-index
will be ignored.
Syntax Example:
.box1 {
position: relative;
z-index: 10;
}
.box2 {
position: relative;
z-index: 5;
}
In this example:
.box1
has az-index
of 10 and will appear in front of.box2
, which has az-index
of 5.
3. Z-Index in Action: Example
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Z-Index Example</title>
<style>
.box {
width: 200px;
height: 200px;
position: relative;
}
.box1 {
background-color: lightblue;
z-index: 1;
}
.box2 {
background-color: coral;
margin-top: -150px; /* Overlap the boxes */
z-index: 2;
}
.box3 {
background-color: lightgreen;
margin-top: -150px;
z-index: 3;
}
</style>
</head>
<body>
<div class="box box1">Box 1</div>
<div class="box box2">Box 2</div>
<div class="box box3">Box 3</div>
</body>
</html>
Explanation:
.box1
hasz-index: 1
,.box2
hasz-index: 2
, and.box3
hasz-index: 3
.- Since the boxes overlap (due to the negative margin), Box 3 will appear on top of Box 2, and Box 2 will appear on top of Box 1, because of their
z-index
values.
4. Stacking Contexts
A stacking context is a set of elements that have a common stacking order. Whenever an element with position
and z-index
is introduced, it forms a new stacking context. Elements inside this context are stacked relative to each other but won’t affect elements outside of their context.
How Stacking Contexts Are Created:
- Elements with
position: relative
,absolute
,fixed
, orsticky
and az-index
other thanauto
create new stacking contexts. - Elements with specific properties like
opacity
less than 1 ortransform
also create new stacking contexts.
Example of Stacking Context:
.parent {
position: relative;
z-index: 10;
}
.child {
position: absolute;
z-index: 5;
}
In this example, .child
has a z-index
of 5, but it is relative to its parent .parent
, which has a z-index
of 10. This means .parent
will stack above elements outside of its context, but .child
is stacked relative to .parent
.
5. Negative Z-Index
You can assign negative values to z-index
to place elements behind others. However, be careful when using negative z-index
because it might hide elements behind other content or backgrounds.
Example:
.background {
position: absolute;
z-index: -1;
width: 100%;
height: 100%;
background-color: lightgrey;
}
.foreground {
position: relative;
z-index: 1;
background-color: white;
}
In this example:
- The
.background
element is positioned behind the.foreground
element because it has az-index
of-1
.
6. Common Use Cases for Z-Index
6.1 Overlapping Elements
The most common use case for z-index
is when you need to overlap elements and control which element appears on top.
Example:
<div class="menu">
<div class="dropdown">Dropdown Menu</div>
</div>
<div class="content">Content</div>
<style>
.menu {
position: relative;
z-index: 1000; /* Make sure menu appears on top */
}
.content {
position: relative;
z-index: 10;
}
</style>
In this case, the .menu
will appear above .content
because it has a higher z-index
.
6.2 Modals and Pop-ups
When you create modals or pop-ups, you often want them to appear on top of all other content. You can achieve this by setting a high z-index
on the modal container.
Example:
.modal {
position: fixed;
z-index: 9999;
width: 300px;
height: 200px;
background-color: white;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
This ensures that the modal will appear on top of all other elements, regardless of their z-index
.
7. Z-Index Troubleshooting Tips
Sometimes, z-index
may not work as expected. Here are some common issues and how to resolve them:
7.1 Ensure Elements are Positioned
If you apply a z-index
and nothing happens, make sure the element has a position
value other than static
(i.e., use relative
, absolute
, fixed
, or sticky
).
.element {
position: relative; /* Required for z-index to work */
z-index: 10;
}
7.2 Check for Stacking Contexts
If an element’s z-index
isn’t behaving as expected, check if it is inside a stacking context. You may need to modify the z-index
of the parent element to adjust its stacking behavior.
.parent {
position: relative;
z-index: 5; /* The stacking context */
}
.child {
position: absolute;
z-index: 10; /* Relative to .parent */
}
In this case, the child element’s z-index
is relative to its parent, not to other elements outside the parent.
8. Practical Example: Layering a Navigation Menu Over Content
Let’s create a navigation bar that stays fixed at the top of the page, layered on top of content using z-index
.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Z-Index Navigation Example</title>
<style>
/* Fixed navigation bar */
.navbar {
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: darkblue;
color: white;
padding: 15px;
z-index: 1000; /* Ensure navbar stays on top */
}
/* Main content */
.content {
margin-top: 80px;
padding: 20px;
background-color: lightgrey;
z-index: 1; /* Content goes behind navbar */
position
: relative;
}
</style>
</head>
<body>
<!-- Navigation Bar -->
<div class="navbar">
Navigation Bar
</div>
<!-- Main Content -->
<div class="content">
<h1>Main Content</h1>
<p>This content is underneath the fixed navigation bar.</p>
</div>
</body>
</html>
Explanation:
- The
.navbar
hasposition: fixed;
and a highz-index
of1000
, ensuring it stays on top of the page content, even as the user scrolls. - The
.content
hasposition: relative;
and az-index
of1
, meaning it will appear underneath the navbar.
Conclusion
The CSS z-index
property is a powerful tool for controlling the stacking order of elements on a webpage. By assigning different z-index
values, you can create layered effects, ensure modals or menus stay on top, and manage the depth of elements in complex layouts.
However, it’s important to understand how stacking contexts and positioning work to use z-index
effectively. Mastering z-index
helps you build polished, professional-looking designs where elements overlap seamlessly and behave as expected.