Practice
1. What does the CSS property
z-index control?easy
Solution
Step 1: Understand the purpose of
Thez-indexz-indexproperty is used to control which elements appear on top when they overlap.Step 2: Compare with other CSS properties
Font size, background color, and margin do not affect stacking order.Final Answer:
The stacking order of overlapping elements -> Option AQuick Check:
z-index controls stacking order [OK]
Hint: Remember: z-index = which element is on top [OK]
Common Mistakes:
- Confusing z-index with color or size properties
- Thinking z-index changes element size
- Assuming z-index works without positioning
2. Which of the following is the correct way to apply
z-index to an element?easy
Solution
Step 1: Check which positions allow z-index
Only positioned elements (relative, absolute, fixed, sticky) respond to z-index.Step 2: Identify correct syntax
Position must not be static (default). Soposition: relativewithz-indexworks.Final Answer:
position: relative; z-index: 10; -> Option AQuick Check:
z-index works only with positioned elements [OK]
Hint: z-index needs position other than static [OK]
Common Mistakes:
- Using z-index without setting position
- Assuming display affects stacking
- Using margin or padding with z-index expecting effect
3. Given the CSS below, which element will appear on top?
div.a { position: relative; z-index: 5; }
div.b { position: relative; z-index: 10; }medium
Solution
Step 1: Compare z-index values
div.a has z-index 5, div.b has z-index 10. Higher z-index means on top.Step 2: Confirm both are positioned
Both haveposition: relative, so z-index applies.Final Answer:
div.b will be on top -> Option BQuick Check:
Higher z-index = top element [OK]
Hint: Higher z-index number means element is on top [OK]
Common Mistakes:
- Ignoring position property
- Thinking lower z-index is on top
- Assuming elements don't overlap
4. Why does the
z-index property not work on this element?.box { z-index: 100; }medium
Solution
Step 1: Check element positioning
By default, elements haveposition: static, which ignores z-index.Step 2: Understand z-index requirements
z-index only works if position is relative, absolute, fixed, or sticky.Final Answer:
Because the element has no position set or is static -> Option CQuick Check:
z-index needs non-static position [OK]
Hint: Set position to relative or absolute for z-index to work [OK]
Common Mistakes:
- Assuming z-index works without position
- Thinking z-index depends on background color
- Believing margin affects stacking order
5. You have three overlapping elements with these styles:
Which order will they stack from bottom to top?
.one { position: relative; z-index: 1; }
.two { position: absolute; z-index: 3; }
.three { position: relative; z-index: 2; }Which order will they stack from bottom to top?
hard
Solution
Step 1: List elements with their z-index
.one = 1, .three = 2, .two = 3.Step 2: Order by ascending z-index
Lower z-index is below higher z-index, so stacking is .one (bottom), .three (middle), .two (top).Final Answer:
.one, .three, .two -> Option DQuick Check:
Stack order = ascending z-index [OK]
Hint: Stack from lowest to highest z-index [OK]
Common Mistakes:
- Ignoring absolute vs relative position effect
- Mixing up stacking order direction
- Assuming position type changes z-index order
