What if you could change your entire website's colors with just one simple class?
Why Class-based dark mode strategy in Tailwind? - Purpose & Use Cases
Imagine you want your website to switch between light and dark themes. You try to change colors by editing every element's style manually for dark mode.
This means changing many CSS rules one by one. It's slow, easy to miss some parts, and hard to keep consistent. Every time you add new content, you must remember to update dark styles too.
Using a class-based dark mode strategy, you add a single class like dark to a parent element. Tailwind automatically applies dark styles only when that class is present, making theme switching simple and reliable.
body { background: white; color: black; }
.dark body { background: black; color: white; }
/* Need to write many rules for each element */<html class="dark"> <body class="bg-white text-black dark:bg-black dark:text-white"> <!-- content --> </body> </html>
You can toggle dark mode by adding or removing one class, instantly changing the whole site's look without rewriting styles everywhere.
Many popular websites let users switch to dark mode with a button. Behind the scenes, they just add or remove a dark class on the root element, and Tailwind handles the rest.
Manually styling dark mode is slow and error-prone.
Class-based dark mode uses one class to control all dark styles.
This makes theme switching easy, consistent, and scalable.
Practice
dark: prefix in Tailwind CSS do when using class-based dark mode?Solution
Step 1: Understand the
Thedark:prefix roledark:prefix in Tailwind applies styles only when thedarkclass is present on a parent element.Step 2: Compare with other options
It does not auto-detect system settings or disable light styles; it depends on thedarkclass presence.Final Answer:
Applies styles only when a parent element has thedarkclass. -> Option AQuick Check:
dark:prefix = styles withdarkclass [OK]
dark: works only if dark class exists [OK]- Thinking
dark:auto-detects system dark mode - Assuming it changes styles without the
darkclass - Confusing
dark:with global dark mode toggle
Solution
Step 1: Check Tailwind config for dark mode strategy
To use class-based dark mode, you must setdarkMode: 'class'intailwind.config.js.Step 2: Eliminate other options
darkMode: 'media'uses system preference, not class. Thedarkclass must be added manually; nodarkattribute exists. The class name must be exactlydark.Final Answer:
"darkMode: 'class'" in tailwind.config.js -> Option BQuick Check:
Config setting for class strategy = 'class' [OK]
- Using 'media' instead of 'class' in config
- Adding wrong attribute instead of class
- Using incorrect class name like 'dark-mode'
<html class="dark">
<body>
<div class="bg-white dark:bg-gray-800 text-black dark:text-white p-4">Hello</div>
</body>
</html>What background and text colors will the <div> have when rendered?
Solution
Step 1: Identify presence of
Thedarkclass<html>tag has thedarkclass, so alldark:styles apply.Step 2: Apply styles based on class
Thedivhasbg-whiteandtext-blacknormally, but withdark:bg-gray-800anddark:text-whitewhendarkclass is present. So background is gray-800 and text is white.Final Answer:
Background gray-800, text white -> Option CQuick Check:
Dark class present = dark styles apply [OK]
dark class exists, dark: styles override [OK]- Ignoring the
darkclass onhtml - Mixing normal and dark styles incorrectly
- Assuming no style changes without JavaScript
dark class on body. Which of these JavaScript snippets correctly toggles dark mode?Solution
Step 1: Understand class toggling in JavaScript
The correct method to toggle a class isclassList.toggle('className').Step 2: Check each option
document.body.classList.toggle('dark') usesclassList.toggle('dark'), which is correct. document.body.toggleClass('dark') uses a non-existenttoggleClassmethod. document.body.className = 'dark' overwrites all classes, removing others. document.body.setAttribute('dark', true) sets an attribute, not a class.Final Answer:
document.body.classList.toggle('dark') -> Option DQuick Check:
Toggle class method = classList.toggle [OK]
- Using jQuery method toggleClass in plain JS
- Replacing className instead of toggling
- Setting attribute instead of class
dark class on the html element. Which React code snippet correctly implements this using Tailwind's class-based dark mode?Solution
Step 1: Identify correct element and method for toggling
Tailwind's class-based dark mode expects thedarkclass on thehtmlelement (document.documentElement). Toggling means adding or removing the class, soclassList.toggleis best.Step 2: Evaluate each option
const toggleDark = () => { document.documentElement.classList.toggle('dark'); }; return Toggle Dark; togglesdarkonhtmlcorrectly. const toggleDark = () => { document.body.classList.add('dark'); }; return Toggle Dark; only addsdarktobody(no toggle). const toggleDark = () => { document.documentElement.setAttribute('dark', 'true'); }; return Toggle Dark; sets an attribute instead of class. const toggleDark = () => { document.body.className = 'dark'; }; return Toggle Dark; overwrites all classes onbody, removing others.Final Answer:
const toggleDark = () => { document.documentElement.classList.toggle('dark'); }; return <button onClick={toggleDark}>Toggle Dark</button>; -> Option AQuick Check:
Toggledarkclass onhtmlwith classList.toggle [OK]
dark class on html using classList.toggle [OK]- Adding class without toggling
- Using wrong element like
body - Setting attribute instead of class
- Overwriting all classes instead of toggling
Ready to prove it?
Pass the test and this pattern moves unknown
