🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Expert Masterclasses.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

CSS Z-Index

Control the stacking order of elements. Master layering, stacking contexts, and overlapping.

style.css
.popup {
position: fixed;
z-index: 9999;
background: white;
}
.overlay {
z-index: 9998;
}
z: 10
layers.css
1 / 6
🥞

Tutor:The 'z-index' property specifies the stack order of an element. Elements with a higher stack order are always in front of an element with a lower stack order.


Stacking Tree

Unlock nodes by mastering Z-Index, Contexts, and Layers.

Concept 1: The Z-Axis

The z-index property accepts integer values (e.g., 1, 100, -5). Elements with higher values appear closer to the user, covering elements with lower values.

System Check

Which element will appear on top?


Community Stacks

Share Your Overlays

Built a complex layered component or fixed a stacking context bug? Share your solution.

Enjoying this guide?

Codesyllabus is 100% free and open-source. Support our mission, pay for server infrastructure, and fuel new tutorials by buying us a coffee!

CSS Z-Index

Author

Pascual Vila

Frontend Instructor.

The z-index property controls the vertical stacking order of elements that overlap. Think of the web page as a 3D environment; z-index determines what is closer to the viewer (higher value) and what is further away (lower value).

Prerequisites

Crucially, z-index only affects elements that have a position value other than static (e.g., relative, absolute, fixed, or sticky). It also works on flex children and grid children.

Stacking Contexts

A Stacking Context is a three-dimensional conceptualization of HTML elements along an imaginary z-axis relative to the user. Elements within a stacking context are stacked in order.

The most important rule: A child element is limited to the stacking level of its parent. If Parent A has z-index: 1 and Parent B has z-index: 2, a child of Parent A with z-index: 9999 will still appear behind Parent B.

Values

  • Auto: The default. The box does not establish a new local stacking context.
  • Integer: A number (positive, negative, or zero). Higher numbers are closer to the screen.

Z-Index Glossary

Stacking Context
A group of elements that are stacked together. Elements inside a context cannot escape it to appear above elements in a higher context.
Z-Axis
The imaginary line extending from the computer screen towards your face.
Natural Stacking Order
Without z-index, elements stack based on their order in the DOM (later siblings cover earlier ones) and their computed position property.