Sub topic 8
Avoiding the flash of the wrong theme
This chapter assumes you have a working class-based dark mode, a theme script that reads localStorage, and basic knowledge of how browsers parse HTML. If the toggle does not work yet, finish that first.
What causes the flash
A page can show the wrong theme for a moment. A returning visitor who chose dark mode sees a white page, and then it turns dark. This is a flash of the wrong theme.
The cause is timing. The dark class lives on the <html> element, and your script is the code that adds it. If that script runs late, the browser has already painted the page with the light styles.
Late scripts usually come from three places:
- A script loaded with
deferortype="module", which runs after the document is parsed. - A bundled script placed at the end of
<body>. - A framework that sets the class only after it hydrates (attaches its JavaScript to server-rendered HTML).
The fix: an inline script in the head
Put a small script directly in <head>, before the stylesheet link. A regular inline script blocks parsing, so the browser runs it before it renders the body. The class is in place before the first paint.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script>
document.documentElement.classList.toggle(
"dark",
localStorage.theme === "dark" ||
(!("theme" in localStorage) &&
window.matchMedia("(prefers-color-scheme: dark)").matches),
);
</script>
<link rel="stylesheet" href="/styles.css" />
</head>
<body class="bg-white dark:bg-gray-900"></body>
</html>The script does one job. It adds dark when the saved choice is "dark". It also adds dark when nothing is saved and the system prefers dark. In every other case it removes the class.
Keep the script small
An inline script blocks rendering, so keep it short. It should do only the class check. Leave the toggle buttons, event listeners, and UI state to your normal bundled code.
The same logic then lives in two places: the inline script and the code that runs when the user changes the setting. Both must agree on the key name (theme) and the values ("light", "dark", and no key for system). If you rename one, rename the other.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Flash on every load | The script is deferred, a module, or at the end of `<body>` | Move a plain inline script into `<head>` |
| Flash only on the first visit | No saved value, and the system check is missing | Include the `matchMedia` check in the inline script |
| Page is correct but the toggle button shows the wrong state | The button reads its state after hydration | Set the button state from the `dark` class in your bundled code |
| Script is blocked | A Content Security Policy (CSP) forbids inline scripts | Allow the script with a nonce or a hash in your CSP |
| Framework warns about a mismatch | Server HTML has no `dark` class, but the client adds it | Mark the `<html>` element to suppress the hydration warning, as your framework documents |
Test it
Test with a slow page, since a fast machine can hide the flash.
- In DevTools, set CPU throttling to a slow setting.
- Run
localStorage.theme = "dark"in the console, then reload. - Watch the first frame. It should be dark with no white flicker.
- Run
localStorage.removeItem("theme"), switch your operating system to dark, and reload. The page should also be dark.
Exercise
Your theme script sits in a bundled file loaded with <script type="module" src="/app.js">. Dark-mode users see a white flash on each load. Why does this happen, and what is the fix?
Show answer
Module scripts run after the document is parsed, so the browser paints the light styles before the script adds the dark class. Move the class check into a plain inline <script> in <head>, before the stylesheet link, and keep the rest of the theme code in the module.
Exercise
A site uses a strict CSP that blocks inline scripts, and the theme flash returned after the policy was added. Name one way to keep the inline script working.
Show answer
Allow that one script in the policy, either with a nonce attribute that matches the CSP header or with a hash of the script's exact contents. Another option is to store the theme in a cookie and have the server render the dark class.