Dark mode is a must-have for modern web apps. CSS custom properties (variables) make it simple to manage two color schemes.
Approach
- Define light mode colors as CSS variables on
:root. - Override them inside a
.darkclass or[data-theme="dark"]attribute. - Use JavaScript to toggle the class on
<body>. - Save the preference to
localStorage.
System Preference
Respect the user's OS preference with @media (prefers-color-scheme: dark).