Create beautiful dark themes for your website with Tailwind's intuitive dark mode utilities
Tailwind CSS provides a powerful dark mode feature that allows you to design both light and dark versions of your website using variant modifiers. You can create responsive, accessible dark themes with minimal effort.
Try it out: Toggle the switch above to see this page in dark mode!
Tailwind CSS offers two strategies for implementing dark mode:
Uses a class on the html element to control dark mode. This gives you control over when dark mode is enabled.
// tailwind.config.js
module.exports = {
darkMode: 'class',
// ...
}
You control dark mode with JavaScript by adding or removing the dark class to the HTML element.
Uses the user's system preference to determine if dark mode should be enabled. This is controlled by the prefers-color-scheme media query.
// tailwind.config.js
module.exports = {
darkMode: 'media',
// ...
}
Dark mode will automatically be enabled when the user has dark mode enabled in their operating system preferences.
To style elements differently in dark mode, use the dark: variant in front of any utility class:
<div class="bg-white dark:bg-slate-800 text-black dark:text-white">
This div will have a white background and black text in light mode,
but a dark slate background and white text in dark mode.
</div>
This card changes background and text colors.
Code: bg-white dark:bg-slate-700 text-gray-800 dark:text-white
Brand colors can also be adapted for dark mode.
Code: bg-indigo-50 dark:bg-indigo-900
Borders can be more prominent in dark mode.
Code: border-gray-300 dark:border-blue-500
Sometimes you might want to reduce or remove shadows in dark mode.
Code: shadow-lg dark:shadow-none
Here are some common patterns and practices for designing effective dark mode interfaces:
bg-black) - use dark grays like bg-slate-900 instead
text-gray-200 instead of text-white for body text
dark:border-gray-600) for separation instead of shadows
bg-gray-800 vs bg-gray-900) to create depth
When using the class strategy, you'll need a way for users to toggle between light and dark mode. Here's a complete implementation:
<!-- Toggle Switch HTML -->
<div class="relative inline-block w-10 mr-2 align-middle">
<input
type="checkbox"
name="toggle"
id="toggle"
class="toggle-checkbox absolute block w-6 h-6
rounded-full bg-white border-4 appearance-none
cursor-pointer"
/>
<label
for="toggle"
class="toggle-label block overflow-hidden h-6
rounded-full bg-gray-300 cursor-pointer"
></label>
</div>
<span id="toggle-label">Light Mode</span>
<style>
.toggle-checkbox:checked {
right: 0;
border-color: #4F46E5;
}
.toggle-checkbox:checked + .toggle-label {
background-color: #4F46E5;
}
</style>
// Toggle dark mode with JS
document.getElementById('toggle').addEventListener('change', function() {
if (this.checked) {
// Enable dark mode
document.documentElement.classList.add('dark');
document.getElementById('toggle-label').innerHTML = 'Dark Mode';
// Save preference to localStorage
localStorage.setItem('darkMode', 'enabled');
} else {
// Disable dark mode
document.documentElement.classList.remove('dark');
document.getElementById('toggle-label').innerHTML = 'Light Mode';
// Save preference to localStorage
localStorage.setItem('darkMode', 'disabled');
}
});
// Check for saved preference (on page load)
if (localStorage.getItem('darkMode') === 'enabled') {
document.documentElement.classList.add('dark');
document.getElementById('toggle').checked = true;
document.getElementById('toggle-label').innerHTML = 'Dark Mode';
}
Try toggling this switch to see the dark mode in action:
This card will change appearance when you toggle the switch above, without affecting the entire page's dark mode setting.
Here are some real-world components styled for both light and dark modes:
A beautiful mountain getaway with amazing views and fresh air.
<div class="bg-white dark:bg-slate-800 rounded-xl
border border-gray-100 dark:border-gray-700
shadow-lg">
<img class="w-full h-48 object-cover" src="img.jpg">
<div class="px-4 py-4">
<h5 class="font-bold text-xl mb-2
text-gray-900 dark:text-white">
Mountain Retreat
</h5>
<p class="text-gray-600 dark:text-gray-400 text-sm">
A beautiful mountain getaway.
</p>
</div>
<div class="px-4 py-3 bg-gray-50
dark:bg-slate-700/50
border-t border-gray-100
dark:border-gray-700">
<span class="text-gray-900 dark:text-white
font-bold">$199</span>
<button class="bg-indigo-600 hover:bg-indigo-700
text-white py-1 px-3 rounded text-sm
font-medium float-right">
Book Now
</button>
</div>
</div>
Don't have an account? Sign up
Note how the form adjusts in dark mode:
Follow these best practices to create an effective dark mode experience:
class and media
dark: variant makes it easy to style elements differently in dark mode