A guide to relative, absolute, fixed, and sticky positioning.
Static Positioning
By default, all HTML elements have position: static. Static elements follow the
normal document flow and ignore top, right, bottom, and
left properties.
.element {
position: static; /* Default */
}
First element
Second element
Third element
Static elements stack in normal document flow.
Static positioning is rarely declared explicitly since it is the default. Understanding it
provides the foundation for the other position values.
Relative Positioning
An element with position: relative is positioned relative to its normal position.
The element still occupies its original space but can be offset using directional properties.
The dark box is offset, but its original space remains.
Key behaviors: The offset is calculated from the element's original position.
Other elements behave as if the positioned element had not moved.
Absolute Positioning
An element with position: absolute is removed from normal flow and positioned
relative to its nearest positioned ancestor. If none exists, it uses the viewport.
See the bottom-right corner of this page for a fixed element. It remains visible as you scroll.
Key behaviors: The element stays in place during scrolling. Common uses include
navigation bars and floating buttons. Be mindful of overlap with page content.
Your Fixed Element
Sticky Positioning
An element with position: sticky acts like relative positioning until a scroll
threshold is reached, then becomes fixed.
.element {
position: sticky;
top: 0;
}
Scroll within the box below to see sticky positioning:
Sticky Header
Content item 1
Content item 2
Content item 3
Content item 4
Content item 5
Content item 6
Content item 7
Content item 8
Content item 9
Content item 10
The header sticks to the top as you scroll.
Key behaviors: Requires a threshold value like top: 0. The sticky
element is constrained to its parent container.
Z-Index and Stacking
When positioned elements overlap, z-index controls which appears on top. Higher
values appear above lower values.