Sticky Navbar
A sticky navigation bar component that freezes with a glass morphism effect and changes to a compact style as you scroll.
Preview
Simulates a glassmorphic fixed navigator with scrolling. Click the button in the preview to see the scrolling effect.
Use cases and features
-
Landing Page/Portfolio: The navigator’s expression changes as you scroll, giving it a well-crafted feel.
-
Suitable for dark themes: Glassmorphism’s
backdrop-filter: blurlooks especially good on dark backgrounds. -
Utilizing
position: sticky: Maximize performance by leveragingposition: stickyin CSS instead of relying only on JavaScript.
Points of design and UX
-
Fires at scroll 10px: Glass morphism appears at neutral timing.
-
Change in padding: Reduced from
1.25rem to 0.75remto make it more compact and secure a wider content area. -
Smooth with
transition: Create natural changes by simultaneously transitioning the four properties of background, blur, box-shadow, and padding.
How to customize
Change scroll start threshold:
// trigger earlier (5px)
if (demo.scrollTop > 5) { ... }
// trigger later (50px)
if (demo.scrollTop > 50) { ... }
add link:
<li><a href="#services">service</a></li>
<li><a href="#blog">Blog</a></li>
Implementation code
<nav class="snav-bar" id="sticky-nav">
<div class="snav-logo">BRAND</div>
<ul class="snav-links">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Work</a></li>
</ul>
<button class="snav-cta">Contact</button>
</nav>
<style>
.snav-bar {
position: sticky;
top: 0;
z-index: 100;
width: 100%;
box-sizing: border-box;
padding: 1.25rem 2rem;
display: flex;
align-items: center;
justify-content: space-between;
gap: 1.5rem;
transition: background 0.3s ease, backdrop-filter 0.3s ease, box-shadow 0.3s ease, padding 0.3s ease;
}
.snav-bar.scrolled {
background: rgba(15,23,42,0.75);
backdrop-filter: blur(16px);
box-shadow: 0 1px 0 rgba(255,255,255,0.08), 0 8px 32px rgba(0,0,0,0.4);
padding: 0.75rem 2rem;
}
</style>
<script>
window.addEventListener('scroll', function() {
var nav = document.getElementById('sticky-nav');
if (window.scrollY > 10) nav.classList.add('scrolled');
else nav.classList.remove('scrolled');
});
</script> Browser compatibility status
| Browser | Compatibility status |
|--------|-------|
| Chrome 76+ | ✅ Fully compatible |
| Firefox 103+ | ✅ Compatible |
| Safari 9+ | ✅ Fully compatible |
| Edge 79+ | ✅ Fully compatible |
backdrop-filteris not supported by some older browsers, but a semi-transparent background ofbackground: rgba(...)is sufficient as a fallback.