The z-index property in CSS is used to control the vertical stacking order of elements that overlap. It only works on positioned elements (i.e., elements with a position value other than static).
Key Concepts
- Stacking Context: A stacking context is formed when an element is positioned and has a
z-indexvalue other thanauto. Elements within a stacking context are stacked according to theirz-indexvalues. - Positioned Elements: For
z-indexto work, the element must have itspositionproperty set torelative,absolute,fixed, orsticky. - Default Stacking Order: By default, elements are stacked in the order they appear in the HTML, with later elements appearing on top of earlier ones.
Syntax
.element {
position: relative; /* or absolute, fixed, sticky */
z-index: 10; /* Any integer value */
}Practical Examples
Example 1: Basic Usage
<!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>
.box1 {
position: absolute;
top: 50px;
left: 50px;
width: 100px;
height: 100px;
background-color: red;
z-index: 1;
}
.box2 {
position: absolute;
top: 100px;
left: 100px;
width: 100px;
height: 100px;
background-color: blue;
z-index: 2;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>Explanation:
.box1has az-indexof 1..box2has az-indexof 2.- Since
.box2has a higherz-index, it will appear on top of.box1.
Example 2: Negative Z-Index
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Negative Z-Index Example</title>
<style>
.box1 {
position: absolute;
top: 50px;
left: 50px;
width: 100px;
height: 100px;
background-color: red;
z-index: -1;
}
.box2 {
position: absolute;
top: 100px;
left: 100px;
width: 100px;
height: 100px;
background-color: blue;
z-index: 1;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>Explanation:
.box1has az-indexof -1..box2has az-indexof 1.- Since
.box1has a negativez-index, it will appear behind.box2.
Practical Exercises
Exercise 1: Stacking Multiple Elements
Task: Create three overlapping boxes with different z-index values and observe their stacking order.
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 Exercise</title>
<style>
.box1 {
position: absolute;
top: 50px;
left: 50px;
width: 100px;
height: 100px;
background-color: red;
z-index: 3;
}
.box2 {
position: absolute;
top: 100px;
left: 100px;
width: 100px;
height: 100px;
background-color: green;
z-index: 1;
}
.box3 {
position: absolute;
top: 150px;
left: 150px;
width: 100px;
height: 100px;
background-color: blue;
z-index: 2;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>Solution:
.box1has the highestz-index(3), so it will be on top..box3has az-indexof 2, so it will be in the middle..box2has the lowestz-index(1), so it will be at the bottom.
Exercise 2: Creating a Stacking Context
Task: Create a stacking context by setting a z-index on a parent element and observe how it affects child elements.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stacking Context Exercise</title>
<style>
.parent {
position: relative;
z-index: 10;
width: 200px;
height: 200px;
background-color: lightgray;
}
.child1 {
position: absolute;
top: 20px;
left: 20px;
width: 100px;
height: 100px;
background-color: red;
z-index: 1;
}
.child2 {
position: absolute;
top: 60px;
left: 60px;
width: 100px;
height: 100px;
background-color: blue;
z-index: 2;
}
</style>
</head>
<body>
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
</body>
</html>Solution:
- The
.parentelement creates a new stacking context with az-indexof 10. - Within this context,
.child2(z-index: 2) will appear on top of.child1(z-index: 1).
Common Mistakes and Tips
- Forgetting to Set Position: The
z-indexproperty only works on positioned elements. Ensure you set thepositionproperty torelative,absolute,fixed, orsticky. - Negative Z-Index: Be cautious with negative
z-indexvalues as they can cause elements to be hidden behind other content. - Stacking Contexts: Remember that each stacking context is independent. Elements within a stacking context are only compared against each other, not elements in other contexts.
Conclusion
The z-index property is a powerful tool for controlling the stacking order of overlapping elements. By understanding how stacking contexts work and how to use z-index effectively, you can create complex and visually appealing layouts. Practice using z-index in different scenarios to become more comfortable with its behavior and nuances.
CSS Mastery: From Beginner to Advanced
Module 1: Introduction to CSS
- What is CSS?
- CSS Syntax and Selectors
- How to Add CSS to HTML
- Basic CSS Properties
- CSS Colors
- CSS Units and Measurements
Module 2: Text and Font Styling
- Text Properties
- Font Properties
- Google Fonts Integration
- Text Alignment and Spacing
- Text Decoration and Transformation
Module 3: Box Model and Layout
- Understanding the Box Model
- Margin and Padding
- Border and Outline
- Width and Height
- Box Sizing
- CSS Display Property
Module 4: Positioning and Floating
- CSS Position Property
- Static, Relative, Absolute, and Fixed Positioning
- CSS Float and Clear
- Creating Layouts with Float
- CSS Z-Index
Module 5: Flexbox
- Introduction to Flexbox
- Flex Container Properties
- Flex Item Properties
- Creating Layouts with Flexbox
- Responsive Design with Flexbox
Module 6: CSS Grid
- Introduction to CSS Grid
- Grid Container Properties
- Grid Item Properties
- Creating Layouts with CSS Grid
- Responsive Design with CSS Grid
Module 7: Advanced CSS Techniques
Module 8: Responsive Design
- Introduction to Responsive Design
- Media Queries
- Responsive Typography
- Responsive Images
- Mobile-First Design
Module 9: Preprocessors and Frameworks
- Introduction to CSS Preprocessors
- Sass Basics
- Less Basics
- Introduction to CSS Frameworks
- Using Bootstrap
