What Is z-index in CSS and Why Is It Not Working?

Learn What Is z-index in CSS and Why Is It Not Working? with practical examples, explanations, best practices, and real-world coding techniques.

Try Editor

What Is z-index in CSS?

The CSS z-index property controls the stacking order of overlapping elements along an imaginary z-axis. While the x-axis represents horizontal placement and the y-axis represents vertical placement, the z-axis represents depth: which element is painted in front of another.

A larger z-index value generally places an element above an element with a smaller stack level when they participate in the same stacking context. z-index accepts positive, zero, and negative integer values, as well as the keyword auto. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/z-index?utm_source=chatgpt.com))

z-index is commonly used for dropdowns, menus, modals, tooltips, badges, sticky headers, floating actions, overlays, and other UI elements that visually overlap.

How Does z-index Work?

Imagine your webpage as a stack of transparent layers. Elements that are painted closer to the viewer appear above elements painted farther away.

Higher stacking level — visually closer
z-index: 10
z-index: 5
z-index: 0
z-index: -1
Lower stacking level — visually farther away

The important detail is that these comparisons happen inside a relevant stacking context. A z-index value is not a universal score for the entire webpage. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Positioned_layout/Stacking_context?utm_source=chatgpt.com))

What Is the Syntax of z-index?

The basic syntax is:

</pre>

        <p class="mt-5">
          Common values include:
        </p>

        <pre class="mt-4 bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.behind {
  z-index: -1;
}

.normal {
  z-index: 0;
}

.above {
  z-index: 10;
}

.default {
  z-index: auto;
}

The auto value does not establish a new local stacking context by itself. An explicit integer z-index establishes a local stacking context for the box in the relevant cases defined by CSS. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/z-index?utm_source=chatgpt.com))

Does z-index Require position: relative?

A common beginner rule says that z-index only works when position is not static. That is useful in many traditional positioning examples, but it is not the complete modern rule.

z-index applies to positioned elements and also to flex items and grid items. So a flex or grid child can participate in z-index stacking without becoming position: relative first. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/z-index?utm_source=chatgpt.com))

</pre>

        <p class="mt-5">
          If you are using ordinary block elements outside Flexbox or Grid, positioning is often
          relevant when you want to use z-index in the traditional positioned-element model.
        </p>
      </section>

      <!-- Simple Example -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          How Do You Use z-index with Overlapping Elements?
        </h2>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code>.box-one {
  position: absolute;
  top: 40px;
  left: 40px;
  z-index: 1;
}

.box-two {
  position: absolute;
  top: 70px;
  left: 70px;
  z-index: 2;
}

When the two boxes overlap in the same stacking context, the element with z-index: 2 is painted above the element with z-index: 1.

z-index: 1
z-index: 2

What Does z-index: auto Mean?

z-index: auto is the initial value. For a positioned element, it places the box at the stack level used by the current stacking context without creating a new local stacking context solely because of z-index. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/z-index?utm_source=chatgpt.com))

</pre>

        <p class="mt-5">
          When several overlapping elements have no explicit z-index, other stacking rules such as
          their positioning and source order can determine which one is painted above another.
        </p>
      </section>

      <!-- Source Order -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          What Happens When z-index Values Are the Same?
        </h2>

        <p class="mb-4">
          Giving several elements the same stacking level does not make them randomly overlap.
          The CSS stacking rules continue to determine their relative order.
        </p>

        <p class="mb-4">
          For example, with otherwise comparable positioned elements in the same stacking context,
          later elements in source order can appear above earlier ones.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.box {
  position: absolute;
  z-index: 1;
}
<div class="box">First</div>
<div class="box">Second</div>
<div class="box">Third</div>

When the relevant stacking conditions are otherwise equivalent, the later box can be painted above the earlier box. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Positioned_layout/Understanding_z-index?utm_source=chatgpt.com))

Can z-index Be Negative?

Yes. Negative integer values can place an element below elements with higher stack levels within the relevant stacking context.

</pre>

        <p class="mt-5">
          Negative z-index values are useful for decorative layers, but they should be used carefully.
          If the stacking context and backgrounds are not understood, an element can appear behind
          a parent's background or become difficult to interact with.
        </p>
      </section>

      <!-- Stacking Context -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          What Is a Stacking Context?
        </h2>

        <p class="mb-4">
          A <strong class="text-white">stacking context</strong> is an independent three-dimensional
          group in which descendant elements are stacked according to the rules for that context.
          A nested stacking context is treated as a single unit when its parent stacking context
          determines its position relative to sibling stacking contexts. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Positioned_layout/Stacking_context?utm_source=chatgpt.com))
        </p>

        <p class="mb-5">
          This is the concept that explains many of the most confusing z-index bugs.
        </p>

        <div class="rounded-xl border border-slate-700 bg-slate-900 p-6">
          <div class="rounded-xl border border-slate-600 p-5">
            <div class="text-sm text-slate-400 mb-4">
              Parent Stacking Context
            </div>

            <div class="space-y-2">
              <div class="rounded-lg border border-slate-600 p-3">
                Child z-index: 1000
              </div>

              <div class="rounded-lg border border-slate-600 p-3">
                Child z-index: 2000
              </div>
            </div>
          </div>

          <div class="text-center my-4 text-slate-500">
            versus
          </div>

          <div class="rounded-xl border border-slate-600 p-5">
            <div class="text-sm text-slate-400 mb-4">
              Sibling Stacking Context
            </div>

            <div class="rounded-lg border border-slate-600 p-3">
              Parent z-index: 2
            </div>
          </div>
        </div>

        <p class="mt-5">
          The child with z-index 2000 cannot simply escape its parent stacking context and beat an
          unrelated sibling stacking context whose parent is already painted above it. The parent
          contexts are compared first.
        </p>
      </section>

      <!-- Stacking Context creators -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          What Creates a Stacking Context?
        </h2>

        <p class="mb-5">
          Several CSS features can create stacking contexts. Common examples include:
        </p>

        <div class="grid md:grid-cols-2 gap-4">

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Root element
            </h3>
            <p class="text-sm">
              The document root establishes the root stacking context.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Positioned element with z-index
            </h3>
            <p class="text-sm">
              An absolute or relative element with a non-auto z-index can create a stacking context.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Fixed and sticky elements
            </h3>
            <p class="text-sm">
              Fixed and sticky positioned elements establish stacking contexts.
            </p>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <h3 class="font-semibold text-white mb-2">
              Other visual and compositing features
            </h3>
            <p class="text-sm">
              Features such as opacity, transforms, filters, containment, and isolation can also
              create stacking contexts.
            </p>
          </div>

        </div>

        <p class="mt-5">
          The exact list is broader than these examples, so when debugging a complex layout,
          inspect ancestors for properties that establish stacking contexts. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Positioned_layout/Stacking_context?utm_source=chatgpt.com))
        </p>
      </section>

      <!-- The Big Mistake -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          Why Does a Higher z-index Sometimes Appear Behind a Lower One?
        </h2>

        <p class="mb-4">
          This is the most important z-index concept.
        </p>

        <p class="mb-4">
          Consider a parent with <code class="text-blue-300">z-index: 1</code> containing a child
          with <code class="text-blue-300">z-index: 9999</code>. Another sibling parent has
          <code class="text-blue-300">z-index: 2</code>.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code>.parent-one {
  position: relative;
  z-index: 1;
}

.child-one {
  position: absolute;
  z-index: 9999;
}

.parent-two {
  position: relative;
  z-index: 2;
}

The child with z-index 9999 does not automatically appear above the second parent. The child is trapped inside the stacking context created by its parent. The two parent stacking contexts are compared at their own level first.

Since the second parent has a higher stacking level than the first parent, its entire stacking context can appear above the first parent's contents. The child's 9999 value cannot escape that context. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Positioned_layout/Understanding_z-index?utm_source=chatgpt.com))

How Should You Think About Nested z-index Values?

Root Context
Parent A — z-index: 1
Child — z-index: 9999
Parent B — z-index: 2
Child — z-index: 1

At the root level, Parent B is above Parent A. The children are then stacked within their respective parents. This is why comparing only the child numbers can produce misleading results.

Does z-index Work on Flex and Grid Items?

Yes. Modern CSS allows z-index to affect flex and grid items even when they are not explicitly positioned. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/z-index?utm_source=chatgpt.com))

</pre>

        <p class="mt-5">
          This is particularly convenient for component layouts where the parent already uses
          Flexbox or Grid and two items need a controlled stacking relationship.
        </p>
      </section>

      <!-- Dropdown Example -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          How Do You Use z-index for a Dropdown Menu?
        </h2>

        <p class="mb-4">
          Dropdown menus often need to appear above nearby content.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.navigation {
  position: relative;
  z-index: 20;
}

.dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 30;
}

However, the dropdown still cannot escape a parent stacking context that is underneath another sibling stacking context. When a dropdown appears behind another section despite having a large z-index, inspect the ancestors rather than simply increasing the number.

How Do You Use z-index for a Modal?

Modals commonly sit above the rest of the page. A typical application-level modal might use:

</pre>

        <p class="mt-5">
          This works when the modal is placed in a suitable stacking context. If an ancestor creates
          an unexpected stacking context, simply increasing the modal's z-index may not solve the problem.
        </p>
      </section>

      <!-- Overlay -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          How Do You Create Layered Overlays with z-index?
        </h2>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.card {
  position: relative;
}

.card-image {
  position: relative;
  z-index: 1;
}

.card-overlay {
  position: absolute;
  inset: 0;
  z-index: 2;
}

.card-content {
  position: relative;
  z-index: 3;
}
Base Layer
Overlay
Content

What Does isolation: isolate Have to Do with z-index?

The isolation property can explicitly create a new stacking context. isolation: isolate is useful when you want a component's stacking behavior to remain independent from surrounding content.

</pre>

        <p class="mt-5">
          This can be useful for component-level decorative layers where you do not want a negative
          z-index child to interact unexpectedly with distant parts of the page.
        </p>
      </section>

      <!-- Top Layer -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          What Is the CSS Top Layer?
        </h2>

        <p class="mb-4">
          Modern browsers also have a special <strong class="text-white">top layer</strong>, which
          sits above the ordinary stacking contexts of the document. Browser-managed features such
          as modal <code class="text-blue-300"><dialog></code> elements and shown popovers can
          use this mechanism. ([developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Glossary/Top_layer?utm_source=chatgpt.com))
        </p>

        <p class="mb-4">
          The top layer is different from simply assigning a very large
          <code class="text-blue-300">z-index</code>. A number such as 999999 does not move an
          ordinary element into the browser's top layer.
        </p>

        <p>
          This distinction is important when building modern overlays because not every layering
          problem should be solved by continuously increasing z-index values.
        </p>
      </section>

      <!-- z-index scale -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          Should You Use z-index: 9999?
        </h2>

        <p class="mb-4">
          There is nothing technically special about
          <code class="text-blue-300">9999</code>. It is simply an integer. Using increasingly large
          numbers does not solve stacking-context problems.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.dropdown {
  z-index: 999999;
}

.modal {
  z-index: 9999999;
}

A better approach is to establish a small, understandable stacking scale for your application.

</pre>

        <p class="mt-5">
          The exact numbers are project-specific. What matters is having a predictable system and
          understanding which stacking context each layer belongs to.
        </p>
      </section>

      <!-- Why not working -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          Why Is z-index Not Working?
        </h2>

        <div class="space-y-5">

          <div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
            <h3 class="text-lg font-semibold text-white mb-2">
              The elements are in different stacking contexts
            </h3>

            <p>
              This is one of the most common causes. A child with z-index 9999 can still appear
              below a sibling belonging to a higher parent stacking context.
            </p>
          </div>

          <div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
            <h3 class="text-lg font-semibold text-white mb-2">
              An ancestor creates a stacking context
            </h3>

            <p>
              Inspect parent and ancestor elements for properties such as positioned z-index,
              opacity, transforms, filters, containment, or isolation.
            </p>
          </div>

          <div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
            <h3 class="text-lg font-semibold text-white mb-2">
              The element is not actually overlapping
            </h3>

            <p>
              z-index only becomes visually relevant when elements occupy overlapping visual space.
              Check positioning, dimensions, margins, and layout first.
            </p>
          </div>

          <div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
            <h3 class="text-lg font-semibold text-white mb-2">
              Overflow is clipping the content
            </h3>

            <p>
              A large z-index does not automatically allow content to escape clipping created by
              an ancestor's overflow behavior.
            </p>
          </div>

          <div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
            <h3 class="text-lg font-semibold text-white mb-2">
              You are comparing z-index values from different contexts
            </h3>

            <p>
              A value such as 100 and another value such as 10,000 are not directly comparable if
              the elements live inside different stacking contexts.
            </p>
          </div>

          <div class="rounded-xl border border-red-900/50 bg-red-950/20 p-5">
            <h3 class="text-lg font-semibold text-white mb-2">
              You are trying to fix everything with a bigger number
            </h3>

            <p>
              Increasing z-index repeatedly can hide the actual architectural problem. Inspect the
              stacking-context hierarchy instead.
            </p>
          </div>

        </div>
      </section>

      <!-- Debugging -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          How Do You Debug z-index Problems?
        </h2>

        <p class="mb-5">
          A reliable debugging process is more useful than randomly changing z-index values.
        </p>

        <div class="space-y-4">

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <div class="flex items-start gap-4">
              <div class="rounded-lg border border-slate-600 px-3 py-2 font-mono text-blue-300">
                01
              </div>

              <div>
                <h3 class="font-semibold text-white">
                  Confirm the elements overlap
                </h3>

                <p class="mt-2">
                  Check their position, dimensions, and layout relationships.
                </p>
              </div>
            </div>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <div class="flex items-start gap-4">
              <div class="rounded-lg border border-slate-600 px-3 py-2 font-mono text-blue-300">
                02
              </div>

              <div>
                <h3 class="font-semibold text-white">
                  Inspect the computed z-index
                </h3>

                <p class="mt-2">
                  Confirm that the expected CSS rule is actually applied.
                </p>
              </div>
            </div>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <div class="flex items-start gap-4">
              <div class="rounded-lg border border-slate-600 px-3 py-2 font-mono text-blue-300">
                03
              </div>

              <div>
                <h3 class="font-semibold text-white">
                  Walk up the ancestor tree
                </h3>

                <p class="mt-2">
                  Look for ancestors that create stacking contexts.
                </p>
              </div>
            </div>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <div class="flex items-start gap-4">
              <div class="rounded-lg border border-slate-600 px-3 py-2 font-mono text-blue-300">
                04
              </div>

              <div>
                <h3 class="font-semibold text-white">
                  Check clipping
                </h3>

                <p class="mt-2">
                  Inspect overflow and clipping properties on ancestors.
                </p>
              </div>
            </div>
          </div>

          <div class="rounded-xl border border-slate-700 bg-slate-900 p-5">
            <div class="flex items-start gap-4">
              <div class="rounded-lg border border-slate-600 px-3 py-2 font-mono text-blue-300">
                05
              </div>

              <div>
                <h3 class="font-semibold text-white">
                  Fix the hierarchy instead of increasing the number
                </h3>

                <p class="mt-2">
                  Move the element to the appropriate stacking context or adjust the parent stacking order.
                </p>
              </div>
            </div>
          </div>

        </div>
      </section>

      <!-- Dropdown Debug -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          Why Does a Dropdown Appear Behind Another Section?
        </h2>

        <p class="mb-4">
          Consider this simplified structure:
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code><section class="content">
  <div class="dropdown">
    Menu
  </div>
</section>

<section class="overlay">
  Overlay
</section>
</pre>

        <p class="mt-5">
          The dropdown may still appear below the overlay because the dropdown is inside the
          stacking context of <code class="text-blue-300">.content</code>. The entire content
          context has a lower stacking level than the overlay context.
        </p>

        <p class="mt-4">
          The correct fix is not necessarily to change
          <code class="text-blue-300">.dropdown</code> from 9999 to 999999. You need to decide
          which parent stacking context should actually be above the other.
        </p>
      </section>

      <!-- Component Design -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          How Should You Manage z-index in Component-Based Applications?
        </h2>

        <p class="mb-4">
          Component-based applications can create many nested layout and stacking contexts.
          A small, deliberate layering system makes debugging much easier.
        </p>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:/* Component layering */
.component {
  position: relative;
  z-index: 0;
}

/* Local overlay */
.component-overlay {
  position: absolute;
  z-index: 1;
}

/* Application-level layers */
.app-header {
  z-index: 10;
}

.app-dropdown {
  z-index: 20;
}

.app-modal {
  z-index: 100;
}

Keep local z-index values local when possible. Reserve higher-level layering values for application-level components such as navigation, dropdown containers, dialogs, and global overlays.

How Does z-index Work in a Flex Layout?

</pre>

        <p class="mt-5">
          Because both elements are flex items, their z-index values can participate in determining
          their stacking order even without setting their position property.
        </p>
      </section>

      <!-- Grid Example -->
      <section>
        <h2 class="text-2xl md:text-3xl font-bold text-white mb-4">
          How Does z-index Work in a Grid Layout?
        </h2>

        <pre class="bg-slate-950 border border-slate-700 rounded-xl p-5 overflow-x-auto text-sm"><code:.layout {
  display: grid;
}

.featured {
  z-index: 2;
}

.background {
  z-index: 1;
}

Grid items can overlap through grid placement, and z-index can then control their stacking relationship when they occupy overlapping areas.

What Are the Best Practices for Using z-index?

Understand stacking contexts

z-index values only have meaning within the appropriate stacking context hierarchy.

Use a small layering scale

Meaningful values such as 10, 20, 30, and 100 are easier to reason about than random huge numbers.

Keep local layering local

Components should avoid unnecessarily competing with global application layers.

Inspect ancestors

When z-index fails, inspect the complete stacking-context hierarchy rather than only the target element.

Avoid unnecessary positioning

Use normal flow, Flexbox, and Grid for layout before introducing absolute positioning just to make layers work.

Treat overlays as an architectural concern

Global modals, popovers, and dropdown systems benefit from a predictable layer strategy.

CSS z-index Quick Reference

Value Meaning
auto Uses the default stack level for the current context and does not create a local stacking context solely because of z-index.
0 Explicit zero stack level in the current stacking context.
positive integer Places the item at a higher stack level relative to lower values in the same context.
negative integer Places the item at a lower stack level relative to higher values in the same context.

What Is a Complete CSS z-index Example?

The following example shows a realistic interface containing a sticky header, dropdown, card overlay, and modal layer.

<div class="app">

  <header class="header">
    <div class="logo">IgKnight</div>

    <div class="menu-wrapper">
      <button>Menu</button>

      <div class="dropdown">
        <a href="#">Documentation</a>
        <a href="#">Practice</a>
      </div>
    </div>
  </header>

  <main>
    <article class="card">
      <img src="course.jpg" alt="Course">

      <span class="badge">New</span>

      <div class="card-content">
        CSS Course
      </div>
    </article>
  </main>

  <div class="modal">
    <div class="modal-content">
      Modal Content
    </div>
  </div>

</div>


        

The important idea is not the specific numbers. The header, dropdown, card internals, and modal each have a deliberate place in the application's layering strategy. The isolation: isolate declaration on the application and card boundaries can also help keep component-level stacking relationships contained.

Key Takeaways

  • z-index controls stacking order along the CSS z-axis.
  • Larger integer values generally appear above smaller values when the elements are participating in the same stacking context.
  • z-index accepts positive, zero, and negative integers, plus auto.
  • z-index applies to positioned elements and can also apply to flex and grid items.
  • A stacking context creates an independent layering environment for its descendants.
  • A child with z-index: 9999 cannot escape a parent stacking context that is below another sibling stacking context.
  • When a z-index change does not work, inspect the element's ancestors, stacking contexts, and clipping behavior.
  • Avoid solving every layering problem with huge z-index numbers. Use a small, predictable layering system.
  • Modern browser-managed features such as modal dialogs and popovers can use the top layer, which is separate from ordinary z-index stacking.
  • The best z-index strategy is based on understanding the layout hierarchy rather than repeatedly increasing numeric values.

Code Examples

Basic z-index Layering

css
.one {
  position: absolute;
  z-index: 1;
}

.two {
  position: absolute;
  z-index: 2;
}
    

Component Overlay

css
.card {
  position: relative;
  isolation: isolate;
}

.badge {
  position: absolute;
  top: 1rem;
  right: 1rem;
  z-index: 2;
}

.content {
  position: relative;
  z-index: 1;
}
    

Application Layer Scale

css
.header {
  position: sticky;
  top: 0;
  z-index: 10;
}

.dropdown {
  position: absolute;
  z-index: 20;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
}
    

Why 9999 Does Not Always Win

css
.parent-one {
  position: relative;
  z-index: 1;
}

.child {
  position: absolute;
  z-index: 9999;
}

.parent-two {
  position: relative;
  z-index: 2;
}

/*
The child cannot escape parent-one's
stacking context.
*/
    

Dropdown and Modal Layers

html
<div class="app">
  <header class="header">
    <div class="menu-wrapper">
      <button>Menu</button>

      <div class="dropdown">
        Documentation
      </div>
    </div>
  </header>

  <main>
    Page Content
  </main>

  <div class="modal">
    <div class="modal-content">
      Modal Content
    </div>
  </div>
</div>

<style>
.app {
  isolation: isolate;
}

.header {
  position: sticky;
  top: 0;
  z-index: 10;
}

.menu-wrapper {
  position: relative;
}

.dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  z-index: 20;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
}

.modal-content {
  position: relative;
  z-index: 101;
}
</style>
    

Pro Tip

When z-index is not working, do not increase 9999 to 99999 immediately. First inspect the nearest stacking contexts above both elements. Compare the parent stacking contexts before comparing the children. This single habit solves a large percentage of real-world z-index bugs.

More CSS lessons

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.