Fixed vs Absolute Positioning
You'll learn how fixed and absolute positioning differ in their scrolling behavior and what they anchor to.
Core Differences
Both fixed and absolute positioning remove elements from normal flow, but they behave very differently:
Absolute positioning anchors an element relative to its nearest positioned ancestor (an ancestor with position: relative, absolute, or fixed). If no positioned ancestor exists, it uses the initial containing block (usually the document). When you scroll the page, absolutely positioned elements scroll along with the content.
Fixed positioning always anchors relative to the viewport (the browser window itself), ignoring all ancestors. Fixed elements stay locked in place on your screen—they don't scroll with the page content.
Think of It This Way
Imagine a sticker:
- Absolute is like placing a sticker on a piece of paper. When you move the paper up or down (scrolling), the sticker moves with it.
- Fixed is like placing a sticker on your computer screen. No matter how you scroll the content behind it, the sticker stays in the exact same spot on the screen.
Common Use Cases
- Absolute: Tooltips, dropdown menus, positioned icons within a container
- Fixed: Navigation bars, chat widgets, "back to top" buttons, cookie banners
The Containing Block
This is the crucial difference: absolute positioning respects your positioned ancestors, making it perfect for positioning elements within specific containers. Fixed positioning ignores all that—it only cares about the viewport.
Key Takeaway: Absolute positioning moves with page scroll and positions relative to ancestors; fixed positioning stays locked to the viewport regardless of scrolling or ancestor elements.