Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Tailwind CSS Dark Mode

Tailwind CSS Dark Mode

Dark Mode is a common UI design trend that provides a dark-toned interface to reduce eye strain and save device power.

Tailwind CSS supports Dark Mode, allowing developers to easily create a dark version of a website to suit users' system preferences.

Tailwind CSS provides a modifier named dark, allowing you to define styles for dark mode.

When a user's device or operating system is set to dark mode, styles with the dark: modifier take effect automatically.

The following example sets styles based on the media query for system preferences:

Example

  This text changes according to the color mode.

Try it »

**In light mode:** the background color is white (bg-white), and the text color is black (text-black)。

**In dark mode:** the background color is black (dark:bg-black), and the text color is white (dark:text-white)。


Enable Dark Mode

By default, Tailwind uses the media strategy to detect dark mode, based on the user's system preferences.

If you need more fine-grained control, you can also use the class strategy.

1. Enable Dark Mode in tailwind.config.js

Example

module.

exports

=

{

  darkMode

:

'media'

,

// Based on system preferences (default)

 

// darkMode: 'class', // Manual switching based on class name

  theme

:

{

    extend

:

{

}

,

 

}

,

  plugins

:

[

]

,

}

Differences Between the Two Modes:

Mode Detection Method Control Method Use Cases
media Mode Media query based on system preferences Automatic switching Simple application scenarios, no custom logic needed
class Mode Based on manually adding the html or body on the element, add dark class Switching logic customized by the developer Complex scenarios requiring a button or toggle

Switching Logic Example (class Mode)

If you want to support manual switching of dark mode instead of relying on operating system preferences, use the class strategy instead of the media strategy.

Now, the dark:{class} classes will no longer work based on prefers-color-scheme, but when the dark class appears in the HTML tree:







  
...
...

Example

Try it »


How to Use Dark Mode Utility Classes

1. Style Switching

Use the dark: prefix to specify styles for dark mode.

Example

  A design that adapts to light and dark modes.

Try it »

2. Combining Responsive and Dark Mode

Responsive breakpoints and dark mode can be used together:

Example

  Responsive text that also supports dark mode.

Try it »

Font is xl on small screens.

In dark mode, the font color on small screens becomes light gray.

3. Combining Interactive States with Dark Mode

You can combine states like hover and focus with dark mode to apply styles:

Example

Try it »

Extending Dark Mode

In some cases, you may need to provide extra colors or styles for dark mode, which you can do by customizing the Tailwind config via theme.extend.

module.exports = {

  theme: {

    extend: {

      colors: {

        brand: {

          DEFAULT: '#1DA1F2',

          dark: '#0d74b8',

        },

      },

    },

  },

}

Using extended colors in HTML:

Example

  Custom dark mode colors!

Try it »


Example

The following is a complete example of implementing dark mode switching:

Example

Welcome to Dark Mode

Try it »