Performance: Common layering issues
This affects visual stability and interaction responsiveness by causing unexpected element overlaps and repaints.
Jump into concepts and practice - no test required
/* Use minimal and logical z-index values with clear stacking order */ .header { position: relative; z-index: 1; } .modal { position: fixed; z-index: 10; } .tooltip { position: absolute; z-index: 20; }
/* Multiple elements with high z-index values creating complex stacking contexts */ .header { position: relative; z-index: 1000; } .modal { position: fixed; z-index: 2000; } .tooltip { position: absolute; z-index: 3000; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| High scattered z-index values | No extra DOM nodes | 0 reflows | Multiple paints and repaints due to complex stacking | [X] Bad |
| Minimal logical z-index values | No extra DOM nodes | 0 reflows | Single paint with simple compositing | [OK] Good |
z-index property sets which element appears on top when elements overlap.position sets how elements are positioned but does not control layering alone; display and float affect layout, not stacking order.z-index work on an element?position set to relative, absolute, fixed, or sticky respond to z-index.position: static is default and ignores z-index. display and float do not enable z-index.div {
position: relative;
}
#box1 {
z-index: 5;
background: red;
width: 100px;
height: 100px;
}
#box2 {
position: absolute;
z-index: 3;
background: blue;
width: 100px;
height: 100px;
top: 20px;
left: 20px;
}position: relative and z-index: 5. #box2 has position: absolute and z-index: 3.z-index means the element is closer to the front. 5 is greater than 3, so #box1 is on top.z-index property not work on this element?.popup {
z-index: 10;
background: yellow;
width: 200px;
height: 100px;
}position set, so it defaults to static.z-index only works on elements with position set to relative, absolute, fixed, or sticky. Static elements ignore z-index.#a { position: relative; z-index: 2; }
#b { position: absolute; z-index: 1; }
#c { position: relative; z-index: 3; }z-index value?