For Astro Tailwind dark mode without a flash, make Tailwind’s dark: variant follow a .dark class on <html>, then set that class with a small is:inline script in the <head> before the page paints. The script reads the saved choice from localStorage and falls back to the system setting, so the first frame is already in the right theme.
Key Takeaways
- The flash happens when the theme is applied after the first paint, usually by a bundled or deferred script.
- An
is:inlinescript in<head>runs synchronously before the body renders.- Tailwind 4 needs one
@custom-variantline to switchdark:from media query to class.- With
<ClientRouter />, re-apply the class after each swap.
Switch the dark variant to a class
By default, Tailwind 4’s dark: follows prefers-color-scheme. That needs no script but gives users no toggle. To control it with a class:
/* src/styles/global.css */
@import 'tailwindcss';
@custom-variant dark (&:where(.dark, .dark *));
On Tailwind 3, set darkMode: 'class' in tailwind.config.mjs instead.
Set the class before first paint
Put this in the <head> of your base layout, above your stylesheets:
---
// src/layouts/Layout.astro
import '../styles/global.css';
---
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script is:inline>
(function () {
let stored = null;
try { stored = localStorage.getItem('theme'); } catch (e) {}
const dark = stored ? stored === 'dark' : matchMedia('(prefers-color-scheme: dark)').matches;
document.documentElement.classList.toggle('dark', dark);
document.documentElement.style.colorScheme = dark ? 'dark' : 'light';
})();
</script>
</head>
<body class="bg-white text-slate-900 dark:bg-slate-950 dark:text-slate-100">
<slot />
</body>
</html>
Two details matter. is:inline tells Astro not to bundle the script, so it stays in the head and runs immediately instead of loading as a deferred module. Setting color-scheme makes native form controls and scrollbars match too. The try block covers browsers where storage is blocked.
Add a toggle
---
// src/components/ThemeToggle.astro
---
<button id="theme-toggle" type="button" aria-label="Toggle dark mode" class="rounded p-2">
<span class="dark:hidden">Dark</span>
<span class="hidden dark:inline">Light</span>
</button>
<script>
document.getElementById('theme-toggle')?.addEventListener('click', () => {
const dark = document.documentElement.classList.toggle('dark');
document.documentElement.style.colorScheme = dark ? 'dark' : 'light';
try { localStorage.setItem('theme', dark ? 'dark' : 'light'); } catch (e) {}
});
</script>
The labels switch with dark: classes, so the button shows the right text on first paint without waiting for JavaScript. Our dark mode toggle guide adds a three-way light, dark and system switch.
With view transitions
If you use <ClientRouter />, Astro swaps the <html> attributes on navigation and the class can be lost. Re-apply it after every swap:
<script is:inline>
document.addEventListener('astro:after-swap', () => {
let stored = null;
try { stored = localStorage.getItem('theme'); } catch (e) {}
const dark = stored ? stored === 'dark' : matchMedia('(prefers-color-scheme: dark)').matches;
document.documentElement.classList.toggle('dark', dark);
});
</script>
The view transitions docs describe the astro:after-swap event. Most of our Astro themes handle this already, and the theme styling docs show where the dark tokens are defined.
FAQ
Why does my Astro site flash white before dark mode loads?
The theme class is being added after the first paint, usually by a script that Astro bundled and deferred. Move the check into an is:inline script in the <head> so it runs before the body renders.
Can I do Tailwind dark mode in Astro without JavaScript?
Yes, if you only want to follow the system setting. Tailwind’s default dark: variant uses prefers-color-scheme, which needs no script. A manual toggle needs a few lines of JavaScript to store the choice.
Where should I store the dark mode preference?
localStorage is the usual choice for a static site because the head script can read it synchronously. A cookie works too, but only helps if a server reads it and renders the class.