Automatic and Manual Dark Mode Toggle
Build a theme toggle that respects system preference by default and lets users override it manually.
The Goal
An ideal dark mode implementation: auto-detects the OS preference by default, and provides a toggle button that lets users override it independently, with the preference persisted across page loads.
Architecture Overview
Three states to handle:
- System preference: use
prefers-color-schemewith CSS variables - User override: dark: set a class or data attribute on
<html> - User override: light: set a different class to prevent auto dark
All lessons in this course
- prefers-color-scheme Media Query
- CSS Variables for Theming
- color-scheme Property
- Automatic and Manual Dark Mode Toggle