This is a common issue when implementing dark mode in client-rendered or server-rendered apps: the page initially loads with the default (light) styles, then switches to dark mode after your JavaScript or Blazor lifecycle method runs, causing a flash of unstyled or wrong-styled content (FOUC).
Why does this happen?
- Your initial HTML and CSS load with the default (light) theme.
- The dark mode preference is stored in local storage or some user setting.
- On
OnAfterRenderAsync (or client-side JS), you read the preference and then apply the dark mode class.
- Until that happens, the page shows the light theme briefly.
How to fix this flash in Blazor Server + Tailwind CSS dark mode?
1. Use a CSS class on the <html> or <body> tag to control dark mode
Tailwind’s dark mode works best when you toggle a class like dark on the root element (<html> or <body>).
2. Apply the dark mode class as early as possible — ideally before Blazor renders
Since Blazor Server apps render on the server and then hydrate on the client, the initial HTML is sent without the dark mode class. You want to add the dark class before the page paints.
Recommended approach: Use a small inline script in your _Host.cshtml (or index.html for Blazor WASM)
Add a script that runs before the CSS loads and sets the dark class if the user prefers dark mode or if your saved preference says so.
Example:
<!DOCTYPE html>
<html lang="en" class="">
<head>
<meta charset="utf-8" />
<title>My Blazor App</title>
<script>
// Immediately apply dark mode class if user prefers or saved preference
(function() {
try {
const userPref = localStorage.getItem('theme');
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (userPref === 'dark' || (!userPref && prefersDark)) {
document.documentElement.classList.add('dark');
}
} catch(e) {
// ignore errors
}
})();
</script>
<link href="css/app.css" rel="stylesheet" />
</head>
<body>
<app>Loading...</app>
<script src="_framework/blazor.server.js"></script>
</body>
</html>
Explanation:
- This script runs before the CSS loads.
- It reads the saved theme from
localStorage (or falls back to system preference).
- It adds the
dark class to <html> immediately.
- Tailwind CSS will then apply dark styles from the start.
- When Blazor hydrates, it will be consistent with the initial theme.
3. Keep your Blazor code in OnAfterRenderAsync to update the theme if user changes it dynamically
Your Blazor code can still update the theme class dynamically, but the initial flash is prevented by the inline script.
Bonus: Tailwind config
Make sure your tailwind.config.js is set to use class strategy:
module.exports = {
darkMode: 'class', // important!
// other config...
}
Summary
- Add a small inline script in your
_Host.cshtml to set the dark class early.
- Use
darkMode: 'class' in Tailwind config.
- Store user preference in
localStorage and read it in the inline script.
- This prevents the white flash on page load.
If you want, I can help you write the exact code for your Blazor Server app! Just let me know.