Skip to main content
Login Join
Snippet · CSS

Sticky Header on Scroll

Shared by Ankit Panchal · July 26, 2026

15 views
1 upvote
Back to Snippets

2 approaches to this. Compare them and pick what fits your setup.

Advanced customization by Ankit Panchal

Keeps your site header fixed to the top of the screen as visitors scroll, so navigation stays within reach on long pages. Includes a -webkit- fallback for older browsers and a variable-driven setup so you can tweak the height, colour, and shadow in one place. Add the .site-header class to your header element.

:root {
    --header-bg: #fff;
    --header-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    --header-z: 999;
}

.site-header {
    position: -webkit-sticky; /* Safari fallback */
    position: sticky;
    top: 0;
    z-index: var(--header-z);
    background: var(--header-bg);
    box-shadow: var(--header-shadow);
}

/* Optional: only show the shadow once the user starts scrolling */
.site-header.is-scrolled {
    box-shadow: var(--header-shadow);
}
.site-header {
    transition: box-shadow 0.2s ease;
}
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments