Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Fixing Common Layering Issues with CSS Z-Index
📖 Scenario: You are creating a simple webpage with three colored boxes stacked on top of each other. However, the boxes are not layering as expected. You need to fix the layering using CSS z-index so that the red box is on top, the green box is in the middle, and the blue box is at the bottom.
🎯 Goal: Build a webpage with three overlapping colored boxes using div elements. Use CSS positioning and z-index to control the layering order so the red box appears on top, the green box in the middle, and the blue box at the bottom.
📋 What You'll Learn
Create three div elements with classes box-red, box-green, and box-blue.
Each box should have a fixed size of 8rem by 8rem and be positioned absolutely with some overlap.
Use CSS z-index to layer the boxes so red is on top, green is in the middle, and blue is at the bottom.
Use semantic HTML structure and include a <main> container for the boxes.
Ensure the layering works correctly in a modern browser.
💡 Why This Matters
🌍 Real World
Layering elements correctly is important for building user interfaces where items overlap, such as modals, dropdowns, and tooltips.
💼 Career
Understanding CSS layering and z-index is a fundamental skill for front-end developers to create visually correct and accessible web pages.
Progress0 / 4 steps
1
Create the HTML structure with three colored boxes
Create a <main> element containing three <div> elements with classes box-red, box-green, and box-blue. Each box will represent a colored square.
CSS
Hint
Use three <div> elements inside <main>. Assign the exact classes box-red, box-green, and box-blue.
2
Add CSS to size and position the boxes with overlap
Add CSS rules to make each box 8rem wide and tall, position them absolutely inside main, and place them so they overlap partially. Use position: relative on main and position: absolute on each box with different top and left values.
CSS
Hint
Set position: relative on main. Then set position: absolute, width, and height on each box. Use different top and left values to overlap them.
3
Add z-index to control layering order
Add CSS z-index properties to the boxes so that .box-red has the highest z-index, .box-green has a middle z-index, and .box-blue has the lowest z-index. Use integer values to set the layering order.
CSS
Hint
Use z-index with higher numbers on the box you want on top. For example, red gets z-index: 3, green z-index: 2, and blue z-index: 1.
4
Add accessibility and responsive improvements
Add a role="main" attribute to the <main> element for accessibility. Also add a CSS media query to reduce the size of the boxes to 6rem by 6rem on screens narrower than 400px for better mobile viewing.
CSS
Hint
Add role="main" inside the <main> tag. Then add a media query @media (max-width: 400px) that sets the boxes' width and height to 6rem.
Practice
(1/5)
1. Which CSS property controls the stacking order of elements on a webpage?
easy
A. float
B. position
C. z-index
D. display
Solution
Step 1: Understand stacking order control
The z-index property sets which element appears on top when elements overlap.
Step 2: Differentiate from other properties
position sets how elements are positioned but does not control layering alone; display and float affect layout, not stacking order.
How can you make #b appear on top without changing its z-index value?
hard
A. Change #b's position to relative and keep z-index 1
B. Wrap #b in a parent with higher z-index and position set
C. Set #a and #c to position: static
D. Increase #b's width and height
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 B
Quick Check:
Use parent stacking context to control layering [OK]
Hint: Use parent with higher z-index to raise child layering [OK]