Settings
Blue Element (Element 1)
1
50px
50px
200px
200px
Red Element (Element 2)
2
100px
100px
200px
200px
Preview
Element 1
z-index: 1
Element 2
z-index: 2
Generated CSS Code
.element-1 {
  position: absolute;
  top: 50px;
  left: 50px;
  width: 200px;
  height: 200px;
  z-index: 1;
}

.element-2 {
  position: absolute;
  top: 100px;
  left: 100px;
  width: 200px;
  height: 200px;
  z-index: 2;
}
How to use?

Adjust the settings on the left to see how z-index affects the stacking order of overlapping elements. The preview will show how the elements will appear with the applied settings.

  • Z-Index: Change the z-index value to control which element appears on top
  • Position: Adjust the top and left position of each element
  • Size: Modify the width and height of each element
  • Show Z-Index Labels: Toggle visibility of z-index values on the elements
About Z-Index Checker

What is Z-Index?

Z-index is a CSS property that specifies the stack order of positioned elements that overlap. An element with a higher z-index value will appear in front of elements with lower z-index values. The z-index property only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky).

Understanding Stacking Order

The stacking order in CSS determines which elements appear on top of others when they overlap. By default, elements are stacked in the order they appear in the HTML document, with later elements appearing on top of earlier ones. The z-index property allows you to control this stacking order explicitly.

Basic Rules of Z-Index

  • Higher Values on Top: Elements with higher z-index values appear on top of elements with lower values
  • Positioned Elements Only: Z-index only affects elements with a position value other than static (the default)
  • Integer Values: Z-index accepts positive, negative, and zero integer values
  • Parent-Child Relationship: Child elements cannot be stacked above their parent elements unless the parent has a z-index value

Stacking Contexts

A stacking context is formed by certain CSS properties that create a hierarchical structure for the z-index property. Within a stacking context, child elements are stacked relative to each other, but the entire context is considered as a single unit in the parent stacking context.

Creating a Stacking Context

A stacking context can be created by any of the following:

  • The root element (HTML)
  • An element with a position value of absolute or relative and a z-index value other than auto
  • An element with a position value of fixed or sticky
  • An element with opacity less than 1
  • An element with a transform, filter, perspective, clip-path, mask, mix-blend-mode, or isolation property
  • An element with a will-change property specifying any property that would create a stacking context

Common Z-Index Issues and Solutions

Issue: Element Not Appearing on Top Despite Higher Z-Index

Solution: Check if the element is positioned (has position: relative, absolute, fixed, or sticky). Also, verify if the element is within a different stacking context than the element it should appear above.

Issue: Z-Index Seems to Have No Effect

Solution: Ensure the element has a position value other than static. Also, check if the element is a child of a stacking context with a lower z-index than competing elements.

Issue: Need Very High Z-Index Values

Solution: If you find yourself using very high z-index values (like 9999), it's often a sign of poor structure. Instead, reorganize your HTML and CSS to create proper stacking contexts.

Best Practices for Using Z-Index

  • Use Small, Consistent Values: Keep z-index values small and consistent (e.g., increments of 10) to make your code more maintainable
  • Create a Z-Index Scale: Define a scale for your project (e.g., modals: 1000, dropdowns: 500, etc.) and stick to it
  • Avoid Using Negative Values: While negative z-index values are valid, they can lead to unexpected behavior
  • Be Aware of Stacking Contexts: Understand how stacking contexts affect the behavior of z-index
  • Document Your Z-Index Usage: Comment your code or create a separate document explaining your z-index strategy

Browser Compatibility

The z-index property is well-supported across all modern browsers. However, be aware that different browsers may have different default stacking orders for certain elements, which can sometimes lead to inconsistent behavior.