CSS Z-Index Get Certified
What is CSS Z-Index?
The CSS z-index property controls the stacking order of positioned elements. Elements with a higher z-index appear above those with a lower value. It only works on elements that have a position value other than static.
Why Use CSS Z-Index?
Z-index is used for overlays, badges, sticky menus, notifications, and any layout where one element must appear in front of another.
Basic Z-Index Syntax
What this code does: Shows the general form of the z-index property on a positioned element.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-box {
position: relative;
z-index: 10;
background: #e0f2fe;
padding: 16px;
border-radius: 6px;
}
</style>
</head>
<body>
<div class="demo-box">Element with z-index: 10</div>
</body>
</html>
Step 1: Overlapping Elements
The z-index property works only on positioned elements. In this example, the blue box appears above the red box because it has a higher z-index.
What this code does: Stacks a blue box (z-index 2) above a red box (z-index 1) so the blue square covers part of the red one.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
position: relative;
min-height: 250px;
}
.demo-blue {
position: absolute;
top: 80px;
left: 80px;
width: 150px;
height: 150px;
background: blue;
z-index: 2;
}
.demo-red {
position: absolute;
top: 40px;
left: 40px;
width: 150px;
height: 150px;
background: red;
z-index: 1;
}
</style>
</head>
<body>
<div class="demo-red"></div>
<div class="demo-blue"></div>
</body>
</html>
Step 2: Place an Element Behind Another
A negative z-index value moves an element behind other positioned elements.
What this code does: Puts an orange background behind the white text by giving it z-index: -1.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-image {
position: relative;
width: 250px;
height: 150px;
background: lightblue;
}
.demo-text {
position: absolute;
top: 40px;
left: 50px;
background: white;
padding: 10px;
z-index: 1;
}
.demo-background {
position: absolute;
top: 20px;
left: 20px;
width: 200px;
height: 100px;
background: orange;
z-index: -1;
}
</style>
</head>
<body>
<div class="demo-image">
<div class="demo-background"></div>
<div class="demo-text">Front Text</div>
</div>
</body>
</html>
Step 3: Image Caption Overlay
Z-index is commonly used to display text above images.
What this code does: Places a dark caption over the bottom of an image using absolute positioning and a higher z-index.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-card {
position: relative;
width: 320px;
}
.demo-card img {
width: 100%;
display: block;
border-radius: 6px;
}
.demo-caption {
position: absolute;
bottom: 10px;
left: 10px;
background: rgba(0,0,0,.7);
color: white;
padding: 10px;
z-index: 5;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="demo-card">
<img src="https://picsum.photos/320/200" alt="Image">
<div class="demo-caption">Beautiful Landscape</div>
</div>
</body>
</html>
Step 4: Floating Notification
A high z-index value keeps notifications and floating buttons above other webpage elements.
What this code does: Pins a green notification to the top-right corner with z-index: 999 so it stays on top.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
min-height: 400px;
}
.demo-notice {
position: fixed;
top: 20px;
right: 20px;
background: #28a745;
color: white;
padding: 15px;
z-index: 999;
border-radius: 6px;
}
</style>
</head>
<body>
<div class="demo-notice">Notification</div>
<p>Page content appears underneath the floating notice.</p>
</body>
</html>
Step 5: Navigation Menu Above Content
Navigation bars often use a high z-index value so they remain above page content.
What this code does: Creates a fixed blue navbar with z-index: 1000 so page content never covers it.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.demo-navbar {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: #007BFF;
color: white;
padding: 15px;
z-index: 1000;
}
.demo-content {
margin-top: 70px;
padding: 20px;
}
</style>
</head>
<body>
<div class="demo-navbar">My Navigation Bar</div>
<div class="demo-content">This content appears below the navigation bar.</div>
</body>
</html>
Complete CSS Z-Index Example
What this code does: Places a red “SALE” badge on top of a product image using absolute positioning and a higher z-index.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-card {
position: relative;
width: 350px;
}
.demo-card img {
width: 100%;
display: block;
border-radius: 8px;
}
.demo-badge {
position: absolute;
top: 15px;
right: 15px;
background: red;
color: white;
padding: 8px 14px;
border-radius: 5px;
z-index: 10;
}
</style>
</head>
<body>
<div class="demo-card">
<img src="https://picsum.photos/350/220" alt="Product">
<div class="demo-badge">SALE</div>
</div>
</body>
</html>
Advantages of CSS Z-Index
Best Practices
- Apply z-index only to positioned elements.
- Use small z-index values whenever possible.
- Reserve high values (such as 999 or 1000) for menus, modals, and popups.
- Avoid using extremely large z-index values unless necessary.
- Organize stacking levels consistently throughout your project.
- Test overlapping elements on different screen sizes.
- Use comments to document important z-index values in large projects.
- Combine z-index with Position, Flexbox, and Grid for modern layouts.
Web Resources on CSS Z-Index
1. MDN Web Docs : z-index CSS property
2. Web.dev : Z-index and stacking contexts
3. CSS Tricks.com : Systems for z-index
4. Dev.to : Z-Index (CSS), Everything you need to know
Click “Run Code” to see the result here.
CSS Syntax Example
The CSS syntax consists of a selector and a declaration block.
h1 { color: blue; font-size: 36px;}Selector: h1 – targets all <h1> elements
Property: color and font-size
Value: blue and 36px
Your CSS Applied Below:
This is an H1 heading with your CSS
This is an H2 heading (not affected)
This paragraph is not affected by the h1 selector.
Welcome to CSS
This page is styled using CSS.
The heading and paragraph use your CSS rules.
- List item with your styles
- Another list item
🧪 Test Your CSS Code
Edit the CSS code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.