TailwindCSS version:
{
"dependencies": {
"tailwindcss": "^4.1.3",
}
}
I'm using Tailwind CSS in a React project with the following configuration:
/** @type {import('tailwindcss').Config} */
module.exports = {
darkMode: 'class',
content: ['./src/**/*.{js,ts,jsx,tsx}'],
theme: {
extend: {},
},
plugins: [],
};
In my components, I use dark mode utility classes like this:
<div className="bg-white text-black dark:bg-black dark:text-white">
Hello
</div>
To toggle dark mode
const root = document.documentElement;
root.classList.toggle('dark', themeMode === 'dark');
localStorageUtil.setMode(themeMode);
Even when the dark class is not present on the element, the dark: styles (like dark:bg-black) are still applied as if dark mode is always enabled.
I think the light mode isn't working for you either. You don't notice this because the background is white and the text is black by default. So you assume that bg-white text-black is working correctly, but it's not.
Since January 2025, running npm install tailwindcss installs the new v4 version. There have been many breaking changes compared to v3. There are many outdated guides online, so use the official documentation for a proper installation.
For this, you just need to install a version-specific package; everything else remains the same:
npm install tailwindcss@3
In this case, your legacy JavaScript-based configuration is set up correctly and will work.
The
selectorstrategy replaced theclassstrategy in Tailwind CSS v3.4.1.
Here you need to clarify your React setup. You likely have two options:
@tailwindcss/postcss plugin.@tailwindcss/vite plugin directly.Both installation methods have been detailed previously on Stack Overflow as well.
If you are using the new stable v4, you need to get familiar with the new CSS-first configuration, since the tailwind.config.js file has been deprecated by default; although it's still possible to use it, but only theme.extend can be applied from it.
To configure dark mode, you must override the dark: variant using the @custom-variant directive:
@custom-variant directive - TailwindCSS v4 Docs./src/app.css
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With