Detailed Explanation of the z-index Property in CSS
Description
The z-index property in CSS controls the stacking order of elements along the axis perpendicular to the screen (the Z-axis). It determines which element appears in front and which is obscured behind when elements overlap.
Core Concepts
- Stacking Context: z-index only takes effect within a specific "stacking context."
- Default Order: Elements that appear later in the HTML will overlay those that appear earlier.
- Numerical Property: A higher z-index value brings the element to the front; values can be positive, negative, or auto.
Basic Usage of z-index
.element1 {
position: relative;
z-index: 10; /* Appears in front */
}
.element2 {
position: relative;
z-index: 5; /* Appears behind */
}
Detailed Explanation of Key Points
1. Prerequisites for z-index to Take Effect
z-index only works on positioned elements (where the position value is not static):
- position: relative/absolute/fixed/sticky
- Children of flex containers (flex)
- Children of grid containers (grid)
2. Creation of a Stacking Context
A new stacking context is created under the following conditions:
- The root element (html)
- Elements with position set to absolute/relative and z-index not set to auto
- Elements with position set to fixed/sticky
- Children of flex/grid containers with z-index not set to auto
- Elements with opacity less than 1
- Elements with properties like transform, filter, perspective, clip-path not set to none
3. Stacking Rules (from back to front)
- Background and borders of the stacking context
- Child stacking contexts with negative z-index
- Non-positioned block-level elements in normal flow
- Non-positioned floated elements
- Non-positioned inline elements in normal flow
- Positioned elements with z-index set to auto or 0
- Child stacking contexts with positive z-index
4. Practical Application Example
<div class="parent">
<div class="child1">Child Element 1</div>
<div class="child2">Child Element 2</div>
</div>
.parent {
position: relative;
z-index: 1; /* Creates a stacking context */
}
.child1 {
position: absolute;
z-index: 100; /* Compares within the parent's stacking context */
}
.child2 {
position: absolute;
z-index: 50; /* Still appears behind child1 */
}
5. Solutions to Common Issues
Issue 1: z-index Not Taking Effect
Cause: The element is not positioned or is in a different stacking context.
Solution: Ensure the element has a positioning property and is compared within the same stacking context.
Issue 2: Parent Element Obscuring Child Element
Cause: The parent element creates a stacking context with a relatively low z-index.
Solution: Adjust the parent element's z-index or reorganize the HTML structure.
6. Best Practices
- Avoid excessive use of high z-index values; use meaningful numerical ranges instead.
- Use CSS variables to manage z-index layers.
- Pay attention to the creation of stacking contexts to avoid unexpected layering relationships.
By understanding how z-index works and the concept of stacking contexts, you can better control the display layering of page elements.