position: static; /* Default behavior */All boxes flow normally, ignoring any top/left values.
position: relative;
top: 20px;
left: 30px;Notice the dashed outline shows original position!
position: absolute;
top: 50px;
right: 20px;Absolute box is positioned relative to this demo container!
position: fixed;
top: 10px;
right: 10px;Click button to see fixed positioning in action!
position: sticky;
top: 0;Keep scrolling to see the sticky behavior...
The sticky box will stick to the top when you scroll!
But it only sticks within this scrollable container.
More content here...
Even more content...
Keep scrolling...
Almost there...
End of scroll area!
z-index: 1; /* Lower */
z-index: 2; /* Middle */
z-index: 3; /* Higher = on top */| Position | Document Flow | Positioned Relative To | Common Use Cases |
|---|---|---|---|
| static | Normal flow | - | Default behavior |
| relative | Keeps space | Its normal position | Small adjustments, parent for absolute |
| absolute | Removed from flow | Nearest positioned parent | Overlays, tooltips, dropdowns |
| fixed | Removed from flow | Viewport (browser window) | Headers, modals, floating buttons |
| sticky | Conditional | Scrolling container | Sticky headers, navigation |
1. Parent-Child Relationship: Absolute elements position relative to their nearest positioned parent (not static).
2. Z-Index Only Works: On positioned elements (relative, absolute, fixed, sticky).
3. Fixed vs Sticky: Fixed stays put always, sticky only when scrolling.
4. Performance: Fixed and absolute elements don't affect other elements' layout.