position: fixed - it stays while you scroll!
This page demonstrates different CSS position values. Scroll to see fixed and sticky in action!
position: static - Elements flow normally. top/left/right/bottom have no effect.
position: relative - Offset from normal position. Original space is preserved!
Notice the gap where the box originally was!
position: absolute - Positioned relative to nearest positioned ancestor (or viewport).
This container has position: relative (the positioned ancestor)
position: fixed - Positioned relative to viewport. Stays in place when scrolling.
Example: Look at the orange header at the top of this page!
.fixed-header {
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 1000;
}
position: sticky - Acts like relative until scroll threshold, then sticks like fixed.
Scroll inside the box below to see sticky headers:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit.
Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit.
Sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.
Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam.
z-index controls which positioned elements appear on top.
| Position | Relative To | In Document Flow? | Use Case |
|---|---|---|---|
| static | Normal flow | Yes | Default behavior |
| relative | Its normal position | Yes (space preserved) | Minor offsets, parent for absolute |
| absolute | Nearest positioned ancestor | No | Overlays, tooltips, dropdowns |
| fixed | Viewport | No | Fixed headers, floating buttons |
| sticky | Scroll container | Yes (until stuck) | Sticky headers, table headers |