Ask any question about Tailwind CSS here... and get an instant response.
Post this Question & Answer:
How can I implement a sticky header with Tailwind CSS while maintaining smooth scrolling?
Asked on Feb 14, 2026
Answer
To create a sticky header with Tailwind CSS, you can use the `sticky` utility class along with `top-0` to keep the header at the top of the viewport. This allows the header to remain visible while scrolling smoothly through the rest of the page content.
<!-- BEGIN COPY / PASTE -->
<header class="sticky top-0 bg-white shadow-md">
<div class="max-w-7xl mx-auto px-4 py-2">
<h1 class="text-xl font-bold">Sticky Header</h1>
</div>
</header>
<main class="mt-4">
<div class="max-w-7xl mx-auto px-4">
<p class="mb-4">... Your content here ...</p>
<!-- Repeat content to demonstrate scrolling -->
<p class="mb-4">... More content ...</p>
<p class="mb-4">... More content ...</p>
<p class="mb-4">... More content ...</p>
</div>
</main>
<!-- END COPY / PASTE -->Additional Comment:
- The `sticky` class makes the element stick to its nearest scrolling ancestor, which is typically the viewport.
- Using `top-0` ensures the header sticks to the top of the page.
- Adding `bg-white` and `shadow-md` helps the header stand out against the content.
- Ensure the content below the header is long enough to scroll, demonstrating the sticky effect.
Recommended Links:
