-
Hajipur, Bihar, 844101
The CSS z-index property controls the stacking order of elements on a webpage. When elements overlap, z-index determines which element appears on top. This property is essential for creating layered designs, dropdown menus, modals, tooltips, and interactive interfaces. In this tutorial, you will learn how z-index works, its relationship with positioning, practical examples, and best practices.
The z-index property defines the stack level of an element. Elements with higher z-index values are displayed in front of elements with lower values. By default, elements have a stack level of auto, meaning they follow the natural order of the document.
z-index only works on elements that have a positioning property other than static. This includes relative, absolute, fixed, and sticky.
Understanding z-index is important because it:
Controls which elements appear on top of others
Helps in designing dropdown menus and tooltips
Is crucial for modals and popups
Ensures layered elements like sliders and cards display correctly
Improves user experience by preventing visual overlaps
Without proper z-index, overlapping elements can look broken or confusing.
By default, elements have z-index: auto. This means elements follow the HTML source order. Elements declared later in the HTML appear on top of earlier elements if they overlap.
<div class="box1">Box 1</div>
<div class="box2">Box 2</div>
.box1, .box2 {
position: relative;
width: 100px;
height: 100px;
}
.box1 { background-color: red; top: 50px; left: 50px; }
.box2 { background-color: blue; top: 70px; left: 70px; }
In this case, .box2 appears on top because it comes later in the HTML.
z-index accepts integer values (positive, negative, or zero).
Positive values bring elements forward
Zero is neutral
Negative values push elements backward
.box1 { position: relative; z-index: 1; }
.box2 { position: relative; z-index: 5; }
.box3 { position: relative; z-index: -1; }
Here, .box2 appears on top, .box1 in the middle, and .box3 behind both.
z-index only works on elements with a positioning property other than static.
div {
z-index: 10; /* has no effect if position is static */
}
div {
position: relative;
z-index: 10; /* now it works */
}
Always ensure an element has position: relative, absolute, fixed, or sticky to use z-index.
z-index is often used to ensure modals appear above all other content.
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 1000;
background-color: white;
padding: 20px;
box-shadow: 0 0 10px rgba(0,0,0,0.5);
}
High z-index ensures the modal overlays the rest of the page content.
Dropdown menus require a higher z-index to appear above other elements.
.nav-item:hover .dropdown {
display: block;
position: absolute;
z-index: 500;
}
This ensures the dropdown menu appears in front of other page elements.
Negative z-index values push elements behind others, useful for background layers.
.background {
position: relative;
z-index: -1;
}
This places the background layer behind content without changing the document flow.
Using z-index on static elements (has no effect)
Ignoring parent stacking contexts
Using excessively high or inconsistent values
Not testing on overlapping elements
Avoiding these mistakes ensures a predictable layout.
A stacking context is created by an element with position and z-index. Children are stacked within this context. This can affect overlapping behavior.
.parent {
position: relative;
z-index: 10;
}
.child {
position: absolute;
z-index: 20;
}
The child cannot escape the parent’s stacking context when interacting with elements outside the parent.
Use the smallest necessary values for clarity
Create stacking only when needed
Understand parent stacking contexts
Test overlapping elements in different scenarios
Use z-index for layering modals, dropdowns, and tooltips
Sticky headers and floating buttons
Modals and popups
Dropdown and mega menus
Sliders and card stacks
Overlapping images or text layers
Correct use of z-index ensures content is layered logically and consistently.
The CSS z-index property controls the stacking order of positioned elements. Elements with higher z-index appear in front, and negative values push elements backward. It works only with positioned elements and is essential for modals, dropdowns, floating elements, and layered layouts. Understanding stacking contexts, proper values, and parent-child relationships allows precise control of overlapping content, improving layout clarity and user experience.
Q1. Make a div appear above another using z-index.
Q2. Stack .card1, .card2, and .card3 with increasing z-index.
Q3. Place a modal dialog above all other elements.
Q4. Hide an element behind the background using z-index: -1.
Q5. Create overlapping boxes and control their layer order.
Q6. Write a rule where a tooltip appears above a button.
Q7. Use inline CSS to set z-index: 9999 on a <div>.
Q8. Set multiple elements with the same z-index and observe stacking.
Q9. Create a notification banner that always appears on top.
Q10. Write a positioned container and a child with a higher z-index.