Where an element really sits
The position property is the foundation of CSS layout control: it decides
whether an element flows normally, shifts visually, or is lifted out of flow and
pinned to a containing block. Getting it wrong produces the classic bugs — a
fixed element that scrolls, or an absolute element anchored to the wrong
parent. This reference explains all five values with their containing block,
inset behaviour, flow impact and stacking-context rules.
How it works
The top, right, bottom and left insets (and the inset shorthand) are
measured against the element’s containing block, which differs per value:
static— default; insets andz-indexare ignored, element stays in flow.relative— offset from its in-flow position; original space is preserved.absolute— removed from flow; positioned in the nearest positioned ancestor.fixed— removed from flow; positioned in the viewport (or a transformed ancestor).sticky— in flow until a scroll threshold, then sticks within its scroll container.
Establishing a stacking context matters for z-index: fixed and sticky
always create one, while relative/absolute only do so once z-index is not
auto. A transform or filter on an ancestor silently reparents fixed
descendants — the single most common positioning surprise.
Tips and notes
- Set
position: relativeon a parent to anchorabsolutechildren to it. - A
transformanywhere up the tree can breakposition: fixed. stickyneeds room to move — it does nothing if the container is the same height.staticignoresz-index; give an element a positioning value to stack it.