EN ES
Home > Web development > CSS Tutorials > Guide to implementing Dark Mode on your website with CSS and JS

Guide to implementing Dark Mode on your website with CSS and JS

Diego Cortés
Diego Cortés
September 19, 2024
Guide to implementing Dark Mode on your website with CSS and JS

Dark mode has become a popular trend in web and application design. It not only offers a modern look but can also help reduce eye strain and save energy on OLED devices. In this guide, we will explore different methods to implement dark mode on your website, as well as best practices to ensure an optimal user experience.

What is Dark Mode?

Dark mode is a design option that uses a dark color scheme, where the background is generally black or dark gray and the text is in light colors. This mode is beneficial in various situations:

  • Reduction of eye strain: It reduces the contrast between the background and text, which can be less harmful to the eyes.
  • Energy saving: On OLED screens, dark pixels consume less energy than light pixels.
  • Modern aesthetics: Dark designs are typically seen as more sleek and contemporary.

How to Implement Dark Mode

1. Detect User Preference

A good way to start is by detecting whether the user prefers a dark theme. You can do this using the CSS media query prefers-color-scheme and JavaScript.

CSS

/* Default styles */
body {
    background-color: white;
    color: black;
}

/* Styles for dark mode */
@media (prefers-color-scheme: dark) {
    body {
        background-color: black;
        color: white;
    }
}

JavaScript

For users who do not have this preference configured, you could allow them to toggle between light and dark modes manually.

const toggleSwitch = document.querySelector('.theme-switch input[type="checkbox"]');

toggleSwitch.addEventListener('change', switchTheme, false);

function switchTheme(event) {
    if (event.target.checked) {
        document.documentElement.setAttribute('data-theme', 'dark');
    } else {
        document.documentElement.setAttribute('data-theme', 'light');
    }    
}

2. Use CSS Variables

CSS variables allow for easier and more effective management of colors on your website. You can define colors for both light and dark modes in a more organized way.

:root {
    --background-color: white;
    --text-color: black;
}

[data-theme='dark'] {
    --background-color: black;
    --text-color: white;
}

body {
    background-color: var(--background-color);
    color: var(--text-color);
}

3. Update Interactive Elements

Don't forget about the interactive elements of your website. Make sure that all buttons, links, and hover states are also visible in both modes. For example, you can make buttons change color when in dark mode.

button {
    background-color: var(--button-background-color);
    color: var(--button-text-color);
}

[data-theme='dark'] button {
    --button-background-color: gray;
    --button-text-color: white;
}

Best Practices

1. Accessibility

Ensure that your dark mode design is accessible to all users. Good contrast between text and background is crucial for ensuring that the information is legible. Use color contrast tools to verify readability.

2. Extensive Testing

Don't limit your testing to desktop screens. Make sure that dark mode works on mobile devices and across different browsers. Test with various screen settings to ensure the colors appear as expected.

3. Allow Users to Choose

Although you can detect a user's system preference, it is also important to allow them to switch between modes at will. This can be accomplished with a simple toggle button.

Conclusion

Implementing dark mode on your website not only enhances aesthetics but can also result in a more comfortable experience for your users. By following the steps and best practices outlined in this guide, you can ensure that your site is at the forefront of design and accessibility.

Always stay attuned to design trends and user preferences, as the world of web design is ever-evolving. Adding dark mode is a small step towards a better user experience on your website. Start today and enhance your platform with this functionality!

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Categories

Page loaded in 36.40 ms