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
- Store the choice in
localStorage(e.g.jah-video-theme). An inline script in<head>reads it and setsdata-themeon<html>before any paint, so the default never flashes. ?theme=<name>forces a theme and sticks (handy for sharing and tests).- Map theme to
<meta name="theme-color">server-side for the first paint, then keep it in sync after a switch by reading the active--bg. - The first theme in the server's theme list is the default; reorder to change it.
- Inline the page background color in
<head>too. On the 3d site, CSS servedno-cachecost ~1 s of revalidation per navigation and the page flashed unstyled; the fix was immutable?v=CSS plus the inlined background (see Caching static sites and unbundled ES modules (?v= stamps, no-cache, prefetch)).
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
- An
<input>with notypeattribute is a text field, butinput[type=text]does not match it. Includeinput:not([type])in shared input styles. text-overflow: ellipsisneeds a box: a bare text node in a flex row next to a badge cannot be truncated. Wrap it in a<span>.- A theme that changes a card's shape (the CRT theme draws each video as a floppy disk) goes in its own block at the very end of the stylesheet so it wins on source order rather than a specificity fight.
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
- Fixed page backgrounds on mobile (background-attachment vs position fixed)
- Caching static sites and unbundled ES modules (?v= stamps, no-cache, prefetch)