Ask any question about Tailwind CSS here... and get an instant response.
Post this Question & Answer:
How can I implement dark mode toggling with Tailwind CSS?
Asked on Mar 09, 2026
Answer
Tailwind CSS supports dark mode by using a class-based strategy, allowing you to toggle between light and dark themes by adding or removing a class on a parent element. Here's how you can implement dark mode toggling using Tailwind's dark mode utilities.
<!-- BEGIN COPY / PASTE -->
<div class="dark:bg-gray-800 bg-white min-h-screen">
<button id="toggleDarkMode" class="p-2 bg-blue-500 text-white">
Toggle Dark Mode
</button>
<p class="dark:text-white text-black">
This text changes color based on the theme.
</p>
</div>
<script>
const toggleButton = document.getElementById('toggleDarkMode');
toggleButton.addEventListener('click', () => {
document.documentElement.classList.toggle('dark');
});
</script>
<!-- END COPY / PASTE -->Additional Comment:
- Tailwind's dark mode is configured in the `tailwind.config.js` file, typically set to "media" or "class".
- Using the "class" strategy, you can manually toggle dark mode by adding or removing the "dark" class on the root element.
- Ensure your Tailwind configuration is set to use "class" mode for this approach to work.
- Dark mode utilities in Tailwind are prefixed with "dark:" and apply styles when the "dark" class is present.
Recommended Links:
