Skip to content

Sticky Header that Shrinks on Scroll

Sticky website header that shrinks its height, logo and wordmark after a short scroll, with a blurred backdrop and an accessible mobile menu.

Also known as: shrinking header, sticky navbar on scroll, header shrink on scroll, compact sticky navigation, sticky header with blur

Sticky Header that Shrinks on Scroll live demo

Open in new tab

Pure CSS code (HTML, CSS, JS)

A sticky website header built with plain CSS and a small vanilla JavaScript helper that shrinks its height, logo and wordmark once the visitor starts scrolling. It suits landing pages, shops and blogs where you want the navigation always within reach without a tall bar eating screen space.

How it works

The root .shk-page wraps everything. Its first child is an invisible .shk-sentinel positioned at the top of the page with a height of --shk-threshold. The header.shk-header uses position: sticky; top: 0, so it stays pinned while the page scrolls underneath.

The script creates an IntersectionObserver for the sentinel. As soon as the sentinel leaves the viewport, it adds .shk-header--compact to the header; scrolling back to the top removes it. The compact class swaps --shk-height for --shk-height-compact on .shk-bar, shrinks .shk-logo from --shk-logo to --shk-logo-compact, reduces the .shk-wordmark font size and tightens the .shk-cta padding. It also adds a soft shadow and a translucent, blurred background (with a solid fallback when backdrop-filter is not supported). All of these changes are animated with CSS transitions, so the script only flips one class and never touches inline styles. Older browsers without IntersectionObserver fall back to a passive scroll listener.

Below 860 px the links collapse into a dropdown. The .shk-toggle button opens .shk-nav by toggling .is-open and updating aria-expanded. The hamburger lines morph into a cross, and the panel closes again on Escape, on a link click or when the viewport grows to desktop width.

Customization

  • --shk-height and --shk-height-compact on .shk-page: the full and scrolled bar heights, e.g. 110px and 56px for a more dramatic shrink.
  • --shk-logo and --shk-logo-compact: the logo size before and after scrolling.
  • --shk-threshold: how far the page must scroll before the header shrinks; the sentinel takes this height.
  • --shk-speed: duration of every height, size and shadow transition.
  • --shk-accent, --shk-bg and --shk-ink: the green brand color, the cream background and the text color.
  • Replace the inline SVG inside .shk-brand and the .shk-wordmark text with your own logo and name.
  • Remove the main.shk-content demo block and place the header at the top of your own page; keep .shk-sentinel right before it.

Use cases

  • Landing pages: the header stays pinned and shrinks its height and logo after a short scroll
  • Online shops: navigation stays in reach with a call-to-action button
  • Blogs: a blurred translucent backdrop keeps the bar light over content
  • Mobile sites: below 860 px the links collapse into a dropdown with a hamburger toggle
  • Content sites: the current page link is marked and Escape closes the menu

Accessibility

The navigation is a nav landmark with aria-label="Main", and the current page link carries aria-current="page". The mobile toggle is a real button with aria-expanded and aria-controls, plus a visually hidden label. All interactive elements show a clear focus outline, and Escape returns focus to the toggle. With prefers-reduced-motion: reduce the header still changes size, but instantly.

Usage notes

Sticky positioning stops working if an ancestor of .shk-header has overflow: hidden or overflow: auto. Several .shk-page roots on one page are supported because the script scopes all queries to each root.

Sticky Header that Shrinks on Scroll at a glance

Framework
Pure CSS
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
None
Layout
Responsive, works on mobile
Category
Navbars
License
Free for personal and commercial use

Questions about this template

Can I use the Sticky Header that Shrinks on Scroll code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

There is nothing to install. Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

No dependencies. Pure HTML, CSS and JavaScript.

Sticky Header that Shrinks on Scroll – Pure CSS navbars component screenshot
Sticky Header that Shrinks on Scroll · Pure CSS Navbars

More Navbars

Navbar with Sliding Active Indicator – Pure CSS navbars preview

Navbar with Sliding Active Indicator

Dark responsive navbar with a glowing underline that slides between links on hover, focus and click, built with CSS custom properties and vanilla JS.

Navbar with Offcanvas Drawer on Mobile – Bootstrap 6 navbars preview

Navbar with Offcanvas Drawer on Mobile

Responsive Bootstrap 6 navbar that shows inline links on desktop and moves them into a slide-in Drawer with icons, studio hours and social links on small screens.

Navbar with Overflow Menu (NavOverflow) – Bootstrap 6 navbars preview

Navbar with Overflow Menu (NavOverflow)

Bootstrap 6 news navbar built on the new NavOverflow component: links that do not fit move into a More menu automatically as the width shrinks.