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

  1. Stacking Context: A stacking context is formed when an element is positioned and has a z-index value other than auto. Elements within a stacking context are stacked according to their z-index values.
  2. Positioned Elements: For z-index to work, the element must have its position property set to relative, absolute, fixed, or sticky.
  3. 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:

  • .box1 has a z-index of 1.
  • .box2 has a z-index of 2.
  • Since .box2 has a higher z-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:

  • .box1 has a z-index of -1.
  • .box2 has a z-index of 1.
  • Since .box1 has a negative z-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:

  • .box1 has the highest z-index (3), so it will be on top.
  • .box3 has a z-index of 2, so it will be in the middle.
  • .box2 has the lowest z-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 .parent element creates a new stacking context with a z-index of 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-index property only works on positioned elements. Ensure you set the position property to relative, absolute, fixed, or sticky.
  • Negative Z-Index: Be cautious with negative z-index values 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

Module 2: Text and Font Styling

Module 3: Box Model and Layout

Module 4: Positioning and Floating

Module 5: Flexbox

Module 6: CSS Grid

Module 7: Advanced CSS Techniques

Module 8: Responsive Design

Module 9: Preprocessors and Frameworks

Module 10: Best Practices and Optimization

Module 11: Project: Building a Responsive Website

© Copyright 2024. All rights reserved