Ask any question about Tailwind CSS here... and get an instant response.
Post this Question & Answer:
How can I use Tailwind CSS to create a dark mode toggle for my website?
Asked on Dec 17, 2025
Answer
To create a dark mode toggle using Tailwind CSS, you can utilize Tailwind's dark mode feature, which allows you to apply different styles based on a dark or light theme. This typically involves toggling a class on the HTML element.
<!-- BEGIN COPY / PASTE -->
<html lang="en">
<head>
<script>
function toggleDarkMode() {
document.documentElement.classList.toggle('dark');
}
</script>
</head>
<body class="bg-white dark:bg-gray-900 text-black dark:text-white">
<button onclick="toggleDarkMode()" class="p-2 bg-gray-200 dark:bg-gray-800 rounded">
Toggle Dark Mode
</button>
<div class="p-4">
<p class="text-lg">This is a paragraph with responsive text color.</p>
</div>
</body>
</html>
<!-- END COPY / PASTE -->Additional Comment:
- Tailwind's dark mode is configured in the `tailwind.config.js` file, typically using the `media` or `class` strategy.
- The example uses the `class` strategy, which toggles the `dark` class on the HTML element.
- Ensure your Tailwind configuration is set to use dark mode by adding `darkMode: 'class'` in the config file.
- Use `dark:` prefix in class names to apply styles in dark mode.
- This approach requires JavaScript to toggle the class, as shown in the example.
Recommended Links:
