Currently Empty: ₹0.00
CSS Z-Index
CSS CODES & EXAMPLES
CSS Z-Index
Learn how the CSS z-index property controls the stacking
order of overlapping elements. Understand layers, overlays, dropdowns,
modals, sticky headers and stacking contexts with practical examples.
01
Introduction to CSS Z-Index
Control the stacking order of elements
The CSS z-index property determines which positioned
elements appear in front of or behind other elements when they overlap.
A higher stacking value normally places an element above an element
with a lower stacking value.
Basic Syntax:
z-index: 10;
02
How z-index Works
Understand stacking order
When elements overlap, CSS uses stacking rules to determine which
element should appear on top. The z-index property can
change this order for elements that participate in a stacking context.
z-index: 1
z-index: 3
z-index: 2
z-index.css
.box-one {
position: relative;
z-index: 1;
}
.box-two {
position: relative;
z-index: 2;
}
.box-three {
position: relative;
z-index: 3;
}
03
Positive z-index
Bring elements to the front
Positive z-index values such as 1,
10 or 100 can be used to place an element
above another element in the same stacking context.
Background Layer
z-index: 10
positive-z-index.css
.back {
position: relative;
z-index: 1;
}
.front {
position: relative;
z-index: 10;
}
04
Negative z-index
Move an element behind other content
Negative values can place an element behind other elements in its
stacking context. For example, z-index: -1 can be used
for decorative background layers.
z-index: -1
Content Layer
negative-z-index.css
.background {
position: absolute;
z-index: -1;
}
.content {
position: relative;
z-index: 1;
}
05
z-index: 0
The common base stacking level
A value of 0 establishes a normal numeric stacking level
within the relevant stacking context.
z-index: 0
Base Layer
This element uses a zero stacking level.
06
Stacking Order
Which element appears first?
When positioned elements overlap, their stacking order is affected by
stacking contexts and their z-index values. Higher
stacking levels generally appear above lower levels within the same
context.
5
Top Layer
4
Above Middle
3
Middle Layer
2
Lower Layer
1
Base Layer
07
Position + Z-Index
Common combination
A common pattern is to combine position with
z-index. This is frequently used for menus, overlays,
badges, popups and navigation components.
Background
Front Element
position-zindex.css
.element {
position: relative;
z-index: 20;
}
08
Overlapping Elements
Create layered designs
1
2
3
overlap.css
.circle-one {
position: absolute;
z-index: 1;
}
.circle-two {
position: absolute;
z-index: 2;
}
.circle-three {
position: absolute;
z-index: 3;
}
09
Image Overlay
Place text above an image
image-overlay.css
.image {
position: relative;
}
.overlay {
position: absolute;
inset: 0;
z-index: 2;
}
10
Dropdown Menu
Keep menus above page contentWebsite Content
The dropdown appears above the content because it has a higher stacking level.
dropdown.css
.navbar {
position: relative;
z-index: 100;
}
.dropdown {
position: absolute;
z-index: 1000;
}
11
Sticky Header
Keep navigation above contentSNCEC
z-index: 999
Sticky Header Example
A sticky header can be combined with a high z-index to ensure that page content does not appear over it.
This technique is useful for educational websites, dashboards, portals and documentation pages.
Scroll inside this area to test the sticky effect.
sticky-header.css
.header {
position: sticky;
top: 0;
z-index: 999;
}
12
Modal / Popup
Place a popup above everythingWebsite Content
Normal page content appears underneath the popup.
Welcome!
This modal uses a high z-index to appear above the page.
modal.css
.modal-backdrop {
position: fixed;
inset: 0;
z-index: 9999;
}
.modal-box {
position: relative;
z-index: 10000;
}
13
Floating Button
Keep an action button above contentFloating Action
A fixed button with a high z-index can stay visible above other page components.
floating-zindex.css
.button {
position: fixed;
right: 25px;
bottom: 25px;
z-index: 5000;
}
14
Multiple Layers
Build a multi-layer interface
Background
Middle
Top
MAIN CONTENT
15
Stacking Context
Understand independent stacking layersA stacking context is a self-contained layer in the browser’s rendering order. Properties such as positioned elements with a non-auto z-index, opacity below 1, transforms and certain other CSS properties can create stacking contexts.
Context A
z-index: 10
Child
Context B
z-index: 20
Child
Important:A child cannot escape the stacking order of its parent stacking
context simply by receiving a very large
z-index value.
16
Common Z-Index Problems
Why your z-index may not work01
Missing Positioning
Check whether the element participates in the positioning context you expect.
02
Wrong Stacking Context
A parent stacking context can limit how its children stack against elements outside it.
03
Overflow Hidden
An ancestor with overflow: hidden may clip an
element even when its z-index is high.
04
Comparing Wrong Values
A value like 9999 does not automatically beat every other stacking context.
17
Responsive Z-Index
Manage layers on smaller screens
Responsive designs sometimes require different stacking priorities.
Media queries can change z-index values when the layout
changes.
z-index: 20
Responsive Layer
The stacking level can be adjusted for different screen sizes.
responsive-zindex.css
.menu {
position: relative;
z-index: 20;
}
@media (max-width: 600px) {
.menu {
z-index: 1000;
}
}
18
Live CSS Runner
Run a real CSS Z-Index exampleLIVE PREVIEW
19
Practice Task
Test your CSS Z-Index knowledge🎯 Your Challenge
Create a modern course card and layered website interface using
CSS z-index.
- Create three overlapping boxes with different z-index values.
- Create an image overlay.
- Create a dropdown menu above page content.
- Create a sticky header with a high z-index.
- Create a modal popup above the complete page.
- Add a floating action button.
- Test positive and negative z-index values.
- Experiment with stacking contexts.
20
CSS Z-Index Quick Reference
Important concepts at a glancez-index: auto
Default stacking behavior.
z-index: 0
Numeric base stacking level.
z-index: 1
Places the element above lower levels in the same context.
z-index: 999
Commonly used for important navigation layers.
z-index: 9999
Often used for high-priority UI such as modals.
z-index: -1
Can place an element behind other content.
position
Frequently used with z-index for layered layouts.
stacking context
An independent layer that controls descendant stacking.



