Position: static, relative, absolute, fixed, sticky
10 min read
Taking Elements Out of Normal Flow
The position property controls how an element is placed in the document. Most elements default to static, meaning they participate in normal document flow. The other values enable precise coordinate-based placement.
css
/* static — default, top/left/etc. have no effect */
.default { position: static; }
/* relative — offset from its normal position, still in flow */
.badge {
position: relative;
top: -4px; /* shift up 4px without affecting other elements */
}
/* absolute — removed from flow, positioned relative to nearest
non-static ancestor */
.tooltip {
position: absolute;
top: 100%;
left: 0;
}
/* Parent must be positioned for absolute child to anchor to it */
.tooltip-wrapper {
position: relative;
}
/* fixed — relative to the viewport, stays on scroll */
.sticky-nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 100;
}
/* sticky — normal flow until threshold, then fixed within parent */
.section-header {
position: sticky;
top: 0;
background: #fff;
}z-index
z-index controls the stacking order of positioned elements (those with position other than static). Higher values appear in front. Elements with the same z-index are stacked in source order — later elements appear on top.
z-index only works on elements that have a position value other than static. Setting z-index on a static element has no effect.
Ready to test yourself?
Practice CSS— quiz & coding exercises