Dark variant helps you make your website look good in dark mode. It changes colors when users prefer dark backgrounds.
Dark variant usage in Tailwind
dark:<utility-class>
The dark: prefix applies styles only when dark mode is active.
You need to enable dark mode in your Tailwind config for this to work.
<div class="bg-white dark:bg-gray-800">Content</div><p class="text-black dark:text-white">Hello</p><button class="bg-blue-500 dark:bg-blue-700">Click me</button>This example shows a box and button that change colors when dark mode is toggled. Click anywhere to switch modes.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Dark Variant Example</title> <script src="https://cdn.tailwindcss.com"></script> <script> tailwind.config = { darkMode: 'class' } </script> <style> /* Just to center content */ body { display: flex; height: 100vh; justify-content: center; align-items: center; margin: 0; } </style> </head> <body class="bg-white dark:bg-gray-900"> <div class="p-6 rounded-lg shadow-lg bg-gray-100 dark:bg-gray-800 text-gray-900 dark:text-gray-100"> <h1 class="text-2xl font-bold mb-4">Dark Variant Example</h1> <p class="mb-2">This box changes colors based on dark mode.</p> <button class="px-4 py-2 bg-blue-600 dark:bg-blue-700 text-white dark:text-gray-900 rounded"> Button </button> </div> <script> // Toggle dark mode on click anywhere document.body.addEventListener('click', () => { document.documentElement.classList.toggle('dark'); }); </script> </body> </html>
Dark mode must be enabled in Tailwind config with darkMode: 'class' or 'media'.
Using darkMode: 'class' means you control dark mode by adding/removing the dark class on html or body.
Using darkMode: 'media' applies dark styles automatically based on user system preferences.
The dark: prefix applies styles only in dark mode.
You must enable dark mode in Tailwind config to use dark variants.
Dark variants help make your site look good and readable in dark mode.
Practice
dark: prefix do in Tailwind CSS?Solution
Step 1: Understand the
Thedark:prefix purposedark:prefix in Tailwind CSS is used to apply styles only when the website is in dark mode.Step 2: Compare with other prefixes
Other prefixes likehover:orsm:apply styles on hover or small screens, not related to dark mode.Final Answer:
Applies styles only when dark mode is active -> Option AQuick Check:
dark:= dark mode styles [OK]
- Confusing dark: with hover: or responsive prefixes
- Thinking dark: applies styles always
- Not enabling dark mode in config
Solution
Step 1: Identify correct dark mode syntax
Tailwind usesdark:prefix before the utility class to apply styles in dark mode, sodark:text-whiteis correct.Step 2: Check other options for syntax errors
text-white dark:text-blackapplies white text always and black text in dark mode, which is reversed. Options B and C have invalid syntax.Final Answer:
dark:text-white-> Option DQuick Check:
Correct dark mode prefix =dark:[OK]
- Placing :dark after class name
- Using dash instead of colon in dark prefix
- Reversing colors for dark and light modes
<button class="bg-white dark:bg-gray-800 text-black dark:text-white p-4">Click Me</button>
Solution
Step 1: Analyze classes for dark mode
The button hasbg-whiteanddark:bg-gray-800, so in dark mode background changes to gray-800.Step 2: Analyze text color in dark mode
Text color istext-blacknormally anddark:text-whitein dark mode, so text becomes white.Final Answer:
Gray-800 background with white text -> Option AQuick Check:
dark:bg-gray-800 + dark:text-white = Gray-800 background with white text [OK]
- Ignoring dark: prefix effects
- Mixing text and background colors
- Assuming colors stay same in dark mode
<div class="dark:bg-black bg-white">Content</div>
What is the most likely reason?
Solution
Step 1: Check Tailwind dark mode setup
Dark mode styles require enabling dark mode in Tailwind config file. Without it,dark:classes won't work.Step 2: Verify class syntax and usage
Class order does not affect dark mode.dark-bg-blackis invalid syntax. Dark mode works on any element, not just buttons.Final Answer:
You forgot to enable dark mode in Tailwind config -> Option CQuick Check:
Enable dark mode in config = dark styles work [OK]
- Not enabling dark mode in tailwind.config.js
- Using wrong class syntax for dark mode
- Thinking dark mode only applies to certain elements
<div class="?">Card Content</div>
Solution
Step 1: Identify light mode styles
Light mode requires white background and black text, sobg-white text-blackare correct for light mode.Step 2: Identify dark mode styles
Dark mode requires dark gray background and light gray text, sodark:bg-gray-900 dark:text-gray-300apply these styles in dark mode.Step 3: Check other options for correctness
bg-gray-900 text-gray-300 dark:bg-white dark:text-black reverses light and dark mode colors. bg-white dark:bg-gray-900 text-gray-300 dark:text-black mixes text colors incorrectly. dark:bg-white dark:text-black bg-gray-900 text-gray-300 places dark mode classes before light mode, which is allowed but colors are reversed.Final Answer:
bg-white text-black dark:bg-gray-900 dark:text-gray-300 -> Option BQuick Check:
Light mode white/black + dark mode gray/gray = bg-white text-black dark:bg-gray-900 dark:text-gray-300 [OK]
- Swapping light and dark mode colors
- Placing dark: classes incorrectly
- Using wrong color shades for readability
Ready to prove it?
Pass the test and this pattern moves unknown
