Im facing a problem with a transition from light mode to dark mode. In JavaScript after the else
the variables cover all of my HTML text that is inside a flexbox and not the body (i think that this might be the problem but I dont know how to solve it.
JavaScript
const toggle = document.getElementById('toggleDark');
const body = document.querySelector('body');
toggle.addEventListener('click', function(){
this.classList.toggle('bi-moon');
if(this.classList.toggle('bi-brightness-high-fill')){
body.style.background = 'white';
body.style.color = 'black';
body.style.transition = '2s';
}else{
body.style.background = 'black';
body.style.color = 'white';
body.style.transition = '2s';
}
});
HTML
<!DOCTYPE html>
<html lang="it">
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="style.css" />
<meta charset="utf-8">
<title>home site</title>
<script src="script.js" defer></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css" />
</head>
<body>
</body>
<div class="top-container">
<span class="scrittaSu"><h1><span class="greg">GREG'S</span> PORTFOLIO</h1></span>
<span class="scrittaGiu"><p>Home.</p></span>
</div>
<header>
<span><li><a class="active" href="http://127.0.0.1:5500/index.html" target="_blank">HOME</a><li></span>
<li><a href="http://127.0.0.1:5500/portfolio.html" target="_blank" font="arial" >PORTFOLIO</a></li>
<li><a href="http://127.0.0.1:5500/aboutme.html" target="_blank" >ABOUT ME</a></li>
<i class="bi bi-brightness-high-fill" id="toggleDark"></i>
</header>
<div class="flex-container"> <!--beijvbihvbsdakjvbalksjvb-->
<div class="flex-left">
<span class="contentTop"><h2>random text</h2><span><br>
<span class="contentBottom"><p>random text
<br>
random text
I expected the whole text to transfer into darkmode, but just the background did. I think its because the text isnt included in body.style.color = 'white';
.
Really dont know wha to do. thank you
2
Answers
there is a better way to add dark/light modes to your website, for example: