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 Dec 28, 2025
Answer
To create a sticky header using Tailwind CSS, you can utilize the "sticky" and "top-0" utilities. These classes will make your header stick to the top of the viewport as you scroll.
<!-- BEGIN COPY / PASTE -->
<header class="sticky top-0 bg-white shadow-md">
<nav class="max-w-7xl mx-auto p-4">
<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 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 height greater than the viewport to see the sticky effect.
Recommended Links:
