CSS Style zIndex Property: CSS Z-Index

June 19, 2025

CSS z-index Property: Controlling Element Stacking

The z-index property in CSS controls the vertical stacking order of elements that overlap. In other words, it specifies which element should appear in front of or behind other elements. This is particularly useful when dealing with positioned elements (elements with a position value of relative, absolute, fixed, or sticky). Understanding z-index is crucial for creating complex layouts and managing the visual hierarchy of elements on your webpage.

Understanding Stacking Order

By default, HTML elements are stacked in the order they appear in the document (from top to bottom). When elements overlap, the element that appears later in the HTML will generally be displayed on top. However, the z-index property allows you to override this default behavior and explicitly control the stacking order.

Syntax

The z-index property accepts integer values, both positive and negative, as well as the auto keyword.

/* Keyword value */
z-index: auto;

/* Integer values */
z-index: 0;
z-index: 3;
z-index: 285;
z-index: -1;

/* Global values */
z-index: inherit;
z-index: initial;
z-index: revert;
z-index: revert-layer;
z-index: unset;

Possible Values

Value Description
`auto` The element’s stacking order is the same as its parent. This is the default value. The element does not establish a new stacking context unless it is the root element.
`` An integer value that determines the stacking order of the element within its stacking context. An element with a higher `z-index` value will be displayed in front of elements with lower values. The value can be positive, negative, or zero.
`inherit` Inherits the `z-index` value from its parent element.
`initial` Sets the property to its default value (`auto`).
`unset` Resets the property to its inherited value if it inherits from its parent or to its initial value if not.

Note: z-index only works on positioned elements (position: relative, position: absolute, position: fixed, or position: sticky). If you try to use z-index on an element with position: static, it will have no effect. ⚠️

Basic Examples

Let’s start with some basic examples to illustrate how the z-index property works.

Example 1: Basic Stacking

In this example, we have two positioned div elements that overlap. By setting the z-index of one element higher than the other, we control which element appears on top.

<style>
  .container_basic_stacking {
    position: relative;
    width: 200px;
    height: 150px;
    margin: 20px;
  }

  .box_basic_stacking {
    position: absolute;
    width: 100px;
    height: 100px;
  }

  .box1_basic_stacking {
    background-color: rgba(255, 0, 0, 0.5); /* Red with transparency */
    top: 0;
    left: 0;
    z-index: 1;
  }

  .box2_basic_stacking {
    background-color: rgba(0, 0, 255, 0.5); /* Blue with transparency */
    top: 20px;
    left: 30px;
    z-index: 2;
  }
</style>

<div class="container_basic_stacking">
  <div class="box_basic_stacking box1_basic_stacking">Box 1 (z-index: 1)</div>
  <div class="box_basic_stacking box2_basic_stacking">Box 2 (z-index: 2)</div>
</div>

The blue box (Box 2) has a higher z-index than the red box (Box 1), so it appears on top.

Example 2: Negative z-index

A negative z-index value can be used to position an element behind its parent.

<style>
  .container_negative_z {
    position: relative;
    width: 200px;
    height: 150px;
    margin: 20px;
    background-color: #eee;
  }

  .box_negative_z {
    position: absolute;
    width: 100px;
    height: 100px;
    top: 50px;
    left: 50px;
  }

  .box1_negative_z {
    background-color: rgba(255, 0, 0, 0.5); /* Red with transparency */
    z-index: -1;
  }

  .box2_negative_z {
    background-color: rgba(0, 0, 255, 0.5); /* Blue with transparency */
    z-index: 0;
  }
</style>

<div class="container_negative_z">
  <div class="box_negative_z box1_negative_z">Box 1 (z-index: -1)</div>
  <div class="box_negative_z box2_negative_z">Box 2 (z-index: 0)</div>
</div>

In this case, Box 1 (red) has a z-index of -1, so it appears behind its parent container, while Box 2 (blue) with z-index 0 appears in front.

Understanding Stacking Contexts

A stacking context is a hierarchical representation of elements along the z-axis. It’s crucial to understand stacking contexts to effectively use z-index.

What Creates a Stacking Context?

A new stacking context is created when an element meets one of the following conditions:

  • The root element of the document (<html>).
  • An element with a position value (relative, absolute, fixed, or sticky) and a z-index value other than auto.
  • An element with position: fixed or position: sticky (even with z-index: auto).
  • An element that is a child of a flex or grid container with a z-index value other than auto.
  • An element with opacity less than 1 (See the specification for opacity).
  • An element with a transform value other than none.
  • An element with a filter value other than none.
  • An element with a isolation value of isolate.
  • An element with a mix-blend-mode value other than normal.
  • An element with a contain value of layout, paint, or composite.
  • An element with a will-change value that specifies any property that would create a stacking context.
  • An element with view-transition-name value other than none.

Example 3: Stacking Contexts

<style>
  .container_stacking_context {
    position: relative;
    width: 300px;
    height: 200px;
    margin: 20px;
    padding: 10px;
    border: 1px solid #000;
  }

  .outer_box_stacking_context {
    position: relative;
    width: 150px;
    height: 100px;
  }

  .outer1_stacking_context {
    background-color: rgba(255, 0, 0, 0.5); /* Red with transparency */
    z-index: 1;
  }

  .outer2_stacking_context {
    background-color: rgba(0, 255, 0, 0.5); /* Green with transparency */
    top: -30px;
    left: 50px;
    z-index: 2;
  }

  .inner_box_stacking_context {
    position: absolute;
    width: 70px;
    height: 50px;
    top: 20px;
    left: 20px;
  }

  .inner1_stacking_context {
    background-color: rgba(0, 0, 255, 0.5); /* Blue with transparency */
    z-index: 3;
  }

  .inner2_stacking_context {
    background-color: rgba(255, 255, 0, 0.5); /* Yellow with transparency */
    z-index: 1;
  }
</style>

<div class="container_stacking_context">
  <div class="outer_box_stacking_context outer1_stacking_context">
    Outer Box 1 (z-index: 1)
    <div class="inner_box_stacking_context inner1_stacking_context">
      Inner Box 1 (z-index: 3)
    </div>
  </div>
  <div class="outer_box_stacking_context outer2_stacking_context">
    Outer Box 2 (z-index: 2)
    <div class="inner_box_stacking_context inner2_stacking_context">
      Inner Box 2 (z-index: 1)
    </div>
  </div>
</div>

In this example, .outer1_stacking_context and .outer2_stacking_context each create a new stacking context because they have a position of relative and a z-index other than auto.

  • .outer2_stacking_context appears on top of .outer1_stacking_context because it has a higher z-index (2 vs. 1).

  • Within each stacking context, the z-index values of the inner boxes are relative to their respective outer boxes. Thus, even though .inner1_stacking_context has a z-index of 3 and .inner2_stacking_context has a z-index of 1, .inner1_stacking_context is only on top of .inner2_stacking_context within its own stacking context and not globally.

Common Pitfalls and Best Practices

  • Only works on positioned elements: Remember that z-index only applies to elements with a position value of relative, absolute, fixed, or sticky.
  • Stacking contexts matter: Understanding stacking contexts is crucial. An element’s z-index only has meaning within its stacking context.
  • Avoid excessive z-index values: Using very large or arbitrary z-index values can make your CSS difficult to maintain. It’s better to use a logical and manageable range of values.
  • Be mindful of inheritance: z-index is not inherited, but stacking contexts are.
  • Test across browsers: Always test your layouts in different browsers to ensure consistent rendering, especially when using z-index.

Real-World Applications of z-index

  • Modal windows and dialogs: Placing a modal window on top of the rest of the page content.
  • Dropdown menus: Ensuring that dropdown menus appear above other elements on the page.
  • Image sliders: Controlling the stacking order of images in a slider or carousel.
  • Tooltips: Displaying tooltips above the elements they are associated with.
  • Overlapping content: Managing the stacking order of elements in complex layouts with overlapping sections.

Use Case Example: Creating a Simple Modal

Let’s create a simple modal window using z-index to ensure it appears on top of the page content.

<style>
  .container_modal_example {
    position: relative;
    width: 100%;
    height: 300px;
    background-color: #f0f0f0;
  }

  .page_content_modal_example {
    padding: 20px;
  }

  .modal_modal_example {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 300px;
    padding: 20px;
    background-color: #fff;
    border: 1px solid #ccc;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
    z-index: 1000; /* Ensure the modal is on top */
    display: none; /* Initially hidden */
  }

  .overlay_modal_example {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 999; /* Below the modal but above other content */
    display: none; /* Initially hidden */
  }

  .show_modal_button_modal_example {
    padding: 10px 20px;
    background-color: #007bff;
    color: #fff;
    border: none;
    cursor: pointer;
  }
</style>

<div class="container_modal_example">
  <div class="page_content_modal_example">
    <h1>Page Content</h1>
    <p>This is some example page content.</p>
    <button class="show_modal_button_modal_example" onclick="showModalFunction()">
      Show Modal
    </button>
  </div>

  <div id="myModal" class="modal_modal_example">
    <h2>Modal Window</h2>
    <p>This is the content of the modal window.</p>
    <button onclick="hideModalFunction()">Close</button>
  </div>

  <div id="overlay" class="overlay_modal_example"></div>
</div>

<script>
  function showModalFunction() {
    document.getElementById("myModal").style.display = "block";
    document.getElementById("overlay").style.display = "block";
  }

  function hideModalFunction() {
    document.getElementById("myModal").style.display = "none";
    document.getElementById("overlay").style.display = "none";
  }
</script>

In this example:

  • The .modal_modal_example has a z-index of 1000, ensuring it is on top.
  • The .overlay_modal_example has a z-index of 999, placing it below the modal but above other content.
  • JavaScript is used to show/hide the modal and overlay when the button is clicked.

This example demonstrates how z-index is essential for creating UI components that need to appear on top of other content.

Conclusion

The z-index property is a powerful tool for controlling the stacking order of elements in CSS. By understanding how z-index works, along with the concept of stacking contexts, you can create complex and visually appealing layouts. Always remember to use z-index judiciously, test your layouts across different browsers, and keep your CSS organized for maintainability. Happy styling!