shadcn/ui dark mode not working? Check these seven things
Is the dark class on <html>? If not: no provider, a flash on load, or Astro view transitions. If it is: colours without a dark value or @theme without inline.
- shadcn 4.23.0
- Tailwind CSS 4.3.5
- Vite 8
- React 19.3
- Astro 7.4.13
- next-themes 0.4.6
shadcn/ui components have no dark mode logic of their own. Every colour is a CSS variable with one value under :root and another under .dark, and the whole interface switches when the dark class is on <html>. So when dark mode "doesn't work", either the class isn't there at the moment you look, or some of your colours have no dark value. Open the inspector, check <html class="…">, and you'll know which half of this page you need.

When the dark class is missing
Nothing sets the class
shadcn needs something that writes the class, remembers the choice and follows the system setting. In our test with CLI 4.23, shadcn init -t vite generated src/components/theme-provider.tsx and wrapped the app in it in main.tsx. Its default is "system", it stores the choice under the key theme, and you can press d to flip it while testing. If you set up the project by hand or deleted that wrapper, nothing adds the class. Check that main.tsx still renders <ThemeProvider> around <App />.
On Next.js the shadcn docs use next-themes. The two settings people miss are attribute="class" (its default is a data-theme attribute, which the shadcn variant doesn't look at) and suppressHydrationWarning on <html>:
import { ThemeProvider } from "next-themes" export default function RootLayout({ children }: { children: React.ReactNode }) { return ( <html lang="en" suppressHydrationWarning> <body> <ThemeProvider attribute="class" defaultTheme="system" enableSystem disableTransitionOnChange> {children} </ThemeProvider> </body> </html> )}The page flashes light before it turns dark
The Vite provider applies the class in a React effect, which runs after the first paint. For a split second the page uses the light values. Set the class before React loads with an inline script in index.html, reading the same storage key the provider uses:
<head> <script> const t = localStorage.getItem("theme"); const dark = t === "dark" || ((!t || t === "system") && matchMedia("(prefers-color-scheme: dark)").matches); document.documentElement.classList.add(dark ? "dark" : "light"); </script></head>next-themes injects an equivalent script for you, which is why it needs suppressHydrationWarning: the server-rendered <html> has no class and the browser's does.
Astro: dark mode resets on every page
This one we reproduced exactly. With Astro's <ClientRouter /> (view transitions), the class is on <html> on the first page and gone after you click a link. The router swaps in the new page's <html> attributes, and the new page's server HTML has no class. Our test logged "dark" before the click and "" after it.
Apply the theme to the incoming document before the swap:
<script is:inline> function applyTheme(doc) { const stored = localStorage.getItem("theme"); const dark = stored ? stored === "dark" : matchMedia("(prefers-color-scheme: dark)").matches; doc.documentElement.classList.toggle("dark", dark); } applyTheme(document); document.addEventListener("astro:before-swap", (e) => applyTheme(e.newDocument));</script>With the last line added, the class survived navigation in the same test. Without view transitions you only need the first two statements.
When the class is there but colours are wrong
A colour has no .dark value
Most "half dark" pages come from colours added after init: a --brand or --success defined in :root and nowhere else. The component goes dark around it and that one colour stays light. Every variable you add needs a line in both blocks. The full pattern is in adding a colour to a shadcn theme.
Hard-coded Tailwind colours
bg-white, text-zinc-900 or border-gray-200 in your own components don't change in dark mode. Use the semantic utilities (bg-background, text-foreground, border-border, bg-muted) or add a dark: counterpart.
Dark mode inside one section doesn't work
If you put class="dark" on a single card or section and it stays light, check the colour mapping. It must be @theme inline. With plain @theme, the colours resolve once on :root and a nested .dark can't change them. We measured it: white with @theme, black with @theme inline, same markup.
A dark: class on <html> itself does nothing
shadcn's CSS defines the variant as @custom-variant dark (&:is(.dark *));. That matches elements inside .dark, not the element that has the class. Put dark: utilities on <body> or below. If you need it on the same element, change the variant to (&:where(.dark, .dark *)); Tailwind dark mode covers the options.
Questions
How do I add a dark mode toggle to shadcn/ui?
Use the hook from the provider: const { setTheme } = useTheme(), then call setTheme("dark"), "light" or "system" from a button or a dropdown. The Vite provider and next-themes both export useTheme with the same shape.
Why does next-themes show a hydration warning?
Because its script changes <html> before React hydrates. suppressHydrationWarning on <html> silences exactly that one element and nothing else.
Can I default to dark?
Pass defaultTheme="dark" to the provider, and change the fallback in your inline script to match, or the first paint will still follow the system.
Related articles
- Tailwind CSS v4Tailwind v4 dark mode: class toggle, system setting and fixesdark: follows prefers-color-scheme by default. One @custom-variant line switches it to a class. A no-flash toggle, and what to check when it fails.
- shadcn/uiAdd a custom colour to a shadcn/ui themeThree places in your CSS: the value in :root, the dark value in .dark, and one line in @theme inline. Plus a brand button variant and a contrast check.
- shadcn/uishadcn/ui not working with Tailwind v4: six causes and fixesUnstyled or transparent shadcn components on Tailwind v4 come from v3 leftovers: the old PostCSS plugin, @tailwind directives, hsl() wrappers, missing imports.
Still stuck? We answer every message ourselves. Write to us