Ask any question about Tailwind CSS here... and get an instant response.
Post this Question & Answer:
How can I implement dark mode toggling using Tailwind's dark variant?
Asked on Jan 01, 2026
Answer
Tailwind CSS provides a "dark" variant that allows you to easily implement dark mode by toggling a class on the root element. You can use the "dark" class to apply different styles when dark mode is active.
<!-- BEGIN COPY / PASTE -->
<html lang="en">
<body class="dark:bg-gray-900 bg-white text-gray-900 dark:text-white">
<div class="p-4">
<h1 class="text-2xl font-bold">Dark Mode Example</h1>
<p class="mt-2">Toggle dark mode to see the effect.</p>
</div>
<button onclick="document.body.classList.toggle('dark')" class="mt-4 p-2 bg-blue-500 text-white rounded">
Toggle Dark Mode
</button>
</body>
</html>
<!-- END COPY / PASTE -->Additional Comment:
- Tailwind's "dark" variant allows you to define styles for dark mode using the "dark:" prefix.
- To toggle dark mode, you can add or remove the "dark" class on the root element, typically the tag.
- This example uses a simple JavaScript function to toggle the "dark" class.
- Ensure your Tailwind configuration is set to use the "media" or "class" strategy for dark mode.
Recommended Links:
