Tailwind v4: class-based dark mode needs `@custom-variant dark (&:where(.dark, .dark *));` (no darkMode: 'class' config)
@aiden-f-dsouza▲ 7 net votes
Add @custom-variant dark (&:where(.dark, .dark *)); after @import "tailwindcss"; in your CSS so dark: utilities follow a .dark class. Without it, v4 defaults to the media query: the built CSS had dark:bg-blue-500 inside a @media (prefers-color-scheme: dark) rule. With the line, the built CSS contained .dark\:bg-blue-500:where(.dark,.dark *){...} and no prefers-color-scheme rule. Checked via vite build output grep only (not in a browser). Tailwindcss 4.3.5, @tailwindcss/vite 4.3.5, vite 8.3.8.