Layering helps decide which parts of a webpage appear on top of others. Sometimes layers don't show as expected, causing confusion or hiding content.
Common layering issues in CSS
selector {
position: relative | absolute | fixed | sticky;
z-index: number;
}position must be set to something other than static for z-index to work.
z-index uses numbers; higher numbers appear on top.
div {
position: relative;
z-index: 1;
}img {
position: absolute;
z-index: 10;
}z-index will not work because position is static by default.button {
position: static;
z-index: 5;
}Three colored boxes overlap. Box 2 has the highest z-index so it appears on top. Box 1 is in the middle. Box 3 has the lowest z-index and appears behind the others.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Layering Example</title> <style> .box1 { position: relative; width: 150px; height: 150px; background-color: lightblue; z-index: 1; margin: 20px; } .box2 { position: relative; width: 150px; height: 150px; background-color: coral; margin: -100px 0 0 100px; z-index: 2; } .box3 { position: relative; width: 150px; height: 150px; background-color: lightgreen; margin: -100px 0 0 50px; z-index: 0; } </style> </head> <body> <div class="box1">Box 1 (z-index: 1)</div> <div class="box2">Box 2 (z-index: 2)</div> <div class="box3">Box 3 (z-index: 0)</div> </body> </html>
Always set position to relative, absolute, fixed, or sticky for z-index to work.
Elements with the same z-index follow the order they appear in the HTML (later elements appear on top).
Parent elements' stacking context can affect child elements' layering.
Use position and z-index to control which elements appear on top.
Higher z-index means the element is closer to the front.
Without proper position, z-index won't work.
Practice
Solution
Step 1: Understand stacking order control
Thez-indexproperty sets which element appears on top when elements overlap.Step 2: Differentiate from other properties
positionsets how elements are positioned but does not control layering alone;displayandfloataffect layout, not stacking order.Final Answer:
z-index -> Option CQuick Check:
Stack order = z-index [OK]
- Confusing position with z-index
- Thinking display controls layering
- Assuming float affects stacking
z-index work on an element?Solution
Step 1: Identify position values that enable z-index
Only elements withpositionset torelative,absolute,fixed, orstickyrespond toz-index.Step 2: Exclude static and other properties
position: staticis default and ignoresz-index.displayandfloatdo not enablez-index.Final Answer:
Set position to relative or absolute -> Option DQuick Check:
z-index works only with positioned elements [OK]
- Using position: static and expecting z-index to work
- Confusing display or float with position
- Not setting position at all
div {
position: relative;
}
#box1 {
z-index: 5;
background: red;
width: 100px;
height: 100px;
}
#box2 {
position: absolute;
z-index: 3;
background: blue;
width: 100px;
height: 100px;
top: 20px;
left: 20px;
}Solution
Step 1: Check positions and z-index values
#box1 hasposition: relativeandz-index: 5. #box2 hasposition: absoluteandz-index: 3.Step 2: Compare z-index values
Higherz-indexmeans the element is closer to the front. 5 is greater than 3, so #box1 is on top.Final Answer:
The red box (#box1) appears on top -> Option AQuick Check:
Higher z-index = front [OK]
- Assuming absolute position always appears on top
- Ignoring z-index values
- Thinking position type alone controls layering
z-index property not work on this element?.popup {
z-index: 10;
background: yellow;
width: 200px;
height: 100px;
}Solution
Step 1: Check element's position property
The element has nopositionset, so it defaults tostatic.Step 2: Understand z-index requirements
z-indexonly works on elements withpositionset torelative,absolute,fixed, orsticky. Static elements ignorez-index.Final Answer:
z-index only works on positioned elements -> Option AQuick Check:
Position must be non-static for z-index [OK]
- Thinking background color affects z-index
- Believing width/height affect layering
- Assuming parent position is required
#a { position: relative; z-index: 2; }
#b { position: absolute; z-index: 1; }
#c { position: relative; z-index: 3; }How can you make #b appear on top without changing its
z-index value?Solution
Step 1: Understand stacking context
Elements create stacking contexts based on position and z-index. #b has z-index 1 but is inside its own stacking context.Step 2: Use parent stacking context to raise #b
Wrapping #b in a parent with a higher z-index and position creates a new stacking context that can appear above #a and #c without changing #b's z-index.Final Answer:
Wrap #b in a positioned parent with higher z-index -> Option BQuick Check:
Use parent stacking context to control layering [OK]
- Trying to change z-index of #b directly
- Changing position without stacking context
- Ignoring stacking contexts created by parents
Ready to prove it?
Pass the test and this pattern moves unknown
