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
Understanding CSS Stacking Context
📖 Scenario: You are creating a simple webpage with two overlapping colored boxes. You want to control which box appears on top using CSS stacking context rules.
🎯 Goal: Build a webpage with two overlapping <div> elements. Use CSS properties to create stacking contexts and control which box appears on top.
📋 What You'll Learn
Create two <div> elements with distinct background colors and sizes.
Position the boxes so they overlap partially.
Use CSS properties to create stacking contexts on the boxes.
Control the stacking order so the red box appears above the blue box.
💡 Why This Matters
🌍 Real World
Web developers often need to control how elements overlap on a page, such as menus, modals, or images. Understanding stacking context helps avoid visual bugs.
💼 Career
Knowing stacking context is essential for front-end developers to create visually correct and accessible user interfaces.
Progress0 / 4 steps
1
Create two overlapping boxes in HTML and CSS
Create two <div> elements with id attributes blue-box and red-box. Style them with CSS so that #blue-box has a blue background, width and height of 10rem, and is positioned relative at top: 2rem and left: 2rem. Style #red-box with a red background, width and height of 10rem, and position it relative at top: 0 and left: 5rem. This will make the boxes overlap partially.
CSS
Hint
Use position: relative; and top, left properties to move the boxes so they overlap.
2
Add z-index to create stacking context on the blue box
Add a CSS property z-index: 1; to the #blue-box style. This will create a stacking context for the blue box and set its stacking order.
CSS
Hint
Adding z-index to a positioned element creates a stacking context.
3
Add z-index to the red box to appear on top
Add a CSS property z-index: 2; to the #red-box style. This will create a stacking context for the red box and make it appear above the blue box.
CSS
Hint
Use a higher z-index value on the red box to make it appear on top.
4
Add an aria-label for accessibility and finalize
Add an aria-label="Blue colored box" attribute to the <div> with id="blue-box" and an aria-label="Red colored box" attribute to the <div> with id="red-box". This improves accessibility by describing the boxes to screen readers.
CSS
Hint
Use aria-label attributes on the <div> elements to describe them for screen readers.
Practice
(1/5)
1. What creates a new stacking context in CSS?
easy
A. An element with color property set
B. An element with position: relative and z-index set
C. Any element with display: block
D. An element with margin applied
Solution
Step 1: Understand stacking context creation
A stacking context is created by elements with position other than static and a z-index value set.
Step 2: Analyze options
Only An element with position: relative and z-index set mentions position: relative with z-index, which creates a stacking context. Other options do not create stacking contexts.
Final Answer:
An element with position: relative and z-index set -> Option B
Quick Check:
Stacking context = position + z-index [OK]
Hint: Look for position plus z-index to spot stacking contexts [OK]
Common Mistakes:
Thinking any positioned element creates stacking context without z-index
Confusing display or color properties with stacking context
Assuming margin affects stacking order
2. Which CSS snippet correctly creates a stacking context?
easy
A. position: static; z-index: 10;
B. display: inline; z-index: 3;
C. position: relative; z-index: 5;
D. color: red; z-index: 1;
Solution
Step 1: Check position and z-index combination
Only elements with position other than static and a z-index value create stacking contexts.
Step 2: Evaluate each option
position: relative; z-index: 5; uses position: relative and z-index: 5, which correctly creates a stacking context. Others either have static position or irrelevant properties.
Final Answer:
position: relative; z-index: 5; -> Option C
Quick Check:
Position relative + z-index creates stacking context [OK]
Hint: Position must not be static to create stacking context [OK]
Common Mistakes:
Using position static with z-index expecting stacking context
Assuming display or color create stacking contexts
Ignoring that inline elements don't create stacking contexts with z-index
3. Given this HTML and CSS, which element appears on top visually?
Which statement is true about their stacking order?
hard
A. Inner's z-index 10 stacks only inside outer's stacking context
B. Inner's z-index 10 places it above all elements outside outer
C. Outer and inner share the same stacking context ignoring z-index
D. Inner's z-index is ignored because outer has lower z-index
Solution
Step 1: Identify stacking contexts
The outer element creates a stacking context with position: relative and z-index: 1. The inner element creates a new stacking context inside outer with position: relative and z-index: 10.
Step 2: Understand stacking context isolation
Inner's stacking order is only relative to siblings inside outer's stacking context. It cannot escape outer's stacking context to appear above elements outside it.
Final Answer:
Inner's z-index 10 stacks only inside outer's stacking context -> Option A