html файл:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Dark Mode</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" href="styles.css" />
<script
src="https://kit.fontawesome.com/81d73e8e4d.js"
crossorigin="anonymous"
></script>
</head>
<body>
<nav class="navbar">
<h1>LOGO</h1>
<ul class="nav-links">
<li class="links">Домой</li>
<li class="links">О нас</li>
<li class="links">Контакты</li>
</ul>
<div id="dark-mode-icon" title="Toggle Dark/Light Mode">
<i class="fa-solid fa-circle-half-stroke"></i>
</div>
</nav>
<main>
<section>
<h2>Домой</h2>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
</p>
</section>
<section>
<h2>О нас</h2>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
</p>
</section>
<section>
<h2>Контакты</h2>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
</p>
</section>
</main>
<script src="script.js"></script>
</body>
</html>
CSS:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
scroll-behavior: smooth;
}
.navbar {
box-shadow: 0 5px 10px rgba(211, 211, 211, 0.8);
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 25px;
position: sticky;
top: 0;
background-color: white;
}
.nav-links {
display: flex;
gap: 20px;
}
.links {
list-style: none;
font-size: 1.1rem;
cursor: pointer;
}
#dark-mode-icon {
font-size: larger;
cursor: pointer;
padding: 15px;
}
section {
width: 10%;
margin: 40px auto;
}
section p {
padding: 15px 0;
color: grey;
}
.dark-mode {
background-color: black;
color: white;
}
.dark-nav {
background-color: black;
box-shadow: none;
border-bottom: 1px rgba(211, 211, 211, 0.205) solid;
}
JavaScript:
let navBar = document.querySelector('.navbar');
let darkMode = document.querySelector('#dark-mode-icon');
let body = document.body;
darkMode.addEventListener('click', () => {
navBar.classList.toggle('dark-nav');
body.classList.toggle('dark-mode');
});