Jul 17, 2024
index.html
, styles.css
, script.js
JavaScript Toggle Dark Mode
.<i id="toggle-icon" class="bi-brightness-high"></i>
margin: 0;
padding: 0;
font-family: Arial;
i
tag):
font-size: 15px;
cursor: pointer;
position: absolute; top: 15px; left: 15px; transform: translate(-15%, -15%);
h1
tag):
text-align: center;
font-size: 3em;
const toggle = document.getElementById('toggle-icon');
const body = document.querySelector('body');
bi-brightness-high
is the class, change to bi-moon
icon and switch to dark mode.toggle.addEventListener('click', function() {
if (toggle.classList.contains('bi-brightness-high')) {
toggle.classList.remove('bi-brightness-high');
toggle.classList.add('bi-moon');
body.style.backgroundColor = 'black';
body.style.color = 'white';
} else {
toggle.classList.remove('bi-moon');
toggle.classList.add('bi-brightness-high');
body.style.backgroundColor = 'white';
body.style.color = 'black';
}
body.style.transition = 'all 2s';
});