freecoding.school100% FREE · NO SIGNUP
Grid CanyonISSUE #4 of 27

z-index · stacking contexts

FlexboVSThe Float Phantom
Flexbo saysz-index stacks positioned elements front-to-back — but only within their stacking context.

z-index orders overlapping positioned elements (higher = front). The catch: it only compares siblings within the same stacking context. Properties like opacity < 1, transform, and position:fixed create new contexts — why a huge z-index sometimes "doesn't work."

Power-ups you unlock

The Float Phantom attacks — common mistakes

Boss battleOverlap two positioned boxes and bring the back one forward with z-index.

Example code

<!doctype html><html><head><meta charset="utf-8">
<style>div{position:absolute;width:90px;height:90px}.a{background:#b388ff;z-index:2}.b{background:#8aff9d;left:50px;top:30px;z-index:1}</style></head><body style="background:#06040d;padding:20px"><div class="a"></div><div class="b"></div></body></html>
▶ Open the interactive comic issue
‹ Position · Static · Relative · Absolute · Fixed · StickyOverflow · Visible · Hidden · Scroll · Auto ›