I am trying to create a hamburger menu in mobile view.
When in mobile view the nav-item gets hidden and when i click the menu icon the nav-items will be displayed. But after clicking on the icon and changing the screen size the nav-item dissapper or their display:flex will be disabled.
I am new to Front End so I am getting stuck at this part.
const nameOfElement = document.querySelector(".nav-items")
var dd = "closed"
function dropdown() {
nameOfElement.style.display = "flex";
if (dd == "closed") {
nameOfElement.style.display = "block";
dd = "opened";
} else {
nameOfElement.style.display = "none";
dd = "closed";
}
}
.wrapper {
background: white;
margin: 0 0;
padding-top: 12px;
padding-bottom: 12px;
box-shadow: 0px 2px 11.3px 0px rgba(0, 0, 0, 0.02);
}
nav {
display: flex;
justify-content: space-around;
padding-left: 64px;
padding-right: 64px;
}
.logo {
display: flex;
justify-content: center;
align-items: center;
}
.nav-items {
display: flex;
justify-content: space-around;
gap: 32px;
}
ul {
list-style-type: none;
display: flex;
gap: 32px;
padding: 0;
}
li {
padding-left: 8px;
padding-right: 8px;
}
li a {
text-decoration: none;
font-family: 'Noto Sans', sans-serif;
font-weight: 500;
font-size: 14px;
color: gray;
}
li a:hover {
color: black;
}
.button {
display: flex;
justify-content: center;
align-items: center;
}
button {
width: 150px;
height: 42px;
padding: 10px 20px;
background: #A900E4;
color: white;
border: 0;
border-radius: 4px;
font-family: 'Noto Sans', sans-serif;
font-weight: 600;
font-size: 14px;
}
.hamburger {
display: none;
}
@media screen and (max-width: 768px) {
nav {
flex-direction: column;
align-content: center;
}
.nav-items {
flex-direction: column;
gap: 8px;
display: none;
}
.logo {
margin-top: 16px;
}
ul {
flex-direction: column;
justify-content: center;
margin-top: 32px;
margin-bottom: 32px;
}
.logo {
display: flex;
justify-content: space-between;
}
li {
text-align: center;
}
.links {
display: flex;
justify-content: center;
}
.button {
margin-bottom: 32px;
}
.hamburger {
display: block;
}
<div class="wrapper">
<nav>
<div class="logo">
<h3>Hello</h3>
<div class="hamburger">
<i onclick="dropdown()" class="fa-solid fa-bars"></i>
</div>
</div>
<div class="nav-items">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About Me</a></li>
<li><a href="#">My Works</a></li>
</ul>
<div class="button">
<button>Get in touch</button>
</div>
</div>
</nav>
</div>
<script src="https://kit.fontawesome.com/dd8784c5d5.js" crossorigin="anonymous"></script>
2
Answers
I just checked your code and it says that show hamburger menu only when screen size is
<=768px
and>768px
display:none
That is the reason for it disappearing
I dislike using element names to style so I added classes.
I also set up a
@media screen and (min-width: 768px) {
query so that when it is a large screen the hamburger is hidden and the elements always display. I converted toem
based on thefont-size:16px;
=1em
which is the default for most browsers.Now the big thing is I then used the data attribute with a value so I can use that in CSS rather than direct style from JavaScript which would have to change as the size change on the screen is made; this this is a bit simpler and only uses the CSS but still keeps the hide/show of the menu based on the hamburger click.
A small fix to put the terminating
}
on the media even though that was probably just a typo.