Back to blog

Dark Mode: Implementing Dark Theme on Your Site with CSS and JavaScript

Dark Mode has become a popular trend in user interface design. It's a design choice that changes the color scheme of an interface to a darker scheme. Many users prefer dark mode because it can be easier on the eyes, especially in low-light environments.

Advertisement

In this article, we'll show you how to implement Dark Mode on your website using CSS and JavaScript.

Benefits of Dark Mode

  1. Energy Saving: On devices with OLED screens, dark mode can save energy.
  2. Visual Fatigue Reduction: It can be more comfortable for reading in low light environment.
  3. Aesthetics: Provides a modern and elegant look.

Implementing Dark Mode with CSS

We can use CSS variables to define colors that can be easily switched between light and dark modes.

:root {  --background-color-light: #ffffff;  --background-color-dark: #000000;  --text-color-light: #000000;  --text-color-dark: #ffffff;}body.dark-mode {  background-color: var(--background-color-dark);  color: var(--text-color-dark);}body.light-mode {  background-color: var(--background-color-light);  color: var(--text-color-light);}

Adding JavaScript to Switch Modes

We can add a button that will allow users to toggle between light and dark modes.

<button id="toggle-dark-mode">Toggle Dark Mode</button>

Now, let's use JavaScript to switch the body classes.

document  .getElementById('toggle-dark-mode')  .addEventListener('click', function () {    document.body.classList.toggle('dark-mode');    document.body.classList.toggle('light-mode');  });

Accessibility Considerations

When implementing Dark Mode, it is essential to ensure that the contrast between the text and the background is sufficient to maintain readability. Tools like Color Contrast Checker can help.

Advertisement

Testing on Different Devices

Be sure to test the implementation on different devices and browsers to ensure a consistent user experience.

Additional Aspects of Dark Mode Implementation

Using System Preferences

You can take the user experience to the next level by automatically identifying and applying the user's system color preferences. With the prefers-color-scheme media query, you can define different styles for light and dark modes according to the user's system settings.

@media (prefers-color-scheme: dark) {  body {    background-color: var(--background-color-dark);    color: var(--text-color-dark);  }}

Persisting User Choice

For a smoother experience, consider storing the user's theme preference in a cookie or localStorage. This will allow the website to remember the user's choice on the next visit.

const userPrefersDarkMode = localStorage.getItem('dark-mode') === 'true';if (userPrefersDarkMode) {  document.body.classList.add('dark-mode');} else {  document.body.classList.add('light-mode');}document  .getElementById('toggle-dark-mode')  .addEventListener('click', function () {    const darkModeEnabled = document.body.classList.toggle('dark-mode');    localStorage.setItem('dark-mode', darkModeEnabled);  });

Considering Image and Component Design

When implementing Dark Mode, it is important to adjust not only text and background colors, but also consider the appearance of images, icons, and other UI components. Some images and components may need specific variations for light and dark modes to maintain visual harmony.

Conclusion

Dark Mode has become an essential part of modern web design, offering both aesthetic (AESTHETIC) and functional benefits. Its implementation not only increases accessibility and visual comfort, but also reflects care for users' preferences and needs.

With the techniques presented in this article, we hope you are inspired to try and incorporate Dark Mode into your web projects. The additional details provided here can help you create an even richer and more personalized experience.

Keep exploring and innovating, and never stop improving the user experience of your creations. After all, the web is a constantly evolving environment, and adapting to emerging trends and technologies is what keeps your work relevant and engaging.

Interested in other design and development techniques? Check out our post about Animating SVG with CSS: Bringing Vector Graphics to Life.

Advertisement

Until next time, HaWkers!

Let's go up! 🦅

Previous post Next post

Comments (0)

This article has no comments yet 😢. Be the first! 🚀🦅

Add comments