CSS Dark Mode with color-scheme: light dark
Dark mode defaults without extra CSS
You used to restyle every form control, scrollbar, and background for dark mode. color-scheme tells the browser to use its built-in light or dark styling for those parts.
The old hack vs the modern fix
View demo@media (prefers-color-scheme: dark) {
input, select, textarea, button {
background: #333;
color: #eee;
}
::-webkit-scrollbar {
...
}
}:root {
color-scheme: light dark;
}<!-- Tailwind uses dark: variant, not color-scheme --><!-- color-scheme: light dark can still be set via arbitrary --><html class="[color-scheme:light_dark]"><!-- Then use dark: for your own colors --><div class="bg-white dark:bg-gray-900 text-black dark:text-white"></div>Color scheme browser support
Live demo
Why the modern way wins
One declaration
The browser applies its native light or dark styling to form controls, scrollbars, and default backgrounds.
No form control hacks
No need to override every input and select for dark. They follow the scheme automatically.
Pairs with light-dark()
Use color-scheme with light-dark() or variables so your colors and system UI match.
- Lines Saved
- 15+ → 3
- No manual dark form styles
- Old Approach
- Manual dark overrides
- Restyle every control and scrollbar
- Modern Approach
- color-scheme
- Browser handles system UI
How it works
The old approach was to add a @media (prefers-color-scheme: dark) block and manually set background and color on every input, select, textarea, and button, plus scrollbar styling. Lots of code and easy to miss a control.
color-scheme: light dark tells the browser that the page supports both schemes. The browser then uses its built-in light or dark styling for form controls, scrollbars, and the default canvas background, based on the user's preference. You still set your own colors for text and backgrounds; color-scheme handles the system-level parts so you do not have to.
Add <meta name="color-scheme" content="light dark"> to the <head> too. The CSS property only kicks in once the stylesheet loads, but the meta tag tells the browser right away, so dark-mode users don't get a white flash while the page loads.