CSS Z-index

The z-index property in CSS controls the stacking order of elements on a web page. It determines which elements appear in front of or behind others when they overlap. The z-index property only works on elements that have a position value other than static (i.e., relative, absolute, fixed, or sticky).

Syntax

element {
    z-index: value;
}

Try It Now

Property Values

  • auto:
    • The default value. The stack level is determined by the element’s order in the document.
    • Example: z-index: auto;
  • number:
    • Specifies the stack level of the element. Higher values place the element closer to the viewer, in front of elements with lower values.
    • Positive and negative values are allowed.
    • Example: z-index: 1;, z-index: -1;
  • inherit:
    • Inherits the z-index value from the parent element.
    • Example: z-index: inherit;

How z-index Works

The z-index property affects the stacking order of elements. Elements with a higher z-index value appear in front of those with a lower value. If two elements have the same z-index value, their order in the HTML document determines which one is displayed on top.

Examples

Example 1: Basic z-index Usage

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        .box1 {
            position: absolute;
            left: 50px;
            top: 50px;
            width: 100px;
            height: 100px;
            background-color: red;
            z-index: 1;
        }
        .box2 {
            position: absolute;
            left: 70px;
            top: 70px;
            width: 100px;
            height: 100px;
            background-color: blue;
            z-index: 2;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
</body>
</html>

Try It Now

In this example, the blue box (box2) has a higher z-index than the red box (box1), so it appears on top.

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">
    <style>
        .box1 {
            position: absolute;
            left: 50px;
            top: 50px;
            width: 100px;
            height: 100px;
            background-color: red;
            z-index: -1;
        }
        .box2 {
            position: absolute;
            left: 70px;
            top: 70px;
            width: 100px;
            height: 100px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
</body>
</html>

Try It Now

In this example, the red box (box1) has a negative z-index, so it appears behind the blue box (box2).

Practical Use Cases

  • Modals and Overlays: Ensuring modals or overlays appear above other content.
  • Dropdowns and Menus: Making dropdown menus appear above other elements.
  • Tooltips: Displaying tooltips above other content.
  • Complex Layouts: Managing stacking contexts in complex web designs.

Stacking Context

The z-index property works within a stacking context. A stacking context is a group of elements with a specific stacking order, and it is created in certain situations:

  • An element with a position value other than static and a z-index value other than auto.
  • Elements with certain CSS properties like opacity less than 1, transform, filter, or perspective.

Elements within a stacking context are stacked according to their z-index values relative to each other and the parent stacking context.

Example of Stacking Context

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        .parent {
            position: relative;
            z-index: 1;
            background-color: yellow;
            width: 200px;
            height: 200px;
        }
        .child {
            position: absolute;
            z-index: 2;
            background-color: green;
            width: 100px;
            height: 100px;
        }
        .sibling {
            position: absolute;
            z-index: 1;
            background-color: red;
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body>
    <div class="parent">
        <div class="child"></div>
        <div class="sibling"></div>
    </div>
</body>
</html>

Try It Now

In this example, the green box (child) has a higher z-index than the red box (sibling), so it appears above it within the parent stacking context.

Summary

The z-index property is essential for managing the stacking order of elements, especially in complex layouts where elements overlap. By understanding how z-index and stacking contexts work, you can ensure that elements are displayed in the correct order, enhancing both the functionality and aesthetics of your web design.