HTML
<header>
<div class="logo">Мой сайт</div>
<button class="burger" id="openMenu" aria-label="Открыть меню">
<svg
xmlns="http://www.w3.org/2000/svg"
width="28"
height="28"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<line x1="3" y1="6" x2="21" y2="6"></line>
<line x1="3" y1="12" x2="21" y2="12"></line>
<line x1="3" y1="18" x2="21" y2="18"></line>
</svg>
</button>
<button class="close" id="closeMenu" aria-label="Закрыть меню">
<svg
xmlns="http://www.w3.org/2000/svg"
width="28"
height="28"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<line x1="18" y1="6" x2="6" y2="18"></line>
<line x1="6" y1="6" x2="18" y2="18"></line>
</svg>
</button>
<nav class="menu" id="menu">
<a href="#">Главная</a>
<a href="#">О нас</a>
<a href="#">Услуги</a>
<a href="#">Контакты</a>
</nav>
<div class="overlay" id="overlay"></div>
</header>
CSS
body {
margin: 0;
font-family: Arial, sans-serif;
}
header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 20px;
background: #333;
color: white;
}
.logo {
font-weight: bold;
}
/* Desktop Menu */
.menu {
display: flex;
gap: 20px;
z-index: 10;
}
.menu a {
color: white;
text-decoration: none;
font-size: 16px;
}
/* ===== BURGER ===== */
.burger {
display: none;
flex-direction: column;
cursor: pointer;
gap: 5px;
margin-left: auto;
}
.close {
display: none;
}
.burger span {
width: 25px;
height: 3px;
background: white;
border-radius: 2px;
}
/* Overlay */
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
visibility: hidden;
transition: opacity 0.3s ease;
z-index: 5;
}
.overlay.active {
opacity: 1;
visibility: visible;
}
/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
header {
justify-content: center;
}
.burger {
display: flex;
}
.close {
display: none;
}
/* ==== NAV ==== */
.menu {
position: fixed;
top: 0;
right: -250px;
height: 100%;
width: 250px;
background: white;
flex-direction: column;
padding: 60px 20px;
transition: right 0.3s ease;
}
.menu a {
margin: 15px 0;
font-size: 18px;
color: black;
}
.menu.active {
right: 0;
}
.menu.active ~ .close {
display: flex;
}
.menu.active ~ .burger {
display: none;
}
}
JS
const openBtn = document.getElementById("openMenu");
const closeBtn = document.getElementById("closeMenu");
const menu = document.getElementById("menu");
const overlay = document.getElementById("overlay");
// открыть меню
openBtn.addEventListener("click", () => {
menu.classList.add("active");
overlay.classList.add("active");
});
// закрыть меню (крестик)
closeBtn.addEventListener("click", () => {
menu.classList.remove("active");
overlay.classList.remove("active");
});
// закрыть меню (клик по overlay)
overlay.addEventListener("click", () => {
menu.classList.remove("active");
overlay.classList.remove("active");
});