DEV Community

Muhammad Ayoub Khan
Muhammad Ayoub Khan

Posted on

Implementing Very Basic Dark/Light Modes with CSS Variables and JavaScript πŸŒ“

Introduction:

In this tutorial, we'll walk through the process of implementing a dynamic theme switching feature on a web page using CSS variables and JavaScript. We'll create both dark and light themes and allow users to toggle between them seamlessly.

Prerequisites:

Basic knowledge of HTML, CSS, and JavaScript.

Step 1: Setting Up the HTML Structure:

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dynamic Theme Switching</title>

    <link rel="stylesheet" href="style.css" />
  </head>

  <body>
    <header>
      <h1>Dynamic Theme Switching 🎨</h1>
    </header>

    <div class="container">
      <div class="card">
        <h2>Hello World! πŸ‘‹</h2>
        <p>Welcome to our website. Enjoy the dynamic theme switching feature!</p>
      </div>
    </div>

    <button id="theme-toggle">Toggle Theme πŸŒ—</button>

    <footer>
      <p>Β© 2024 Dynamic Theme Switching. All rights reserved.</p>
    </footer>

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

</html>
Enter fullscreen mode Exit fullscreen mode

Step 2: Creating the CSS Styles:

/* styles.css */

:root {
  --bg-color: #ffffff; /* default light mode background color */
  --text-color: #000000; /* default light mode text color */
}

.dark-mode {
  --bg-color: #333333;
  --text-color: #ffffff;
}

body {
  background-color: var(--bg-color);
  color: var(--text-color);
  transition:
    background-color 0.3s,
    color 0.3s;
  font-family: Arial, sans-serif;
  margin: 0;
}

header {
  background-color: var(--bg-color);
  color: var(--text-color);
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid rgb(175, 175, 175);
}

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 60vh;
}

.card {
  background-color: var(--light-bg-color);
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  padding: 20px;
  text-align: center;
}

button {
  background-color: #007bff;
  color: #ffffff;
  border: none;
  padding: 10px 20px;
  font-size: 16px;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s;
  margin-top: 20px;
}

button:hover {
  background-color: #0056b3;
}

button#theme-toggle {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background-color: #007bff;
  color: #ffffff;
  border: none;
  padding: 10px 20px;
  font-size: 16px;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s;
}

button#theme-toggle:hover {
  background-color: #0056b3;
}

footer {
  border-top: 1px solid rgb(175, 175, 175);
  background-color: var(--bg-color);
  color: var(--text-color);
  padding: 20px;
  text-align: center;
}

Enter fullscreen mode Exit fullscreen mode

Step 3: Implementing the JavaScript Logic:

const themeToggle = document.getElementById('theme-toggle');
const body = document.body;

themeToggle.addEventListener('click', () => {
  body.classList.toggle('dark-mode');
});
Enter fullscreen mode Exit fullscreen mode

Step 4: Testing and Final Touches:

Open the HTML file in a browser.
Click the "Toggle Theme" button to switch between dark and light modes.
Make any additional adjustments to the CSS for better theme consistency.

Conclusion:

Congratulations! You've successfully implemented a dynamic theme switching feature using CSS variables and JavaScript. Users can now enjoy a personalized browsing experience with dark and light modes.

Top comments (0)