Hybrid Behavior
position: sticky is when an element behaves like relative until we scroll to it, and turns into fixed when it touches the edge.
Three Iron Rules of Sticky:
- Coordinate is mandatory: Need to specify at least one property (e.g.,
top: 0), otherwise it won't " stick " . - Height is needed: The element's parent must be taller than the sticky block itself. The sticky block " rides " inside the parent, and if the parent ends — the block will move away with it.
- Sticky Killer: If ANY parent up the tree has
overflow: hidden,overflow: auto, oroverflow: scroll, stickiness stops working.
Tip
Sticky is perfect for list headers, sidebars (sidebar), and " sticky " table headers.