Menu

1 / 5

Dark mode in Next.js 16 with no flash: Tailwind CSS 4, one inline script, no library

DEV Community: tailwindcss·Nando Septian Husni·3 days ago
#wDax6n9C
#devtotailwindcss#devto#dev#ededed#dark
Long article (1814 words) — no AI summary yet.
Reading 0:00
15s threshold

A user picks dark mode, reloads, and sees a white page for a moment before it turns dark. The server can't read localStorage , so it renders the default theme, and any code that waits for React runs after the browser has already painted that default.

This post builds a light / dark / system toggle with no theme library, then checks the first paint with Playwright. I tested everything on Next.js 16.4.6, React 19.3.0 and Tailwind CSS 4.3.3 , on a production build ( next build && next start ) unless I say otherwise.

1. Point Tailwind's dark: variant at a class

By default, Tailwind's dark: variant follows the prefers-color-scheme media query. A manual toggle needs it to follow a class instead.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.