Last updated on May 8th, 2025 at 08:13 am

Want to stick a header at the top or a footer at the bottom of your page that stays put while scrolling? Here’s a beginner-friendly guide to doing just that using CSS.

Goal

Fix a div so that it always remains:

  • At the top of the viewport (like a sticky navbar)
  • Or at the bottom (like a persistent footer or chat button)

Method 1: Use position: fixed

This is the most reliable method and works in all modern browsers.

Fix to the Top

<div class="fixed-top">This div sticks to the top</div>
.fixed-top {
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: #f8f9fa;
z-index: 1000;
}

Fix to the Bottom

<div class="fixed-bottom">This div sticks to the bottom</div>
.fixed-bottom {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
background-color: #f8f9fa;
z-index: 1000;
}

Tip: Use z-index to ensure your fixed div stays above other elements.

Common Pitfalls

  • Hidden Behind Content: Add top/bottom margin or padding to your page’s content to avoid it being hidden behind the fixed div.
  • Responsive Design: Don’t forget to test on mobile! Fixed elements can cover content on smaller screens.

Alternative: Use position: sticky (For Scroll-Aware Behavior)

sticky lets the div act like relative until a scroll threshold is passed.

.sticky-top {
position: sticky;
top: 0;
background-color: #fff;
}

This is great for sticky headers that scroll with the page until they hit the top.

Conclusion

Using position: fixed or sticky, you can easily keep elements pinned to the top or bottom of the screen for better navigation or user interaction.

Demo

Top
Bottom (height changed)