Consider three overlapping boxes with the following CSS:
.box1 { position: relative; z-index: 2; }
.box2 { position: relative; z-index: 5; }
.box3 { position: relative; z-index: 1; }Which box will appear on top?
Higher z-index means the element is closer to the viewer.
The element with the highest z-index value appears on top when elements overlap.
Given an element with position: static; and z-index: 10;, what will happen?
z-index only works on positioned elements (relative, absolute, fixed, sticky).
z-index has no effect on elements with position: static; because they are not positioned.
Which of the following CSS z-index values is invalid?
z-index accepts integers or 'auto', but not decimals.
z-index must be an integer or 'auto'. Decimal values like 1.5 are invalid.
You have many nested elements with different z-index values creating complex stacking contexts. What is the best way to optimize rendering performance?
Complex stacking contexts can slow down rendering.
Reducing positioned elements and unnecessary z-index values simplifies stacking contexts and improves performance.
Given these CSS rules:
.parent { position: relative; z-index: 1; }
.child1 { position: absolute; z-index: 10; }
.child2 { position: absolute; z-index: 5; }
.sibling { position: relative; z-index: 2; }Why might .child1 not appear above .sibling even though it has a higher z-index?
Stacking contexts are created by positioned elements with z-index.
Because .child1 is inside .parent with z-index 1, it cannot escape that stacking context. .sibling is outside with z-index 2, so it appears above.