Level Up Your Website: Implementing a Dark Mode Toggle with CSS and JavaScript


In recent years, dark mode has become increasingly popular in web design, offering users an alternative color scheme that's easier on the eyes and reduces eye strain, especially in low-light environments. If you want to provide your website visitors with the option to switch between light and dark modes, you're in luck! In this blog post, we'll explore how to implement a dark mode toggle on a website using CSS and JavaScript. By the end of this tutorial, you'll have a working dark mode feature that users can easily switch on or off.


HTML Structure


Let's start with a simple HTML structure, consisting of a header, primary content, and a footer. We'll add a toggle button in the header to enable users to switch between light and dark modes. Here's an example:


(index.html)


<!DOCTYPE html>

<html>

  <head>

    <link rel="stylesheet" type="text/css" href="styles.css" />

  </head>

  <body>

    <header>

      <h1>Website Title</h1>

      <button id="darkModeToggle">Toggle Dark Mode</button>

    </header>


    <main>

      <!-- Main content goes here -->

    </main>


    <footer>

      <!-- Footer content goes here -->

    </footer>


    <script src="script.js"></script>

  </body>

</html>


CSS Styling


Next, we'll define the CSS styles for both light and dark modes in a separate CSS file (styles.css):


/* Light mode styles */

body {

  background-color: #ffffff;

  color: #000000;

}


/* Dark mode styles */

body.dark-mode {

  background-color: #1f1f1f;

  color: #ffffff;

}


JavaScript Functionality


To implement the dark mode toggle functionality, we'll use JavaScript. Create a JavaScript file (script.js) and link it to the HTML file.


// Get the dark mode toggle button element

const darkModeToggle = document.getElementById("darkModeToggle");


// Listen for a click event on the toggle button

darkModeToggle.addEventListener("click", function () {

  // Toggle the dark mode class on the body element

  document.body.classList.toggle("dark-mode");


  // Store the user's preference in localStorage

  if (document.body.classList.contains("dark-mode")) {

    localStorage.setItem("darkMode", "enabled");

  } else {

    localStorage.setItem("darkMode", "disabled");

  }

});


// Check if dark mode preference is saved in localStorage

if (localStorage.getItem("darkMode") === "enabled") {

  document.body.classList.add("dark-mode");

} else {

  document.body.classList.remove("dark-mode");

}


Explanation


Within the JavaScript code, we first get the dark mode toggle button element using its ID. We then listen for a click event on the button and toggle the 'dark-mode' class on the body element accordingly.


When the user clicks the toggle button, the 'dark-mode' class is added or removed from the body element, which triggers the CSS styles defined for dark mode.


Additionally, we use the localStorage object to store the user's preference for dark mode. If the 'dark-mode' class is present, we store 'enabled' in localStorage; otherwise, we store 'disabled'. This way, the user's preference is remembered even if they refresh the page.


Conclusion


Implementing a dark mode toggle on your website can enhance the user experience and provide a visually appealing alternative. By following the steps outlined in this tutorial, you can easily add a dark mode feature using CSS and JavaScript. Feel free to customize the styles to match your website's design and provide a seamless experience for your users. Embrace the dark side of web design and make your website even more user-friendly!