How to create a simple Dark Mode for websites

Web Development

In this example, the HTML code creates a toggle button that allows the user to switch between light and dark mode. The CSS code defines the styles for the light mode (default) and dark mode. The JavaScript code uses the classList property of the body element to toggle the dark-mode class, which applies the appropriate styles to the page.

This is just a basic example of how to implement a dark mode toggle. You can customize the styles and functionality to suit your needs and preferences. For example, you could use local storage or cookies to persist the dark mode preference between page loads, or you could use media queries to apply different styles for different screen sizes and devices.


Dark mode has become a popular feature on many websites and apps, as it allows users to switch to a darker color scheme that is easier on the eyes and can save battery life on devices with OLED screens. In this blog post, we will be discussing how to implement a dark mode toggle for your website using HTML, CSS, and JavaScript.

The HTML code for the dark mode toggle is relatively simple. We just need a button element that the user can click to switch between light and dark mode. Here is an example of the HTML code for the toggle button:

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

Next, we need to define the styles for light mode (the default) and dark mode using CSS. We can do this by creating two separate CSS classes and applying them to the body element using JavaScript. Here is an example of the CSS code for the light and dark mode styles:

body {
  background-color: white;
  color: black;
}
.dark-mode {
  background-color: black;
  color: white;
}

Finally, we need to use JavaScript to toggle the dark-mode class on the body element when the user clicks the toggle button. Here is an example of the JavaScript code that achieves this:

const toggleButton = document.getElementById('dark-mode-toggle');

toggleButton.addEventListener('click', () => {
  document.body.classList.toggle('dark-mode');
});

In this code, we use the getElementById function to select the toggle button element, and then we use the addEventListener function to attach a click event handler to the button. When the button is clicked, the event handler function toggles the dark-mode class on the body element using the toggle method of the classList property.

That’s it! With just a few lines of code, we have created a simple but effective dark mode toggle for our website. You can customize the styles and functionality to suit your needs and preferences. For example, you could use local storage or cookies to persist the dark mode preference between page loads, or you could use media queries to apply different styles for different screen sizes and devices.

Overall, implementing a dark mode toggle

<!-- HTML code for the dark mode toggle button -->
<button id="dark-mode-toggle">Toggle Dark Mode</button>

<!-- CSS code for the dark mode styles -->
<style>
  body {
    background-color: white;
    color: black;
  }
  .dark-mode {
    background-color: black;
    color: white;
  }
</style>

<!-- JavaScript code for the dark mode toggle -->
<script>
  const toggleButton = document.getElementById('dark-mode-toggle');
  toggleButton.addEventListener('click', () => {
    document.body.classList.toggle('dark-mode');
  });
</script>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.