Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Brtest Tutorial


Project: Light/Dark Mode Switch

In this project, you will create a simple toggle button that switches your web page between light mode and dark mode. You will use JavaScript to respond to button clicks, update CSS styles, and store the user’s choice.


Project Overview

You will build a web page that:

  • Switches between light and dark themes
  • Uses classList.toggle() for easy styling
  • Remembers the theme with localStorage

HTML Structure

Example

Basic HTML setup:

<div class='container'>
  <h2>Light/Dark Mode Switch</h2>
  <p>Click the button below to change the theme.</p>
  <button id='toggleBtn'>🌙 Dark Mode</button>
</div>
Try it Yourself »

CSS Styling

We define light mode as default, and dark mode using a CSS class.

Example

Light and dark styles:

body {
  font-family: Arial, sans-serif;
  background: #f4f4f4;
  color: #222;
  text-align: center;
  padding: 50px;
  transition: background 0.3s, color 0.3s;
}

.container {
  max-width: 400px;
  margin: auto;
  padding: 20px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 4px 10px rgba(0,0,0,0.1);
  transition: background 0.3s;
}

button {
  padding: 10px 20px;
  font-size: 16px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  background: #111;
  color: white;
  transition: background 0.3s;
}

.dark-mode {
  background: #111;
  color: #f4f4f4;
}

.dark-mode .container {
  background: #222;
  box-shadow: 0 4px 10px rgba(255,255,255,0.1);
}

.dark-mode button {
  background: #f4f4f4;
  color: #111;
}
Try it Yourself »

JavaScript Functionality

Now let’s add interactivity. We will toggle a CSS class on the body element when the button is clicked.

Example

Toggle dark mode with JavaScript:

<script>
const btn = document.getElementById('toggleBtn');
const body = document.body;

// Load saved theme
if (localStorage.getItem('theme') === 'dark') {
  body.classList.add('dark-mode');
  btn.textContent = '☀️ Light Mode';
}

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

  const dark = body.classList.contains('dark-mode');
  btn.textContent = dark ? '☀️ Light Mode' : '🌙 Dark Mode';
  localStorage.setItem('theme', dark ? 'dark' : 'light');
});
</script>
Try it Yourself »

How It Works

  1. The button click toggles the dark-mode class on the body.
  2. CSS automatically updates the background and text colors.
  3. The current theme is saved in localStorage so it stays after refreshing the page.

Bonus Features

  • Detect the user’s system preference using window.matchMedia('(prefers-color-scheme: dark)').
  • Add a smooth fade-in or icon animation when switching themes.
  • Sync the theme across multiple pages using localStorage.

Challenge

  • Add a keyboard shortcut (e.g. press “D”) to toggle dark mode.
  • Store the date and time the user last changed the theme.
  • Animate the background color transition using CSS.

Summary

  • You built a light/dark mode switch using JavaScript and CSS classes.
  • classList.toggle() makes theme switching simple.
  • localStorage remembers user settings across reloads.

Next, you'll move on to the JavaScript Browser API section — learning how to interact with browser features such as local storage, geolocation, and history.

Next » JavaScript Browser API

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.