Ask any question about Tailwind CSS here... and get an instant response.
How can I implement a sticky header with Tailwind CSS?
Asked on Dec 10, 2025
Answer
To create a sticky header using Tailwind CSS, you can utilize the "sticky" and "top-0" utility classes. These classes will ensure that your header remains 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 hover:text-blue-500">Home</a></li>
<li><a href="#" class="text-gray-700 hover:text-blue-500">About</a></li>
<li><a href="#" class="text-gray-700 hover:text-blue-500">Contact</a></li>
</ul>
</nav>
</header>
<!-- END COPY / PASTE -->Additional Comment:
- The "sticky" class makes the element stick to the top of the viewport.
- "top-0" ensures the element is positioned at the top without any offset.
- Adding "bg-white" and "shadow-md" provides a background color and shadow for better visibility.
- Ensure that the parent element has sufficient height to observe the sticky effect.
Recommended Links:
