Ask any question about Tailwind CSS here... and get an instant response.
Post this Question & Answer:
How can I use Tailwind CSS to create a sticky header that only appears after scrolling?
Asked on Apr 14, 2026
Answer
To create a sticky header that appears after scrolling using Tailwind CSS, you can utilize Tailwind's utility classes for positioning and JavaScript for scroll detection. Tailwind provides classes like "sticky" and "top-0" to help position elements, but you'll need JavaScript to toggle visibility based on scroll position.
<!-- BEGIN COPY / PASTE -->
<header id="sticky-header" class="fixed top-0 left-0 w-full bg-white shadow-md transform -translate-y-full transition-transform duration-300">
<nav class="container mx-auto p-4">
<h1 class="text-xl font-bold">My Sticky Header</h1>
</nav>
</header>
<script>
window.addEventListener('scroll', function() {
const header = document.getElementById('sticky-header');
if (window.scrollY > 100) {
header.classList.remove('-translate-y-full');
} else {
header.classList.add('-translate-y-full');
}
});
</script>
<!-- END COPY / PASTE -->Additional Comment:
- The "fixed" class positions the header at the top of the viewport.
- The "-translate-y-full" class initially hides the header by translating it out of view.
- JavaScript is used to toggle the "-translate-y-full" class based on scroll position.
- Ensure the header has a "bg-white" or similar background to stand out.
- Adjust the "window.scrollY > 100" value to control when the header appears.
Recommended Links:
