Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to switch between dark and light mode with CSS and JavaScript?\\n
To switch between dark and light mode with JavaScript, the code is as follows −
Example
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
padding: 25px;
background-color: white;
color: black;
font-size: 25px;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.dark-mode {
background-color: black;
color: white;
}
.toggleButton {
padding: 12px;
font-size: 18px;
border: 2px solid green;
}
</style>
</head>
<body>
<h1>Toggle Dark/Light Mode Example</h1>
<button class="toggleButton">Toggle dark mode</button>
<h2>Click the above button to toggle dark mode</h2>
<script>
document .querySelector(".toggleButton") .addEventListener("click", toggleDarKMode);
function toggleDarKMode() {
var element = document.body;
element.classList.toggle("dark-mode");
}
</script>
</body>
</html>
Output
The above code will produce the following output −

On clicking the “Toggle dark mode” button −

Advertisements