-
Hajipur, Bihar, 844101
z-index
in CSS?The z-index
property in CSS specifies the stacking order of elements along the z-axis (i.e., which element appears in front of or behind another).
It only works on positioned elements (relative
, absolute
, fixed
, or sticky
).
element {
position: relative;
z-index: 2;
}
✅ A higher z-index
means the element will be placed on top of others with a lower z-index
.
Default z-index
is auto (treated as 0).
Only works when the element is positioned (not static).
Elements with higher z-index
appear in front.
If two elements have the same z-index
, the later one in HTML appears on top.
.box1 {
position: absolute;
z-index: 1;
background-color: red;
}
.box2 {
position: absolute;
z-index: 2;
background-color: blue;
}
✅ .box2
will appear on top of .box1
.
.box {
position: relative;
z-index: -1;
}
✅ Element is placed behind others (often even behind the background).
Layering modal windows
Placing tooltips above elements
Arranging background and content layers
Creating image stacks or cards
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
.
Q1: What does z-index control?
Q2: Which property must be set for z-index to work?
Q3: Which z-index value places an element on top?
Q4: If two elements have the same z-index, which one appears on top?
Q5: What does z-index: -1 do?
Q6: Which value is valid for z-index?
Q7: Can z-index work without a position value?
Q8: What is the default value of z-index?
Q9: Which of the following elements will appear above?
Q10: Which scenario is best for using a high z-index?