Discover how to make menus and buttons that follow you smoothly as you scroll, without any tricky code!
Why Position fixed and sticky in CSS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you want a menu bar that stays visible when you scroll down a long page. You try to move it manually by changing its position every time you scroll.
Manually adjusting the menu's position on scroll is slow, tricky, and causes flickering or jumps. It's hard to keep it smooth and consistent across devices.
CSS position: fixed and position: sticky let the browser handle this automatically. Fixed keeps an element always visible in the same spot, while sticky keeps it visible only after scrolling past it.
/* JavaScript needed to move menu on scroll */
window.onscroll = function() {
if(window.scrollY > 100) {
menu.style.top = '0';
} else {
menu.style.top = '100px';
}
};nav {
position: sticky;
top: 0;
background: white;
}You can create smooth, reliable sticky headers or fixed buttons that stay visible without extra code or glitches.
Think of a website where the navigation bar stays at the top as you scroll down, so you can always click to other pages without scrolling back up.
Manual scrolling adjustments are complicated and error-prone.
Position fixed keeps elements always visible in one spot.
Position sticky keeps elements visible only after scrolling past them.
Practice
position: fixed; do to an element on a webpage?Solution
Step 1: Understand fixed positioning
Elements withposition: fixed;stay in the same place on the screen regardless of scrolling.Step 2: Compare with other positions
Unlike normal flow or relative positioning, fixed elements do not move when the page scrolls.Final Answer:
Keeps the element always visible in the same spot on the screen, even when scrolling. -> Option AQuick Check:
Fixed = Always visible on screen [OK]
- Thinking fixed elements scroll with the page
- Confusing fixed with relative positioning
- Assuming fixed hides the element
Solution
Step 1: Identify sticky syntax
The correct way to make an element sticky is usingposition: sticky;with an offset liketop: 10px;.Step 2: Check other options
Fixed keeps element always on screen, absolute positions relative to nearest positioned ancestor, relative moves element relative to normal spot.Final Answer:
position: sticky; top: 10px; -> Option DQuick Check:
Sticky syntax = position: sticky + offset [OK]
- Using fixed instead of sticky
- Forgetting to add top offset
- Using relative or absolute incorrectly
<style>
header {
position: sticky;
top: 0;
background: lightblue;
padding: 1rem;
}
</style>
<header>Sticky Header</header>
<div style='height: 2000px;'>Content</div>Solution
Step 1: Understand sticky with top: 0
The header will scroll normally until it reaches the top of the viewport, then it sticks there.Step 2: Visualize scrolling effect
As you scroll down, the header remains visible stuck at the top, making it easy to access.Final Answer:
The header stays stuck at the top of the viewport when scrolling down. -> Option AQuick Check:
Sticky + top:0 = sticks at top on scroll [OK]
- Thinking sticky behaves like fixed always
- Assuming header scrolls away immediately
- Confusing top: 0 with bottom positioning
position: sticky; and it doesn't stick. What is a likely cause?Solution
Step 1: Check parent container overflow
Sticky positioning requires the parent container not to clip overflow;overflow: hidden;orautocan prevent sticky from working.Step 2: Understand sticky requirements
Sticky depends on scroll container; if parent clips overflow, sticky won't stick.Final Answer:
The parent container has overflow: hidden; or overflow: auto;. -> Option CQuick Check:
Sticky fails if parent clips overflow [OK]
- Switching to fixed without need
- Ignoring parent container styles
- Assuming tag type affects sticky
Solution
Step 1: Understand sticky sidebar behavior
Sticky lets the sidebar scroll normally until it reaches the top, then it sticks there, perfect for this use case.Step 2: Why not fixed or absolute?
Fixed would always keep sidebar visible, ignoring scroll position. Absolute positions relative to container but doesn't stick on scroll.Final Answer:
Use position: sticky; top: 0; on the sidebar inside a tall container. -> Option BQuick Check:
Sticky = scroll then stick at offset [OK]
- Using fixed which ignores scroll position
- Using absolute which doesn't stick on scroll
- Using relative which just shifts position
