What is the CSS Dark Mode Generator?
Generate dark mode CSS using prefers-color-scheme, data-theme attributes, or CSS custom properties. Three implementation strategies in one tool.
Everything runs locally in your browser. Adjust the controls above, preview instantly, and copy clean, production-ready CSS — no account, no upload.
Using the CSS Dark Mode Generator
Everything lives in the panel at the top of this page. There is nothing to install — it all runs right here in your browser.
Choose strategy
OS preference only: follows the OS setting. Toggle + OS: lets users override the OS setting. Class toggle: legacy approach for maximum compatibility.
Set your colour pairs
Enter light and dark values for background, surface, text, secondary text, border, and primary colour.
Copy the CSS and optional JS
Strategy 2 requires a small JavaScript snippet to toggle the data-theme attribute. It is included in the output.
What the generated code looks like
Clean, production-ready CSS — no vendor bloat, no unnecessary declarations.
/* Strategy 1: OS preference only (simplest) */
@media (prefers-color-scheme: dark) {
:root {
--bg: #000;
--text: #f5f5f7;
}
}
/* Strategy 2: Toggle + OS fallback (recommended) */
:root { --bg: #fff; --text: #1d1d1f; }
[data-theme="dark"] { --bg: #000; --text: #f5f5f7; }
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--bg: #000; --text: #f5f5f7;
}
}
/* Strategy 3: Class toggle (legacy) */
.dark-mode body { background: #000; color: #f5f5f7; }Pro tips for better results
Always test dark mode for contrast ratios
Dark backgrounds with coloured text often fail WCAG AA contrast (4.5:1). Light text on dark backgrounds can also be hard to read at full saturation. Use a contrast checker after generating your dark palette.
Frequently asked questions
From the blog
Read more →CSS clamp() and Fluid Typography: Stop Writing Font-Size Media Queries (2026)
Learn how CSS clamp() creates type that scales smoothly between screen sizes with a single line, the formula behind it, and how to build a fully fluid type scale without media queries.
CSS Grid vs Flexbox: Which Should You Use? (2026)
CSS Grid vs Flexbox, settled. Here's a senior dev's honest breakdown of when to use each, with real code examples, a comparison table, and no hedging.
CSS Animations: A Practical Guide to Keyframes and Transitions (2026)
Learn the difference between transitions and keyframe animations, how to write @keyframes, the properties worth animating for 60fps, and easing that actually looks good.