Source Code
<nav class="navbar navbar-expand-lg bsshrink-bar fixed-top" id="bsshrinkNav">
<div class="container">
<a class="navbar-brand fw-bold" href="javascript:void(0)"><span class="bsshrink-dot"></span>Northwind</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#bsshrinkCollapse">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="bsshrinkCollapse">
<ul class="navbar-nav ms-auto">
<li class="nav-item"><a class="nav-link" href="javascript:void(0)">Product</a></li>
<li class="nav-item"><a class="nav-link" href="javascript:void(0)">Pricing</a></li>
<li class="nav-item"><a class="nav-link" href="javascript:void(0)">Docs</a></li>
<li class="nav-item ms-lg-2"><button class="btn btn-primary btn-sm">Sign up</button></li>
</ul>
</div>
</div>
</nav>
<main class="bsshrink-stage" id="bsshrinkStage">
<div class="bsshrink-spacer"></div>
<p class="bsshrink-hint">Scroll down inside this preview — the navbar shrinks its padding and adds a shadow past 40px of scroll, then grows back when you scroll to the top.</p>
<p class="bsshrink-hint">State: <strong id="bsshrinkState">expanded</strong></p>
<div class="bsshrink-spacer bsshrink-tall"></div>
</main>body { margin: 0; }
.bsshrink-bar {
background: #fff;
padding-block: 18px;
border-bottom: 1px solid transparent;
transition: padding .2s ease, box-shadow .2s ease, border-color .2s ease;
}
.bsshrink-bar.bsshrink-scrolled {
padding-block: 8px;
box-shadow: 0 2px 14px rgba(15,23,42,.08);
border-color: #eceef1;
}
.bsshrink-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: #6366f1; margin-right: 7px; }
.bsshrink-stage { padding-top: 90px; height: 100vh; overflow-y: auto; box-sizing: border-box; background: #f6f7f9; }
.bsshrink-spacer { height: 30vh; }
.bsshrink-tall { height: 120vh; }
.bsshrink-hint { text-align: center; font-size: 13px; color: #6b7280; padding: 0 24px; }
.bsshrink-hint strong { color: #6366f1; }const nav = document.getElementById('bsshrinkNav');
const stage = document.getElementById('bsshrinkStage');
const stateEl = document.getElementById('bsshrinkState');
stage.addEventListener('scroll', () => {
const scrolled = stage.scrollTop > 40;
nav.classList.toggle('bsshrink-scrolled', scrolled);
stateEl.textContent = scrolled ? 'scrolled (shrunk)' : 'expanded';
});Bootstrap Sticky Navbar with Scroll Shrink — Free Snippet
Bootstrap Sticky Navbar with Scroll Shrink · Navigation · Plain HTML, CSS & JS · Live preview
What's included
Features
About this UI Snippet
Bootstrap Sticky Navbar with Scroll Shrink — HTML, CSS & JavaScript

A navbar fixed to the top of the page can start to feel oversized once a visitor has scrolled past the hero — this snippet fixes that by shrinking the navbar's own padding on scroll, using real Bootstrap 5.3's navbar/navbar-collapse component underneath.
A single scroll listener compares the container's scrollTop against a 40px threshold and toggles one class, .bsshrink-scrolled, which reduces the navbar's vertical padding, adds a subtle shadow, and fades in a bottom border — all through a CSS transition, so the resize itself is smooth rather than a hard snap. The threshold is intentionally low: a navbar should visually settle almost immediately once scrolling starts, not wait for a large scroll distance.
Build with AI
Build, Understand, Optimize, and Extend It With AI
Hand this snippet's HTML, CSS, and JS to an AI coding assistant like Claude and ask it to debounce or throttle the scroll listener for very scroll-heavy pages, or to add a second threshold that hides the navbar entirely when scrolling down fast and reveals it again on scroll-up. It's also a good exercise to ask the assistant to convert the scroll listener into an IntersectionObserver watching a sentinel element instead of reading scrollTop directly.
Prompt to recreate it
Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:
Build a Bootstrap 5.3 fixed-top navbar that shrinks on scroll, using the real Bootstrap CDN framework (bootstrap.min.css and bootstrap.bundle.min.js), not custom CSS made to resemble Bootstrap.
Requirements:
- A real Bootstrap navbar (navbar-expand-lg, fixed-top) with a brand, nav links, and Bootstrap's own hamburger collapse for mobile.
- A single scroll event listener that compares scroll position against a configurable threshold constant and toggles one CSS class on the navbar when crossing it.
- That class must reduce the navbar's vertical padding, add a box-shadow, and fade in a bottom border, all via CSS transitions — no JavaScript animation.
- The navbar must expand back to its original state automatically when scrolled back above the threshold.Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.
Step by step
How to Use
- 1Load the snippetClick the snippet in the sidebar Library tab. The preview loads with a tall, padded navbar at the top.
- 2Scroll the previewScroll down inside the preview past about 40px — the navbar's padding shrinks and a shadow fades in.
- 3Scroll back to the topThe navbar expands back to its original padding and the shadow disappears.
- 4Adjust the thresholdChange the 40 in the JS panel's scrollTop comparison to control how far a visitor must scroll before it shrinks.
- 5Export in your formatClick "HTML" for a standalone file, "JSX" for React, or "Tailwind" for React + Tailwind CSS.
Real-world uses
Common Use Cases
Got questions?
Frequently Asked Questions
Yes — the actual bootstrap.min.css and bootstrap.bundle.min.js loaded from a CDN, using Bootstrap's genuine navbar and Collapse component for the mobile hamburger menu.
A scroll event listener compares the scrollable container's scrollTop against a 40px threshold and toggles one CSS class; the padding/shadow/border changes are all handled by a CSS transition on that class.
Yes — swap the listener from the preview's scroll container to window and read window.scrollY instead of stage.scrollTop; the class-toggling logic is identical either way.
No — only padding, box-shadow, and border-color change, none of which affect the position of content below the fixed navbar, so nothing jumps.
Yes — add a second rule under .bsshrink-scrolled targeting .bsshrink-dot or the brand text's font-size with its own transition; it will animate in sync with the padding change.