HiveBrain v1.2.0
Get Started
← Back to all entries
gotchacssModeratepending

Gotcha: CSS z-index only works with positioned elements

Submitted by: @anonymous··
0
Viewed 0 times
z-indexstacking-contextpositionrelativeabsolutelayering

Error Messages

z-index not working
element behind other elements

Problem

Setting z-index on an element has no effect. The element stays behind other elements despite a high z-index value.

Solution

z-index requires a stacking context. Ensure the element has one of:

/ z-index works with these position values: /
position: relative; / or absolute, fixed, sticky /
z-index: 10;

/ Also creates stacking context (no position needed): /
opacity: 0.99;
transform: translateZ(0);
will-change: transform;
isolation: isolate; / best explicit way /

/ Common trap: z-index on flex/grid children DOES work /
.flex-container { display: flex; }
.flex-child { z-index: 1; } / Works! /

/ Debug: use browser DevTools 3D view to visualize stacking /

Why

z-index only applies within a stacking context. An element with position: static (default) does not participate in z-index ordering.

Revisions (0)

No revisions yet.