DEV Community

DCT Technology Pvt. Ltd.
DCT Technology Pvt. Ltd.

Posted on

Dark Mode, Prefers-Color-Scheme & Theming in Modern UI

Dark Mode isn’t just a trend anymore — it’s a user expectation. Whether it's about reducing eye strain, saving battery, or just offering a slick experience, users love control over how they view your app or site.

But here’s the twist — implementing dark mode isn’t just about flipping colors.

It’s about embracing system preferences, smart CSS, and flexible theming that adjusts beautifully to context.

Let’s explore how to do it right using prefers-color-scheme, CSS variables, and a bit of JS magic.

Image description

🌗 What is prefers-color-scheme and Why Should You Care?

The prefers-color-scheme media query lets your site adapt to the user’s system theme — light or dark — automatically.

No toggle needed. No cookie saved.

It’s built into modern browsers and supported across macOS, Windows, Android, and iOS.

✅ Here’s how simple it is:

/* Light mode default */
body {
  background: #ffffff;
  color: #000000;
}

/* Auto-switch to dark mode */
@media (prefers-color-scheme: dark) {
  body {
    background: #121212;
    color: #ffffff;
  }
}
Enter fullscreen mode Exit fullscreen mode

🔥 That’s it. You've just made your site smart-theme aware.


🎨 Theming with CSS Variables

To go next level, use CSS variables to define and manage color schemes. This makes your design system scalable and theme-switching effortless.

:root {
  --bg-color: #ffffff;
  --text-color: #000000;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-color: #121212;
    --text-color: #ffffff;
  }
}

body {
  background: var(--bg-color);
  color: var(--text-color);
}
Enter fullscreen mode Exit fullscreen mode

🧠 Bonus: You can now switch themes dynamically using JavaScript too. Perfect for adding a toggle button if users want manual control.

Here’s a deep dive into dynamic theming with CSS variables and JS


🔁 Add a Theme Toggle (Let Users Choose!)

Want to offer a toggle switch? Here’s a JavaScript-powered theme switcher with localStorage so it remembers the choice.

<button onclick="toggleTheme()">Toggle Theme</button>

<script>
  const toggleTheme = () => {
    const current = document.documentElement.getAttribute('data-theme');
    const nextTheme = current === 'dark' ? 'light' : 'dark';
    document.documentElement.setAttribute('data-theme', nextTheme);
    localStorage.setItem('theme', nextTheme);
  };

  // Load theme on page load
  const saved = localStorage.getItem('theme');
  if (saved) {
    document.documentElement.setAttribute('data-theme', saved);
  }
</script>
Enter fullscreen mode Exit fullscreen mode

Pair this with CSS like:

:root[data-theme='light'] {
  --bg-color: #ffffff;
  --text-color: #000000;
}
:root[data-theme='dark'] {
  --bg-color: #121212;
  --text-color: #ffffff;
}
Enter fullscreen mode Exit fullscreen mode

Boom — full control, delightful UX.


🛠️ Tips for Better Theming

  • Use semantic tokens like --primary-color, --surface-color instead of hard-coded values.
  • Test across devices and OS — dark mode varies slightly on iOS vs Android.
  • Use tools like Dark Reader to test and audit your theme.
  • Don’t forget about images and SVGs — you may need to invert or switch them in dark mode.
  • Provide clear toggles — avoid ambiguous moon/sun icons without tooltips.

🔮 What’s Next? Advanced Theming & Custom Schemes

Why stop at just light and dark?

Create themes like:

  • Solarized Light/Dark
  • Neon Night
  • High Contrast for Accessibility

The trick is using layered CSS variables and toggling root attributes — theming is just a systemized abstraction.


💬 Your Turn

Are you using prefers-color-scheme yet?

Have you faced any weird quirks or surprises while implementing dark mode?

👇 Drop your tips, questions, or favorite tricks in the comments!


🚀 Follow [DCT Technology]for more actionable insights on **web development, design, SEO, and IT consulting.

Let’s make the web smarter, cleaner, and more delightful together!


#webdevelopment #css #darkmode #javascript #uiux #frontend #programming #accessibility #webdesign #dcttechnology #theming #developers #code #productivity

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →