Ask any question about Tailwind CSS here... and get an instant response.
Post this Question & Answer:
How can I create a sticky header with Tailwind CSS that also supports dark mode?
Asked on May 26, 2026
Answer
To create a sticky header with Tailwind CSS that supports dark mode, you can use Tailwind's utility classes for positioning and dark mode support. Tailwind provides a "sticky" utility for positioning and "dark" variant for styling in dark mode.
<!-- BEGIN COPY / PASTE -->
<header class="sticky top-0 bg-white dark:bg-gray-800 text-black dark:text-white p-4 shadow-md">
<h1 class="text-lg font-bold">Sticky Header</h1>
</header>
<!-- END COPY / PASTE -->Additional Comment:
- The "sticky" class makes the header stick to the top of the viewport.
- "top-0" ensures the header is positioned at the top.
- "bg-white" and "dark:bg-gray-800" switch the background color based on the theme.
- "text-black" and "dark:text-white" adjust the text color for dark mode.
- Ensure your project is configured to support dark mode by setting it in your Tailwind configuration file.
Recommended Links:
