Respect prefers-color-scheme for the first paint, then let users override with a toggle that sets data-theme="light" or data-theme="dark" on document.documentElement. Persist the choice in localStorage so return visits stay consistent.
Avoid flash of wrong theme
Inline a tiny script in <head> (before CSS) that reads storage and sets data-theme immediately—same pattern as Bootstrap color modes and modern docs sites.
Pico does not ship a toggle
You own the button and storage; Pico supplies the variables that react to data-theme. Keep the control a real button with an accessible name (“Toggle dark mode”).
Self-check
- Which DOM node should carry
data-themefor a global switch? - Why read
localStoragebefore the first render?
Interview prep
- How does Pico handle dark mode?
Set
data-theme="light|dark"onhtmlor respectprefers-color-schemedepending on your setup.