Get started with 33% off your first certification using code: 33OFFNEW

Supporting Dark Mode in Tailwind CSS

3 min read
Published on 14th November 2023

Blog Image

Dark mode isn't just a cosmetic preference; it's become a fundamental feature that users have come to expect. And Tailwind CSS, the utility-first framework, has built-in provisions to help you implement dark mode with ease. Let's take a nocturnal journey through setting up dark mode in Tailwind, ensuring your users have a pleasant viewing experience, day or night.

Embracing the Dark Class

Tailwind makes toggling dark mode straightforward with the dark: variant. By default, Tailwind uses the prefers-color-scheme media query, which automatically detects if the user has requested a light or dark interface. But for manual control, you can enable class-based dark mode. In your tailwind.config.js, set the darkMode option to 'class':

// tailwind.config.js
module.exports = {
  darkMode: 'class',
  // ...
}

With this setup, you can control the darkness yourself. Sprinkle the dark: variant on your classes like so:

<div class="bg-white dark:bg-gray-800">
  <!-- Your content goes here -->
</div>

Designing for the Dark Side

When you design for dark mode, it's not just about inverting colors. It's about ensuring readability and visual comfort. Here's a tip: don't go absolute black (#000000). Use slightly lighter shades of gray to reduce the harshness on the eyes.

A Splash of Color

Managing colors in dark mode can be a bit of a balancing act. For example, a bright yellow that looks stunning in light mode might be jarring in dark mode. Adjust your palette accordingly:

<p class="text-yellow-500 dark:text-yellow-300">
  Stay sunny, San Diego!
</p>

Shadows and Shades

Shadows often need tweaking in dark mode to stand out. Consider softening or changing the color of your box shadows:

<div class="shadow-lg dark:shadow-gray-700/50">
  <!-- Content with shadows that adapt to the theme -->
</div>

Transitioning Between Themes

For a smooth transition between light and dark themes, use CSS transitions on your colors and backgrounds. Tailwind's transition-colors utility class can handle this gracefully.

<div class="transition-colors duration-500 bg-white dark:bg-gray-800">
  <!-- This div will fade between light and dark backgrounds -->
</div>

Automate with JavaScript

If you're allowing users to toggle dark mode manually, you'll need a bit of JavaScript. Here's a simple toggle function:

function toggleDarkMode() {
  const html = document.documentElement;
  if (html.classList.contains('dark')) {
    html.classList.remove('dark');
    localStorage.theme = 'light';
  } else {
    html.classList.add('dark');
    localStorage.theme = 'dark';
  }
}

Remember to check the user's preference on page load:

if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
  document.documentElement.classList.add('dark');
} else {
  document.documentElement.classList.remove('dark');
}

Testing Your Dark Mode

Don't deploy your dark mode theme without testing. Check the contrast, ensure images and media don't look out of place, and validate that all text is readable.

Tailwind CSS's utility-first approach makes supporting dark mode less of a hassle and more of a delight. With a few strategic class additions and some smart design adjustments, you can offer a dark mode experience that keeps your night owl users engaged and comfortable. Remember, the best dark mode design is one that feels as natural as the shift between day and night.