Implementing Dark Mode in Tailwind CSS: A Practical Guide
Learn how to implement dark mode in your Tailwind CSS project. Master configuration, the dark: modifier, and building flexible color schemes for modern UIs.

Previously in this course, we refined our typography and spacing to ensure our marketing site looks professional across all devices as covered in our guide to typography refinement. Now that our base design is solid, it's time to add a requested feature for modern web interfaces: a native dark mode.
Dark mode isn't just a trend; it's a critical accessibility feature that reduces eye strain and helps users conserve battery on mobile devices. Tailwind makes implementing this incredibly straightforward through the dark: modifier.
Enabling Dark Mode in Configuration
By default, Tailwind’s dark mode is disabled to keep the initial build size smaller. To enable it, you need to update your tailwind.config.js file.
Open your configuration file and add the darkMode property. In modern Tailwind (v3.0+), the recommended approach is to use the media strategy, which automatically switches the site theme based on the user's operating system preferences.
JAVASCRIPT// tailwind.config.js module.exports = { darkMode: CE9178">'media', theme: { extend: {}, }, plugins: [], }
Setting this to 'media' tells Tailwind to look for the user's browser-level or OS-level preference for dark mode. If they have "Dark Mode" enabled in their system settings, your site will automatically react.
Using the dark: Modifier
Once enabled, you can use the dark: prefix just like any other responsive modifier (like md: or lg:). Whenever you want an element to look different in dark mode, you prefix the utility class with dark:.
Think of this as a conditional override. If the system is in dark mode, the styles following the dark: prefix will take precedence over the base classes.
Consider this example where we set a light background for light mode and a dark background for dark mode:
HTMLstyle="color:#808080"><style="color:#4EC9B0">div class="bg-white dark:bg-slate-900 text-black dark:text-white p-8"> style="color:#808080"><style="color:#4EC9B0">h1 class="text-2xl font-bold">Welcome to my sitestyle="color:#808080"></style="color:#4EC9B0">h1> style="color:#808080"><style="color:#4EC9B0">p>This text adjusts its color based on your system theme.style="color:#808080"></style="color:#4EC9B0">p> style="color:#808080"></style="color:#4EC9B0">div>
In this snippet, bg-white and text-black are applied by default. When the user's system signals it's in dark mode, Tailwind swaps these for bg-slate-900 and text-white respectively.
Setting Up Dark Mode Color Schemes
When building a theme, you shouldn't just pick random colors. It’s best to create a semantic relationship between your light and dark variants. If your site has a primary "Brand" color, you might want it to be slightly less saturated in dark mode to prevent "glowing" or halos around text.
Here is how you would structure a button that works in both modes:
HTMLstyle="color:#808080"><style="color:#4EC9B0">button class="bg-blue-600 hover:bg-blue-700 text-white dark:bg-blue-500 dark:hover:bg-blue-400 px-4 py-2 rounded"> Click me style="color:#808080"></style="color:#4EC9B0">button>
By explicitly defining the dark variants, you keep full control over the contrast and visual hierarchy of your component.
Hands-on Exercise
- Open your
tailwind.config.jsand set thedarkModestrategy to'media'. - Locate the main container of your hero section from our previous project work.
- Apply
dark:bg-gray-900to your body or main wrapper anddark:text-gray-100to your text elements. - Refresh your browser and toggle your operating system's dark mode setting to see the changes take effect in real-time.
Common Pitfalls
- Forgetting to rebuild: If you are using the Tailwind CLI, remember that changes to
tailwind.config.jsusually require a restart of the watcher to take effect. - Contrast issues: A common mistake is using light gray text on a black background. Ensure your text remains readable by checking your color contrast ratios—the
dark:mode is not an excuse to sacrifice accessibility. - Hardcoded colors: Avoid using arbitrary hex codes inside your HTML when possible. Stick to the Tailwind color palette to ensure that your dark mode transitions feel cohesive and intentional.
FAQ
Can I force dark mode regardless of system settings?
Yes, you can change your darkMode setting to 'class' in your config. This requires you to add the dark class to your <html> or <body> element manually (often via JavaScript) to toggle the theme.
Does dark: work with shadows?
Absolutely. You can use dark:shadow-none or even dark:shadow-slate-700 to adjust how your cards appear against dark backgrounds.
Should I use media or class strategy?
Use media if you want to respect the user's OS preference by default. Use class if you want to provide a manual theme toggle button on your website, which is a common requirement for production-grade UIs.
Recap
Implementing dark mode is a matter of updating your configuration to enable the feature, then using the dark: modifier to selectively override your styles. By following the system preference approach, you provide a seamless experience that respects the user's environment.
Up next: We will dive into customizing the theme, where you'll learn how to extend Tailwind's default design tokens with your own unique brand colors and spacing scales.
Work with me

Next.js Website & Landing Page Development
A blazing-fast, SEO-optimized website or landing page in Next.js — the kind that loads instantly and ranks. Design-to-code, done right.

Next.js Full-Stack Web App Development
A fast, SEO-ready full-stack web app built with Next.js 16 — from idea to deployed product, by an engineer who ships to production.


