Dark Mode & Theming in CSS

From the CSS cheat sheet ยท Custom Properties & Functions ยท verified Jul 2026

Dark Mode & Theming

Implement dark mode with CSS variables and media queries

css
:root {
  --bg: #ffffff;
  --text: #1f2937;
  --primary: #3b82f6;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111827;
    --text: #f9fafb;
    --primary: #60a5fa;
  }
}

body { background: var(--bg); color: var(--text); }
๐Ÿ’ก Override CSS variables in @media (prefers-color-scheme: dark) โ€” one change, everything updates
โšก color-scheme: light dark on :root makes browser UI (scrollbars, inputs) match your theme
๐Ÿ“Œ Support both system detection and manual toggle โ€” store the choice in localStorage
๐ŸŸข Slightly dim images in dark mode with filter: brightness(0.9) to reduce eye strain
dark-modethemingvariables

More CSS tasks

Back to the full CSS cheat sheet