Ask any question about Tailwind CSS here... and get an instant response.
Post this Question & Answer:
How can I implement a sticky header using Tailwind CSS utilities?
Asked on May 27, 2026
Answer
To create a sticky header using Tailwind CSS, you can utilize the `sticky` and `top-0` utilities. These classes will ensure that your header remains fixed at the top of the viewport as you scroll.
<!-- BEGIN COPY / PASTE -->
<header class="sticky top-0 bg-white shadow-md">
<nav class="flex justify-between items-center p-4">
<div class="text-lg font-bold">My Website</div>
<ul class="flex space-x-4">
<li><a href="#" class="text-gray-700">Home</a></li>
<li><a href="#" class="text-gray-700">About</a></li>
<li><a href="#" class="text-gray-700">Contact</a></li>
</ul>
</nav>
</header>
<!-- END COPY / PASTE -->Additional Comment:
- The `sticky` utility makes the element stick to its nearest ancestor with a scrolling mechanism.
- `top-0` ensures the header stays at the top of the viewport.
- Adding `bg-white` and `shadow-md` provides a background color and shadow for better visibility.
- Ensure the parent container has a defined height for the sticky effect to work properly.
Recommended Links:
