Deep Color Mode | Beginner Tutorial
\nDeep color mode (Dark Mode) is a common user interface design trend that provides a dark-toned interface to reduce eye strain and save device battery.
\nTailwind CSS supports the deep color mode (Dark Mode) feature, making it easy for developers to create dark versions of websites to adapt to users' system preferences.
\nTailwind CSS provides a modifier named dark, which allows you to define styles specifically for dark mode.
\nWhen the user's device or operating system is set to dark mode, styles with the dark: modifier will automatically take effect.
\nExample
\nIn light mode: Background color is white (bg-white), text color is black (text-black).
\nIn dark mode: Background color is black (dark:bg-black), text color is white (dark:text-white).
\n\n
Enabling Deep Color Mode
\nBy default, Tailwind uses a media strategy to detect dark mode, based on the user's system preference.
\nIf finer control is needed, the class strategy can also be used.
\n1. Enabling Dark Mode in tailwind.config.js
\nmodule.exports={\n darkMode: 'media', // Based on system preference (default)\n // darkMode: 'class', // Manually switch using class names\n theme: {\n extend: {},\n },\n plugins: [],\n }\n `media` mode: Automatically switches based on the user's operating system settings (through media query `prefers-color-scheme: dark`).
\n`class` mode: Allows developers to manually switch dark mode by adding the `dark` class to the `html` or `body` tag.
\n| Mode | \nDetection Method | \nControl Method | \nUse Case | \n
|---|---|---|---|
| `media` mode | \nMedia query based on system preference | \nAutomatic switching | \nSimpler use cases without custom logic | \n
| `class` mode | \nManually add `dark` class to `html` or `body` | \nCustom switching logic by developer | \nComplex scenarios requiring buttons or toggles | \n
Switching Logic Example (Class Mode)
\nTo support manual switching of dark mode instead of relying on system preferences, use the class strategy instead of the media strategy.
\nNow, the dark:{class} class no longer works based on prefers-color-scheme; instead, it activates when the dark class appears in the HTML tree:
\nExample
\n \n \n const toggle = document.getElementById('toggle-dark-mode');\n toggle.addEventListener('click', () => {\n document.documentElement.classList.toggle('dark');\n });\n \n Try it Β»\n\n
How to Use Dark Mode Utility Classes
\n1. Style Switching
\nUse the dark: prefix to specify styles for dark mode.
\nExample
\n2. Responsive and Dark Mode Together
\nResponsive breakpoints and dark mode can be used together:
\nExample
\nSmall screen font size is xl.
\nIn dark mode, the small screen font color becomes light gray.
\n3. Interactive States and Dark Mode Together
\nYou can apply styles to interactive states like hover, focus, etc., combined with dark mode:
\nExample
\n \n Try it Β»\nExtending Dark Mode
\nIn some cases, you may need to provide additional colors or styles for dark mode through theme.extend to customize the Tailwind configuration.
\nmodule.exports = { theme: { extend: { colors: { brand: { DEFAULT: '#1DA1F2', dark: '#0d74b8', }, }, }, },}\n Using extended colors in HTML:
\nExample
\n\n
YouTip