How to detect if the User's OS prefers dark mode and change your site with CSS and JS

1 · Scott Hanselman · Sept. 14, 2021, 8:03 p.m.
I got a tweet from Stevö John who said he found my existing light theme for my blog to be jarring as he lives in Dark Mode. I had never really thought about it before, but once he said it, it was obvious. Not only should I support dark mode, but I should detect the user's preference and switch seamlessly. I should also support changing modes if the browser or OS changes as well. Stevö was kind enough to send some sample CSS and a few links so I started to explore the topic. There's a few things ...