The Wayback Machine - https://web.archive.org/web/20241224092122/https://www.geeksforgeeks.org/how-to-make-dark-mode-for-websites-using-html-css-javascript/
Open In App

How to Make Dark Mode for Websites using HTML CSS & JavaScript ?

Last Updated : 01 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Dark mode (or night mode) is a popular design feature where the background is dark, and the text and icons are light. It reduces eye strain in low-light conditions and can help save battery life on some devices.

In this article, we’ll explain how to easily add dark mode to your website using HTML, CSS, and JavaScript.

Steps to Add Dark Mode to Your Website

Follow these simple steps to implement dark mode on your website:

Step 1: Create an HTML document.

Step 2: Add CSS properties to the body and add dark-mode class properties in CSS.

Step 3: Add buttons to switch between Dark and Light Mode.

Step 4: Add functionality to buttons to switch between dark mode and light mode using JavaScript.

Different Ways to Implement Dark Mode on a Website

Below are some examples showcasing how to implement dark mode on website.

Example 1: Switching Between Dark and Light Mode

This example demonstrates how to switch between dark and light modes on a webpage using two buttons. The dark mode changes the background to black with white text, while the light mode restores the original white background with black text.

HTML
<!-- Filename:index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" 
          content="IE=edge" />
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0" />
    <title>
          How to Make Dark Mode for Websites using HTML CSS & JavaScript  ?
      </title>
    <style>
        body {
            padding: 25px;
            background-color: white;
            color: black;
            font-size: 25px;
        }

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

        .light-mode {
            background-color: white;
            color: black;
        }
    </style>
</head>

<body>
    <h1>GeeksForGeeks</h1>

    <p>
        This is a sample GeeksForGeeks Text.
        Dark Mode Websites using Html CSS &
        Javascript
    </p>

    <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20200122115631/GeeksforGeeks210.png" />

    <h3 id="DarkModetext">Dark Mode is OFF</h3>
    <button onclick="darkMode()">Darkmode</button>
    <button onclick="lightMode()">LightMode</button>
    <script>
        function darkMode() {
            let element = document.body;
            let content = document.getElementById("DarkModetext");
            element.className = "dark-mode";
            content.innerText = "Dark Mode is ON";
        }
        function lightMode() {
            let element = document.body;
            let content = document.getElementById("DarkModetext");
            element.className = "light-mode";
            content.innerText = "Dark Mode is OFF";
        }
    </script>
</body>
</html>

Output:

Example 2: Toggle Dark Mode using HTML, CSS, and JavaScript

This example demonstrates how to toggle between dark and light modes using a single button. The dark mode switches the background to black with white text, and the same button is used to toggle back to light mode.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0" />
    <title>
          How to Make Dark Mode for Websites using HTML CSS & JavaScript  ?
      </title>
    <style>
        body {
            padding: 25px;
            background-color: white;
            color: black;
            font-size: 25px;
        }

        .dark-mode {
            background-color: black;
            color: white;
        }
    </style>
</head>

<body>
    <h1>GeeksForGeeks</h1>
    <h2>Dark Mode tutorial using HTML, CSS and JS</h2>

    <p>GeeksForGeeks Sample Text</p>

    <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20200122115631/GeeksforGeeks210.png" />
    <div>
        <button onclick="darkMode()">Darkmode</button>
    </div>
    <script>
        function darkMode() {
            var element = document.body;
            element.classList.toggle("dark-mode");
        }
    </script>
</body>
</html>

Output:

These are some ways to add dark mode to a website. Example 1 uses separate buttons for switching between modes, while Example 2 employs a single toggle button. Both methods offer simple yet effective approaches to implementing dark mode, improving accessibility and user experience across different lighting conditions.




Next Article

Similar Reads

three90RightbarBannerImg