Sticky headers are a common UI pattern that keeps your navigation visible as users scroll, improving usability and accessibility. With Tailwind CSS and a touch of vanilla JavaScript, you can build a smooth, performant sticky header that changes style when users scroll past a threshold.
HTML Structure & Styling
Start with a basic header layout:
<header id="site-header" class="bg-white shadow-md fixed top-0 left-0 w-full transition-all duration-300">
<div class="max-w-6xl mx-auto px-4 py-3 flex items-center justify-between">
<a href="/" class="text-xl font-bold">MySite</a>
<nav class="space-x-6">
<a href="#features" class="text-gray-700 hover:text-gray-900">Features</a>
<a href="#pricing" class="text-gray-700 hover:text-gray-900">Pricing</a>
<a href="#contact" class="text-gray-700 hover:text-gray-900">Contact</a>
</nav>
</div>
</header>
<main class="pt-20">
<!-- Page content -->
</main>
-
fixed
,top-0
, andw-full
keep it pinned at the top -
transition-all duration-300
enables smooth style changes - The
pt-20
on<main>
accounts for header height to avoid content overlap
Change Styles on Scroll
Use vanilla JavaScript to toggle classes based on scroll position:
const header = document.getElementById('site-header');
const scrollThreshold = 50;
window.addEventListener('scroll', () => {
if (window.scrollY > scrollThreshold) {
header.classList.add('bg-white', 'shadow-lg');
header.classList.remove('bg-transparent', 'shadow-md');
} else {
header.classList.add('bg-transparent', 'shadow-md');
header.classList.remove('bg-white', 'shadow-lg');
}
});
- This switches between transparent and solid backgrounds
- Adjusts shadows to indicate the header’s state
Adding a Back-to-Top Button
Pair it with a back-to-top button that appears after scrolling:
<button id="back-to-top"
class="hidden fixed bottom-4 right-4 bg-indigo-600 text-white p-3 rounded-full shadow-lg transition-opacity duration-300">
↑
</button>
And the JavaScript:
const btn = document.getElementById('back-to-top');
window.addEventListener('scroll', () => {
btn.classList.toggle('hidden', window.scrollY < 300);
});
btn.addEventListener('click', () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
This gives users a convenient way to navigate to the top when content is long.
Responsiveness & Dark Mode
- Use Tailwind’s responsive classes (
sm:
,md:
) to adjust nav spacing - Leverage
dark:
to style header in dark mode:
<header class="bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100">
Maintain theme consistency based on user preference or toggle state.
Accessibility Tips
- Ensure adequate color contrast (check WCAG)
- Use
aria-label
on the back-to-top button - Include
skip to content
links for better keyboard navigation
Performance & UX
- Use
requestAnimationFrame
if scroll performance becomes choppy - Keep JavaScript minimal and avoid heavy libraries for better speed
- Tailwind keeps CSS clean, while small JS snippets manage logic
Build Clean & Scalable Headers
By combining Tailwind’s utility-first design with minimal JavaScript, you can create sticky navigation that’s responsive, accessible, and elegant—perfect for modern sites and blogs.
Need a deeper dive into scalable UI patterns, design tokens, theming, and component architecture? Check out:
Mastering Tailwind at Scale: Architecture, Patterns & Performance
It’s a complete guide for building maintainable, high-performance design systems with Tailwind—just $10.
Enhance your site’s navigation experience with clean, efficient, and stylish components built with Tailwind CSS.
Top comments (0)