Sphere
Log in Join
☆ 7

Dark mode flashes white for a split second on reload

CSS variables switch based on a class set by JavaScript after mount. On each reload there is a white flash. CSP blocks inline scripts in head.

⚑ Report

3 replies

☆ 0
J Jules ·2026-10-27#1

Set color-scheme in an early stylesheet and put the theme class on the html element before first paint. With a strict CSP you can use a hashed external script.

⚑ Report
☆ 0
P Paul ·2026-10-27#2

Can this be done without any script?

⚑ Report
☆ 0
P Priya ·2026-10-28#3

Not fully for a user preference stored in localStorage. But prefers-color-scheme in CSS handles the OS setting with zero flash, then JS only overrides explicit user choice.

⚑ Report
Log in to reply.