Supporting Dark Mode is no longer an optional luxury; it is a critical expectation for modern user experience, reducing eye strain in low-light environments and conserving OLED battery life. Modern CSS provides native media queries, form accent colors, and the next-generation light-dark() function to implement theme systems seamlessly.
Tell the browser engine that your website supports both light and dark modes to automatically adapt native browser scrollbars, form controls, and default canvas backgrounds:
:root {
color-scheme: light dark;
}
The prefers-color-scheme media query detects whether the user's operating system (Windows, macOS, Android, iOS) is set to Dark Mode:
/* 1. Light Mode Tokens (Default) */
:root {
--bg-primary: #ffffff;
--text-primary: #0f172a;
--card-bg: #f8fafc;
--card-border: #e2e8f0;
}
/* 2. Automatic Dark Mode Override */
@media (prefers-color-scheme: dark) {
:root {
--bg-primary: #0f172a;
--text-primary: #f8fafc;
--card-bg: #1e293b;
--card-border: #334155;
}
}
body {
background-color: var(--bg-primary);
color: var(--text-primary);
}
Instead of writing separate media queries for every component, the modern light-dark() function declares both light and dark colors in a single line:
.card {
/* Syntax: light-dark([light-value], [dark-value]); */
background-color: light-dark(#ffffff, #1e293b);
color: light-dark(#0f172a, #f8fafc);
border: 1px solid light-dark(#e2e8f0, #334155);
}
Customize native HTML radio buttons, checkboxes, range sliders, and progress bars to match your brand without custom CSS hacks:
:root {
accent-color: #d22b2b; /* Veewom Brand Crimson for all checkboxes and radios! */
}
Boost your CSS and front-end development proficiency by exploring these closely connected topics:
Understand margins, borders, padding, content and the universal box-sizing fix.
Master 1D alignment, justify-content, align-items, flex-wrap and responsive flow.
Build powerful two-dimensional grid layouts with template areas and fr units.
Eliminate styling conflicts and master selector weights from 0,0,0,1 to !important.