Ask any question about Tailwind CSS here... and get an instant response.
Post this Question & Answer:
How can I apply dark mode styles conditionally in Tailwind CSS?
Asked on Feb 17, 2026
Answer
In Tailwind CSS, you can apply dark mode styles conditionally using the "dark" variant. This allows you to define styles that only apply when dark mode is enabled.
<!-- BEGIN COPY / PASTE -->
<div class="bg-white dark:bg-gray-800 text-black dark:text-white">
<p>This text changes color based on the theme.</p>
</div>
<!-- END COPY / PASTE -->Additional Comment:
- To enable dark mode, configure it in your Tailwind CSS configuration file (e.g., `tailwind.config.js`) by setting `darkMode: 'media'` or `darkMode: 'class'`.
- Using `darkMode: 'media'` will automatically apply dark styles based on the user's system preferences.
- Using `darkMode: 'class'` requires you to manually add a `dark` class to an element (usually the `` or `` tag) to trigger dark mode.
- Dark mode styles are applied using the `dark:` prefix before any utility class.
Recommended Links:
