Ask any question about Tailwind CSS here... and get an instant response.
Post this Question & Answer:
How can I implement dark mode toggle with Tailwind without using JavaScript?
Asked on Jan 29, 2026
Answer
In Tailwind CSS, you can implement a dark mode toggle using the `dark` variant, which allows you to apply styles conditionally based on a parent class. This approach requires toggling a class on the root element (like `html`) using a server-side language or a framework that supports class manipulation.
<!-- BEGIN COPY / PASTE -->
<html class="dark">
<body class="bg-white dark:bg-gray-900 text-black dark:text-white">
<div class="p-4">
<h1 class="text-2xl font-bold">Dark Mode Example</h1>
<p>This text changes color based on the mode.</p>
</div>
</body>
</html>
<!-- END COPY / PASTE -->Additional Comment:
- To toggle dark mode, you can add or remove the "dark" class on the `html` element using server-side logic or a framework.
- Tailwind's `dark` variant allows you to define styles that apply when the "dark" class is present.
- This method does not require JavaScript but relies on a mechanism to toggle the class, such as user preferences stored in a database.
- Ensure your Tailwind configuration has `darkMode: 'class'` set to enable this feature.
Recommended Links:
