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
Z-index stacking control
📖 Scenario: You are building a simple webpage with overlapping colored boxes. You want to control which box appears on top using z-index values.
🎯 Goal: Create a small HTML structure with three overlapping boxes using Tailwind CSS. Assign z-index values to control their stacking order so that the red box is on top, the green box is in the middle, and the blue box is at the bottom.
📋 What You'll Learn
Create three div elements with background colors red, green, and blue respectively.
Use Tailwind CSS classes to position the boxes so they overlap.
Add z-index classes to control the stacking order: red on top, green in the middle, blue at the bottom.
💡 Why This Matters
🌍 Real World
Web designers often need to control which elements appear on top when they overlap, such as menus, modals, or layered images.
💼 Career
Understanding z-index stacking is essential for front-end developers and UI designers to create visually correct and user-friendly interfaces.
Progress0 / 4 steps
1
Create three colored boxes
Create three div elements with Tailwind CSS classes for background colors: bg-red-500, bg-green-500, and bg-blue-500. Each box should have fixed width and height of w-24 h-24.
Tailwind
Hint
Use Tailwind classes bg-red-500, bg-green-500, and bg-blue-500 for colors and w-24 h-24 for size.
2
Position boxes to overlap
Add Tailwind CSS classes relative to each div and use top-0 left-0 for the first box, top-6 left-6 for the second, and top-12 left-12 for the third to position them so they overlap diagonally.
Tailwind
Hint
Use relative positioning and offset classes like top-6 and left-6 to overlap boxes diagonally.
3
Add z-index classes to control stacking
Add Tailwind CSS z-index classes z-30 to the red box, z-20 to the green box, and z-10 to the blue box to set their stacking order with red on top.
Tailwind
Hint
Use Tailwind z-index classes like z-30, z-20, and z-10 to control which box appears on top.
4
Wrap boxes in a container with relative positioning
Wrap the three boxes inside a div container with Tailwind class relative and fixed width w-48 and height h-48 to keep the boxes positioned correctly.
Tailwind
Hint
Wrap the boxes in a container div with relative positioning and fixed size to keep layout stable.
Practice
(1/5)
1. What does the z-index property control in Tailwind CSS?
easy
A. The size of an element
B. Which element appears on top when elements overlap
C. The color of an element
D. The font style of text
Solution
Step 1: Understand the purpose of z-index
The z-index property controls stacking order of overlapping elements.
Step 2: Identify what z-index affects
It determines which element appears on top visually when elements overlap.
Final Answer:
Which element appears on top when elements overlap -> Option B
Quick Check:
z-index controls stacking order = B [OK]
Hint: z-index means who is on top visually [OK]
Common Mistakes:
Confusing z-index with size or color
Thinking z-index changes element size
Ignoring stacking order concept
2. Which Tailwind class correctly sets an element's z-index to 10?
easy
A. z-10
B. z-index-10
C. z10
D. index-z-10
Solution
Step 1: Recall Tailwind z-index syntax
Tailwind uses z- prefix followed by the number to set z-index.
Step 2: Match correct class
The correct class for z-index 10 is z-10.
Final Answer:
z-10 -> Option A
Quick Check:
Tailwind z-index class = A [OK]
Hint: Tailwind z-index classes start with 'z-' [OK]
Common Mistakes:
Using 'z-index-10' which is invalid
Missing dash between z and number
Reversing order like 'index-z-10'
3. Given two overlapping divs with classes relative z-20 and relative z-10, which div appears on top?
medium
A. The div with z-20
B. They overlap with no order
C. They appear side by side
D. The div with z-10
Solution
Step 1: Understand z-index values
Higher z-index values stack above lower ones.
Step 2: Compare given z-index values
z-20 is higher than z-10, so it appears on top.
Final Answer:
The div with z-20 -> Option A
Quick Check:
Higher z-index on top = D [OK]
Hint: Higher z-index means on top visually [OK]
Common Mistakes:
Assuming lower number is on top
Ignoring position requirement
Thinking they don't overlap
4. Why does the z-50 class not bring an element on top if it has no positioning class like relative or absolute?
medium
A. Because the element is hidden
B. Because z-50 is not a valid Tailwind class
C. Because z-index only works on positioned elements
D. Because z-index only works on text elements
Solution
Step 1: Recall z-index requirements
z-index only affects elements with position set (relative, absolute, fixed, sticky).
Step 2: Analyze missing positioning
Without positioning, z-index has no effect, so z-50 won't change stacking.
Final Answer:
Because z-index only works on positioned elements -> Option C
Quick Check:
Position required for z-index = A [OK]
Hint: z-index needs position set to work [OK]
Common Mistakes:
Thinking z-50 is invalid
Assuming z-index works without position
Confusing visibility with stacking
5. You have three overlapping elements with classes absolute z-30, relative z-40, and fixed z-20. Which element will appear on top and why?
hard
A. The element with fixed z-20 because fixed position overrides z-index
B. The element with absolute z-30 because absolute is always on top
C. All elements appear at the same level because positions differ
D. The element with relative z-40 because it has the highest z-index
Solution
Step 1: Understand z-index and position interaction
z-index controls stacking order only among positioned elements; all three are positioned.
Step 2: Compare z-index values
The element with z-40 has the highest z-index, so it appears on top regardless of position type.
Final Answer:
The element with relative z-40 because it has the highest z-index -> Option D
Quick Check:
Highest z-index on positioned elements = C [OK]
Hint: Highest z-index wins if position is set [OK]