How can I make the two-lined hamburger icon change to an X shape when the .btn-menu is clicked? Currently, the lines are not centered properly. Can anyone help me identify what I am missing in my code?
Below is the HTML, CSS, and JavaScript code that I have been working on:
const menuBtn = document.querySelector(".btn-menu");
// Create menu animation
menuBtn.addEventListener("click", function () {
menuBtn.classList.toggle("transform");
});
.btn-menu {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
gap: 0.8rem;
transition: all 0.6s;
}
.toggle-line {
width: 5rem;
height: 1rem;
background-color: black;
border-radius: 3px;
}
.is-top,
.is-bottom {
transition: all 0.5s;
}
.transform .is-top {
transform: translateY(8px) rotate(45deg);
}
.transform .is-bottom {
transform: translateY(-8px) rotate(-45deg);
}
<div class="btn-menu" role="button">
<div class="toggle-line is-top"></div>
<div class="toggle-line is-bottom"></div>
</div>