Full answer
The position property decides how an element is placed and what the top, right, bottom and left offsets are measured from. The default is static, which means normal flow, and offsets do nothing.
relative: the element stays in normal flow and keeps its original space. Offsets move it visually from where it would have been. It also becomes the reference box for absolutely positioned children.absolute: the element is taken out of flow. Other content closes the gap. Offsets are measured from the nearest ancestor that is positioned (anything other thanstatic). If there is none, it uses the initial containing block, which is roughly the page.fixed: also out of flow, but offsets are measured from the viewport, so the element stays on screen while the page scrolls. An ancestor with atransform,filterorperspectivecan become its containing block instead.sticky: acts asrelativeuntil the scroll position reaches the threshold you set withtop(or another offset), then it sticks inside its scroll container until its parent box ends.
A classic use: a notification badge on a shopping cart icon.
In this page the badge sits 8px above and to the right of the cart box. The help button stays at the bottom-right corner after scrolling, and the heading stays at the top.
html
<style>
body { margin: 0; height: 1500px; }
.cart { position: relative; width: 120px; height: 60px; background: #eee; margin: 20px; }
.badge { position: absolute; top: -8px; right: -8px; background: crimson; color: #fff; padding: 2px 8px; border-radius: 999px; }
.help { position: fixed; bottom: 10px; right: 10px; background: #222; color: #fff; padding: 8px; }
.heading { position: sticky; top: 0; background: gold; padding: 8px; }
</style>
<div class="heading" id="h">Sticky heading</div>
<div class="cart" id="cart">Cart <span class="badge" id="badge">3</span></div>
<div class="help" id="help">Help</div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- static ignores offsets; relative offsets from its own place and keeps its space.
- absolute leaves the flow and uses the nearest positioned ancestor.
- fixed uses the viewport, unless an ancestor has transform, filter or perspective.
- sticky needs an offset such as top and sticks within its parent.
- A relative parent without offsets is the usual anchor for an absolute child.
Common mistakes
- using absolute positioning for page layout. The element no longer takes space, so the parent collapses and content overlaps.
- sticky that does nothing. The usual causes are no offset such as top: 0, or an ancestor with overflow: hidden, auto or scroll between the element and the scrolling area.
Follow-ups
- does position: absolute change display?
- Why does a fixed element sometimes scroll with its parent?
- What changes about display when an element is absolutely positioned?
Tips
- Set position: relative on the parent without any offsets whenever you only want it to be the anchor for an absolute child.
- Use one image in your head: a badge on a cart icon. It covers relative plus absolute in one example.
- Remember to name sticky; many answers stop at three values.