📚 Encyclopedia

Web Development · updated 2026-10-13

CSS theme switching without a flash (and contrast over photos)

cssthemescolor-schemelocalstorageaccessibilitycontrast

Patterns for multi-theme sites built on one set of CSS custom properties, from videos.jhampton.io (CRT, Daylight, Midnight, Terminal), the 3d site and the Joyride game.

Apply the stored theme before first paint

Pin color-scheme per theme

Leaving color-scheme to the OS broke a light theme on a dark-mode desktop: form controls rendered white text on white and checkboxes as black blocks. Pin it per theme (light for light themes, dark for dark ones). The <head> can still say content="light dark" so the first paint is not a white flash.

Selector traps

Text over a photo: measure contrast across the image

A photographic background is not one color. The CRT phosphor photo averaged #54b54b (8.2:1 against black, 2.6:1 against white, so black text), but its dark corner needed more than the average did. Measure contrast at the brightest, average and darkest regions on the rendered page. A 22% pale wash over the photo lifted the darkest-corner floor to 5.1:1 for body text and 4.5:1 for muted text; a 16% wash measured 4.2:1 and failed WCAG AA. Overlay geometry measured off a photo (the floppy label inset: 10% left, 11.2% right, 42% top, 6.1% bottom of a 250x262 image) must be re-measured if the photo changes.

Games: canvas palettes as theme objects

For a canvas game, keep the palette in a THEMES.day / THEMES.night object read every frame, and store a tone index on world objects (not a color) so a theme switch recolors live. Chrome around the canvas uses CSS variables with a body[data-theme] override block. "Dark mode" suits a developer tool, not a game; Justin asked for a brighter day theme as the default. On a light palette, outline solid-colored sprites dark (a neon outline on grey reads as nothing) and stroke floating text.

See also

Linked from

Sources: justinahampton memory (videos-crt-theme, arcade-joyride v7), 3d-subdomain