Z-index helps decide which item appears on top when things overlap. It controls the stack order.
Z-index stacking control in Tailwind
Start learning this pattern below
Jump into concepts and practice - no test required
class="z-{value}" // where {value} is a number like 0, 10, 20, 30, 40, 50
Higher numbers appear on top of lower numbers.
Tailwind provides default z-index values like z-0, z-10, z-20, etc.
<div class="z-10">Content A</div> <div class="z-20">Content B</div>
<div class="z-0">Background</div> <div class="z-50">Popup</div>
<div class="z-10">Layer 1</div> <div class="z-10">Layer 2</div>
This example shows three overlapping colored boxes. Box 2 with z-20 appears on top, Box 1 with z-10 is below it, and Box 3 with z-0 is at the bottom.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Z-index Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="relative h-64"> <div class="absolute top-10 left-10 w-32 h-32 bg-blue-500 z-10 text-white flex items-center justify-center"> Box 1 (z-10) </div> <div class="absolute top-20 left-20 w-32 h-32 bg-red-500 z-20 text-white flex items-center justify-center"> Box 2 (z-20) </div> <div class="absolute top-32 left-32 w-32 h-32 bg-green-500 z-0 text-white flex items-center justify-center"> Box 3 (z-0) </div> </body> </html>
Z-index only works on positioned elements (relative, absolute, fixed, sticky).
Time complexity is not applicable as this is a styling property.
Common mistake: forgetting to set position, so z-index has no effect.
Use z-index to control layering instead of changing HTML order when possible.
Z-index controls which element appears on top when overlapping.
Higher z-index values stack above lower ones.
Works only on elements with position set (relative, absolute, fixed, sticky).
Practice
z-index property control in Tailwind CSS?Solution
Step 1: Understand the purpose of z-index
Thez-indexproperty 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 BQuick Check:
z-index controls stacking order = B [OK]
- Confusing z-index with size or color
- Thinking z-index changes element size
- Ignoring stacking order concept
Solution
Step 1: Recall Tailwind z-index syntax
Tailwind usesz-prefix followed by the number to set z-index.Step 2: Match correct class
The correct class for z-index 10 isz-10.Final Answer:
z-10 -> Option AQuick Check:
Tailwind z-index class = A [OK]
- Using 'z-index-10' which is invalid
- Missing dash between z and number
- Reversing order like 'index-z-10'
relative z-20 and relative z-10, which div appears on top?Solution
Step 1: Understand z-index values
Higher z-index values stack above lower ones.Step 2: Compare given z-index values
z-20is higher thanz-10, so it appears on top.Final Answer:
The div with z-20 -> Option AQuick Check:
Higher z-index on top = D [OK]
- Assuming lower number is on top
- Ignoring position requirement
- Thinking they don't overlap
z-50 class not bring an element on top if it has no positioning class like relative or absolute?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, soz-50won't change stacking.Final Answer:
Because z-index only works on positioned elements -> Option CQuick Check:
Position required for z-index = A [OK]
- Thinking z-50 is invalid
- Assuming z-index works without position
- Confusing visibility with stacking
absolute z-30, relative z-40, and fixed z-20. Which element will appear on top and why?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 withz-40has 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 DQuick Check:
Highest z-index on positioned elements = C [OK]
- Thinking fixed is always on top
- Ignoring z-index values
- Assuming position type alone controls stacking
