\n\n\n 🧠 Explanation: We include a button with id=\"toggle-btn\" for switching themes. The includes the external CSS. JavaScript is linked at the bottom with script.js. 🔹 Step 2: Style the Themes in CSS We define two themes using CSS: default (light) and .dark-mode. /* styles.css */\nbody {\n background-color: #ffffff;\n color: #000000;\n font-family: Arial, sans-serif;\n transition: all 0.3s ease;\n}\n\n.dark-mode {\n background-color: #121212;\n color: #ffffff;\n}\n\n.container {\n text-align: center;\n padding: 50px;\n}\n\n#toggle-btn {\n padding: 10px 20px;\n cursor: pointer;\n font-size: 16px;\n}\n 🧠 Explanation: Default theme is light (white background, black text). .dark-mode class applies dark background and white text. Smooth transition is added for visual effect. 🔹 Step 3: JavaScript to Toggle Theme and Use localStorage Now let’s write the main logic in JavaScript. // script.js\n\n// Get button and body\nconst toggleBtn = document.getElementById('toggle-btn');\nconst body = document.body;\n\n// Check localStorage for saved theme\nconst savedTheme = localStorage.getItem('theme');\n\nif (savedTheme === 'dark') {\n body.classList.add('dark-mode');\n}\n\n// Toggle theme on button click\ntoggleBtn.addEventListener('click', () => {\n body.classList.toggle('dark-mode');\n\n // Save the selected theme\n if (body.classList.contains('dark-mode')) {\n localStorage.setItem('theme', 'dark');\n } else {\n localStorage.setItem('theme', 'light');\n }\n});\n 🧠 Explanation: Get Elements: We access the body and the toggle button. Check localStorage: localStorage.getItem('theme') checks if the user has a saved theme. If it’s 'dark', we apply the dark mode by adding .dark-mode to . Event Listener: When the button is clicked, it toggles the class. Then it saves the theme choice in localStorage. 📌 What is localStorage and Why Use It? localStorage is a browser feature that lets you store data locally that doesn’t get deleted when the page is refreshed. It’s perfect for remembering theme choices. Example: If a user selects dark mode today, it stays even after refreshing or revisiting the site. 🎉 Final Output When you open the page: It loads the saved theme automatically. You can toggle between dark and light themes. The preference is saved and remembered. ✅ Summary Step What You Did HTML Created a structure with a toggle button CSS Defined light and dark themes JS Wrote logic to switch themes and save preference","@type":"NewsArticle","author":[{"@type":"Person","name":"ttimesnow","url":"https://www.ttimesnow.com/author/11"}],"description":"Learn how to implement a dark and light mode toggle using JavaScript and localStorage. Step-by-step guide with code examples and simple explanations.","inLanguage":"en","dateModified":"2025-05-23T01:50:03.273Z","mainEntityOfPage":{"@type":"WebPage","@id":"https://www.ttimesnow.com/easily-create-dark-and-light-mode-with-localstorage-in-javascript"},"url":"https://www.ttimesnow.com/amp/easily-create-dark-and-light-mode-with-localstorage-in-javascript","datePublished":"2025-05-23T01:50:03.273Z","publisher":{"@type":"Organization","name":"TtimesNow","logo":{"@type":"ImageObject","width":180,"url":"https://www.ttimesnow.com/apple-touch-icon.png","height":180},"@id":"https://www.ttimesnow.com#organization","url":"https://www.ttimesnow.com","sameAs":["https://www.facebook.com/donotbelazymotivation"]},"@id":"https://www.ttimesnow.com/easily-create-dark-and-light-mode-with-localstorage-in-javascript#article","headline":"Easily Create Dark and Light Mode with localStorage in JavaScript","articleSection":"javascript","thumbnailUrl":"https://www.ttimesnow.com/view/gk/free-courses-to-learn-javascript.webp"}],"@context":"https://schema.org"}
Google Advertisement

Easily Create Dark and Light Mode with localStorage in JavaScript

Google Advertisement
🔥 Read with Full Features on Our Website

Learn how to implement a dark and light mode toggle using JavaScript and localStorage. Step-by-step guide with code examples and simple explanations.

Published on 23 May 2025
By ttimesnow

🌓 How to Implement a Dark/Light Mode Toggle Using localStorage in JavaScript

Adding a dark/light mode toggle is a popular feature in modern websites. It improves user experience by letting visitors choose a theme that suits their eyes, especially in low-light environments. In this tutorial, you’ll learn how to add a dark/light mode toggle with a button, and save the user’s preference using localStorage so it stays even after refreshing the page.


✅ What You’ll Learn


🔹 Step 1: Create the HTML

This is the basic structure with a toggle button.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Dark/Light Mode Toggle</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="container">
    <h1>Welcome to My Website</h1>
    <button id="toggle-btn">Toggle Theme</button>
  </div>
  <script src="script.js"></script>
</body>
</html>

🧠 Explanation:


🔹 Step 2: Style the Themes in CSS

We define two themes using CSS: default (light) and .dark-mode.

/* styles.css */
body {
  background-color: #ffffff;
  color: #000000;
  font-family: Arial, sans-serif;
  transition: all 0.3s ease;
}

.dark-mode {
  background-color: #121212;
  color: #ffffff;
}

.container {
  text-align: center;
  padding: 50px;
}

#toggle-btn {
  padding: 10px 20px;
  cursor: pointer;
  font-size: 16px;
}

🧠 Explanation:


🔹 Step 3: JavaScript to Toggle Theme and Use localStorage

Now let’s write the main logic in JavaScript.

// script.js

// Get button and body
const toggleBtn = document.getElementById('toggle-btn');
const body = document.body;

// Check localStorage for saved theme
const savedTheme = localStorage.getItem('theme');

if (savedTheme === 'dark') {
  body.classList.add('dark-mode');
}

// Toggle theme on button click
toggleBtn.addEventListener('click', () => {
  body.classList.toggle('dark-mode');

  // Save the selected theme
  if (body.classList.contains('dark-mode')) {
    localStorage.setItem('theme', 'dark');
  } else {
    localStorage.setItem('theme', 'light');
  }
});

🧠 Explanation:

  1. Get Elements: We access the body and the toggle button.

  2. Check localStorage:

    • localStorage.getItem('theme') checks if the user has a saved theme.

    • If it’s 'dark', we apply the dark mode by adding .dark-mode to <body>.

  3. Google Advertisement

    Event Listener:

    • When the button is clicked, it toggles the class.

    • Then it saves the theme choice in localStorage.


📌 What is localStorage and Why Use It?

localStorage is a browser feature that lets you store data locally that doesn’t get deleted when the page is refreshed.


🎉 Final Output

When you open the page:


✅ Summary

Step What You Did
HTML Created a structure with a toggle button
CSS Defined light and dark themes
JS Wrote logic to switch themes and save preference
❤️ Like 💬 Comment 🔗 Share
Google Advertisement
👉 View Full Version on Main Website ↗
Google Advertisement
👉 Read Full Article on Website