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
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.