skip to Main Content

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


  1. I just checked your code and it says that show hamburger menu only when screen size is <=768px and >768px display:none

    @media screen and (max-width: 768px) {
    .hamburger {
        display: block;
      }
    }
    

    That is the reason for it disappearing

    Login or Signup to reply.
  2. 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 to em based on the font-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.

    //const nameOfElement = document.querySelector(".nav-items");
    //var dd = "closed";
    var hamburgerIcon = document.querySelector(".hamburger").querySelector(".hamburger-icon");
    
    hamburgerIcon.addEventListener("click", (event) => {
      const nav = document.querySelector(".nav-items");
      var navStyle = nav.dataset.menustyle;
      nav.dataset.menustyle = navStyle == "closed" ? "opened" : "closed";
    });
    * {
      font-size: 16px;
      box-sizing: border-box;
      margin: 0;
      padding: 0;
      border: 0;
    }
    
    .wrapper {
      background: white;
      padding-top: 0.75em;
      padding-bottom: 0.75em;
      box-shadow: 0em 0.125em 0.75em 0em rgba(0, 0, 0, 0.02);
    }
    
    nav {
      display: flex;
      justify-content: space-around;
      padding-left: 4em;
      padding-right: 4em;
    }
    
    .logo {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    .nav-items {
      display: flex;
      justify-content: space-around;
      gap: 2em;
    }
    
    .nav-items[data-menustyle="closed"] {
      display: none;
    }
    
    .nav-items[data-menustyle="open"] {}
    
    .nav-block {
      list-style-type: none;
      display: flex;
      gap: 2em;
    }
    
    .nav-item {
      padding-left: 0.5em;
      padding-right: 0.5em;
    }
    
    .nav-link {
      text-decoration: none;
      font-family: 'Noto Sans', sans-serif;
      font-weight: 500;
      font-size: 0.875em;
      color: gray;
      border: 1px solid #00FF00;
    }
    
    .nav-link:hover {
      color: black;
    }
    
    .button {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    .in-touch {
      width: 9.375em height: 2.625em;
      padding: 0.625em 1.25em;
      background-color: #A900E4;
      color: white;
      border-radius: 0.25em;
      font-family: 'Noto Sans', sans-serif;
      font-weight: 600;
      font-size: 0.875em;
    }
    
    
    @media screen and (max-width: 768px) {
      nav {
        flex-direction: column;
        align-content: center;
      }
      .nav-items {
        flex-direction: column;
        gap: 8px;
      }
      .logo {
        margin-top: 1em;
      }
      .nav-block {
        flex-direction: column;
        justify-content: center;
        /*   margin-top: 2em;
        margin-bottom: 2em;*/
      }
      .logo {
        display: flex;
        justify-content: space-between;
      }
      .nav-item {
        text-align: center;
      }
      .in-touch-container {
        margin-bottom: 2em;
      }
      .nav-items[data-menustyle="closed"] {
        display: none;
      }
      .nav-items[data-menustyle="open"] {}
    }
    
    @media screen and (min-width: 768px) {
      .hamburger {
        display: none;
      }
      .nav-items[data-menustyle="closed"] {
        display: flex;
      }
    }
    <div class="wrapper">
      <nav>
        <div class="logo">
          <h3>Hello</h3>
          <div class="hamburger">
            <i class="fa-solid fa-bars hamburger-icon"></i>
          </div>
        </div>
        <div class="nav-items" data-menustyle="closed">
          <ul class="nav-block">
            <li class="nav-item"><a class="nav-link" href="#">Home</a></li>
            <li class="nav-item"><a class="nav-link" href="#">About Me</a></li>
            <li class="nav-item"><a class="nav-link" href="#">My Works</a></li>
          </ul>
          <div class="nav-item in-touch-container">
            <button class="in-touch">Get in touch</button>
          </div>
        </div>
      </nav>
    </div>
    <script src="https://kit.fontawesome.com/dd8784c5d5.js" crossorigin="anonymous"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search