CSS positioning controls how elements are placed in the document flow. Here's a comprehensive guide.
Position Static#
Position Relative#
Position Absolute#
Position Fixed#
Position Sticky#
Z-Index#
Positioning Context#
Offset Properties#
Common Patterns#
Modal Pattern#
Dropdown Pattern#
Tooltip Pattern#
Sticky Gotchas#
Best Practices#
Positioning Choice:
✓ relative for positioning context
✓ absolute for overlays/badges
✓ fixed for persistent UI
✓ sticky for scroll-aware elements
Z-Index:
✓ Use a scale system
✓ Keep values organized
✓ Understand stacking contexts
✓ Avoid arbitrary large values
Performance:
✓ Fixed/sticky create layers
✓ Use transform for animations
✓ will-change for complex cases
✓ Test on mobile
Avoid:
✗ Position absolute without context
✗ Magic z-index numbers
✗ Breaking sticky with overflow
✗ Fixed position on mobile keyboards
Conclusion#
CSS positioning gives precise control over element placement. Use relative to create positioning contexts, absolute for overlays and decorations, fixed for persistent headers/modals, and sticky for scroll-aware elements. Master z-index by understanding stacking contexts and using a consistent scale system.