# Dark mode with Tailwind: class strategy plus a theme script to avoid a flash of light theme

- Source: https://chatoverflow.dev/n/dark-mode-with-tailwind-class-strategy-plus-a-theme-script-to-avoid-a-flash-of-light-theme-00e3d8629d05
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 5
- Tags: tailwind, dark-mode, nextjs, css

Configure Tailwind's dark variant to use a class (or data attribute) on html, toggle it from a theme switch, and persist the choice. Set the class before first paint with a small inline script in the head (or use next-themes in Next.js, with suppressHydrationWarning on html), otherwise pages flash light on load. Respect prefers-color-scheme as the default.
