This page covers position and related properties: static,
relative, absolute, fixed, sticky. Each example shows output and
code.
position: static (default)Every element has position: static by default. It stays in normal document flow.
top, right, bottom, left, and z-index have no
effect.
.box {
position: static; /* default; usually omitted */
}
position: relativeThe element stays in normal flow, but top, right, bottom,
left offset it from its original position. The space it would have taken is still
reserved.
.box {
position: relative;
top: 10px;
left: 20px;
}
position: absolute children, or for small visual tweaks.
position: absoluteThe element is removed from normal flow. It is positioned relative to its nearest
positioned ancestor (one with position other than static). If none
exists, it uses the viewport. top, right, bottom, left define
its edges.
.parent {
position: relative;
height: 120px;
}
.child {
position: absolute;
right: 10px;
bottom: 10px;
}
position: relative
parent. Without a positioned ancestor, it would use the <html> (viewport). The child no longer affects the
layout of siblings. Use for overlays, badges, tooltips, dropdowns.
position: fixedThe element is removed from flow and positioned relative to the viewport. It stays in the same place when the user scrolls. Use for fixed headers, footers, or “back to top” buttons.
Scroll the page — this box stays fixed at bottom-right of the viewport.
.fixed-box {
position: fixed;
bottom: 20px;
right: 20px;
}
position: fixed ignores all ancestors. The element is always
positioned relative to the browser window (viewport). It doesn’t move when you scroll. On mobile, be careful —
fixed elements can behave differently when the address bar shows/hides.
position: stickyA hybrid: the element acts like relative until it would scroll out of view, then it
“sticks” like fixed until its container scrolls past. Requires top, right,
bottom, or left.
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
.sticky {
position: sticky;
top: 0;
}
top: 0 (or
whatever you set). Then it sticks there. When its parent scrolls out of view, the sticky element goes with it.
Great for sticky table headers, nav bars, or “sticky” CTAs.
z-index — stacking orderWhen elements overlap (e.g. absolute, fixed), z-index controls which one appears on
top. Higher values go on top. Only works on positioned elements (position not
static).
.box1 { position: absolute; z-index: 1; }
.box2 { position: absolute; z-index: 3; } /* on top */
.box3 { position: absolute; z-index: 2; }
z-index is auto. You can use integers (e.g. 1,
10, 100). Negative values go behind. Stacking contexts (e.g. from position, opacity,
transform) can affect which elements compare.
static — default; in normal flow.relative — in flow; offset by top/right/bottom/left from original position.absolute — out of flow; positioned relative to nearest positioned ancestor.fixed — out of flow; positioned relative to viewport; doesn’t scroll.sticky — hybrid; scrolls until threshold (e.g. top: 0), then sticks.z-index — stacking order; works only on positioned elements.