DEV Community

Cover image for The Easiest Way to Add Dark Mode to Your Website
Ali Karbasi
Ali Karbasi

Posted on • Edited on

The Easiest Way to Add Dark Mode to Your Website

The ability to use websites and applications in dark mode has grown in popularity in recent years. It relieves eye strain, prolongs battery life on OLED-screening devices, and offers an aesthetically pleasing substitute for the traditional light theme. This tutorial will show you how to use JavaScript to toggle themes and CSS variables to add dark mode to your website.

Step 1: Setting Up Your HTML

First, let's start with a basic HTML structure. Create an index.html file with the following content:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dark Mode Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <header>
        <h1>Welcome to Dark Mode Tutorial</h1>
        <button id="theme-toggle">Toggle Dark Mode</button>
    </header>
    <main>
        <p>This is a sample website to demonstrate dark mode implementation.</p>
    </main>
    <script src="script.js"></script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

This HTML file includes a header with a title and a button to toggle dark mode, a main content area with some text, and links to our CSS and JavaScript files.

Step 2: Defining CSS Variables

Now let's define the variables in our CSS. Make a styles.css file with the following content:

:root {
    --background-color: #ffffff;
    --text-color: #000000;
    --header-background-color: #f1f1f1;
}

body {
    background-color: var(--background-color);
    color: var(--text-color);
    font-family: Arial, sans-serif;
    transition: background-color 0.3s, color 0.3s;
}

header {
    background-color: var(--header-background-color);
    padding: 20px;
    text-align: center;
}

button {
    padding: 10px 20px;
    margin-top: 20px;
    cursor: pointer;
}

.dark-mode {
    --background-color: #181818;
    --text-color: #ffffff;
    --header-background-color: #242424;
}
Enter fullscreen mode Exit fullscreen mode

We use :root to define a group of CSS variables in this CSS file. For the light mode, these variables determine the background color, text color, and header background color. Additionally, a .dark-mode class is defined, and its settings take precedence over these variables. A seamless transition between themes is guaranteed by the body element's transition property.

Step 3: Adding JavaScript for Theme Toggling

Now, let's add the JavaScript to handle the theme toggling. Create a script.js file with the following content:

const themeToggle = document.getElementById('theme-toggle');
const body = document.body;

// Check for saved user preference
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
    body.classList.add(savedTheme);
}

// Toggle dark mode
themeToggle.addEventListener('click', () => {
    body.classList.toggle('dark-mode');

    // Save user preference
    if (body.classList.contains('dark-mode')) {
        localStorage.setItem('theme', 'dark-mode');
    } else {
        localStorage.removeItem('theme');
    }
});
Enter fullscreen mode Exit fullscreen mode

This script selects the theme toggle button and the body element. When the button is clicked, it toggles the dark-mode class on the body. The script also saves the user's theme preference in localStorage, so the theme persists across page reloads.

Step 4: Testing the Implementation

Launch your web browser and open the index.html file to test the implementation. To switch between bright and dark themes, click the "Toggle Dark Mode" button.
Remember that refreshing the page shouldn't affect the theme preference.

Conclusion

You can quickly add a dark mode toggle to your website by utilizing CSS variables and a little JavaScript. This methodology facilitates seamless theme transitions and offers an enhanced user experience. Please feel free to add more elements to your page and modify the styles in order to build upon this lesson.

Happy coding :D

Top comments (0)

The discussion has been locked. New comments can't be added.