Core Concepts

Dark Mode

Using Tailwind CSS to style your site in dark mode.

Basic usage

Now that dark mode is a first-class feature of many operating systems, it’s becoming more and more common to design a dark version of your website to go along with the default design.

To make this as easy as possible, Tailwind includes a dark variant that lets you style your site differently when dark mode is enabled:

Dark Mode - 图1

  1. <div class="bg-white dark:bg-gray-900 rounded-lg px-6 py-8 ring-1 ring-gray-900/5 shadow-xl">
  2. <div>
  3. <span class="inline-flex items-center justify-center p-2 bg-indigo-500 rounded-md shadow-lg">
  4. <svg class="h-6 w-6 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true"><!-- ... --></svg>
  5. </span>
  6. </div>
  7. <h3 class="text-gray-900 dark:text-white mt-5 text-base font-medium tracking-tight">Writes Upside-Down</h3>
  8. <p class="text-gray-500 dark:text-gray-400 mt-2 text-sm">
  9. The Zero Gravity Pen can be used to write in any orientation, including upside-down. It even works in outer space.
  10. </p>
  11. </div>

By default this uses the prefers-color-scheme CSS media feature, but you can also build sites that support toggling dark mode manually using the ‘class’ strategy.


Toggling dark mode manually

If you want to support toggling dark mode manually instead of relying on the operating system preference, use the class strategy instead of the media strategy:

tailwind.config.js

  1. module.exports = {
  2. darkMode: 'class',
  3. // ...
  4. }

Now instead of dark:{class} classes being applied based on prefers-color-scheme, they will be applied whenever dark class is present earlier in the HTML tree.

  1. <!-- Dark mode not enabled -->
  2. <html>
  3. <body>
  4. <!-- Will be white -->
  5. <div class="bg-white dark:bg-black">
  6. <!-- ... -->
  7. </div>
  8. </body>
  9. </html>
  10. <!-- Dark mode enabled -->
  11. <html class="dark">
  12. <body>
  13. <!-- Will be black -->
  14. <div class="bg-white dark:bg-black">
  15. <!-- ... -->
  16. </div>
  17. </body>
  18. </html>

How you add the dark class to the html element is up to you, but a common approach is to use a bit of JS that reads a preference from somewhere (like localStorage) and updates the DOM accordingly.

Here’s a simple example of how you can support light mode, dark mode, as well as respecting the operating system preference:

  1. // On page load or when changing themes, best to add inline in `head` to avoid FOUC
  2. if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
  3. document.documentElement.classList.add('dark')
  4. } else {
  5. document.documentElement.classList.remove('dark')
  6. }
  7. // Whenever the user explicitly chooses light mode
  8. localStorage.theme = 'light'
  9. // Whenever the user explicitly chooses dark mode
  10. localStorage.theme = 'dark'
  11. // Whenever the user explicitly chooses to respect the OS preference
  12. localStorage.removeItem('theme')

Again you can manage this however you like, even storing the preference server-side in a database and rendering the class on the server — it’s totally up to you.