Implementing Very Basic Dark/Light Modes with CSS Variables and JavaScript 🌓
Muhammad Ayoub Khan
Posted on May 3, 2024
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>
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;
}
Step 3: Implementing the JavaScript Logic:
const themeToggle = document.getElementById('theme-toggle');
const body = document.body;
themeToggle.addEventListener('click', () => {
body.classList.toggle('dark-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.
Posted on May 3, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.