By default, elements lay out in the normal flow — block elements stack, inline elements sit in a line. The position property lets you break out of that flow to place things precisely: a badge on a card, a sticky header, a modal overlay. It's powerful and, once understood, predictable.
The normal flow
position: static is the default — the element stays in flow and ignores top/left. The other values move it relative to a reference point, and use the offset properties top, right, bottom, left.
relative & absolute
/* relative: nudge from its normal spot; still takes its original space.Most usefully, it becomes a positioning context for absolute children. */.card { position: relative; }/* absolute: removed from flow; positioned against the nearestpositioned ancestor (here, .card). */.card .badge {position: absolute;top: 8px; right: 8px;}
fixed & sticky
/* fixed: pinned to the viewport; stays put while the page scrolls. */.back-to-top { position: fixed; bottom: 24px; right: 24px; }/* sticky: normal until it hits a scroll threshold, then it sticks. */.section-nav { position: sticky; top: 0; }
sticky is the modern hero for headers and table-of-contents rails — it's in flow until you scroll past its top offset, then it holds position.
z-index & stacking
When positioned elements overlap, z-index controls which is on top — higher wins. But it only works on positioned elements, and it's scoped to a stacking context:
.modal { position: fixed; z-index: 100; }.overlay { position: fixed; z-index: 99; }.tooltip { position: absolute; z-index: 10; }
Practice
Put a "New" badge in the top-right corner of a card (relative parent + absolute child). Make a sticky section header that pins on scroll, and a fixed back-to-top button. Then build a simple modal with an overlay behind it using z-index.
Key takeaways
- 01static (default) keeps flow; relative/absolute/fixed/sticky break out of it using top/right/bottom/left.
- 02The core pattern: position: relative parent + position: absolute child.
- 03fixed pins to the viewport; sticky stays in flow until a scroll threshold, then sticks.
- 04z-index orders overlapping positioned elements — higher on top.
- 05z-index is scoped to a stacking context (opacity/transform/filter create new ones) — keep values low.
