htmlcssnavbarresponsivehamburger-menu

Responsive navbar does not show menus when I resize window or on mobile


I'm trying to build my first website, and I am stuck on this problem since a week. I want to create a responsive Navbar with hamburger menu when I resize a window or when I use the website on a mobile. However, when I click on the hamburger menu, I just see the animation of the hamburger menu changing (from 3 lines to 2 lines with resembling a triangle) but the nav menus that I usually see in the full screen size don't show. What can I try next?

Here you should be able to see the behaviour https://codepen.io/Robmelikn/pen/qBMLzEj (I hope I made it right, it's the first time I use it) Thanks in advance for your time and help!!

document.addEventListener('DOMContentLoaded', function() {
  // Get the hamburger menu element
  const hamburgerMenu = document.querySelector('.hamburger-menu');

  // Get the mobile menu element
  const mobileMenu = document.querySelector('.mobile-menu');

  // Toggle the active class on both the hamburger menu and mobile menu
  function toggleMenu() {
    hamburgerMenu.classList.toggle('active');
    mobileMenu.classList.toggle('active');
  }

  // Add a click event listener to the hamburger menu
  hamburgerMenu.addEventListener('click', toggleMenu);

  // Add a click event listener to each mobile menu link to close the menu when a link is clicked
  mobileMenu.querySelectorAll('a').forEach(function(menuItem) {
    menuItem.addEventListener('click', toggleMenu);
  });
});
/* Header */

header {
  width: 100%;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 2rem;
}

.nav-logo {
  max-width: 50px;
  max-height: 50px;
  margin-right: 8px;
  vertical-align: middle;
}


/* Navigation Menu */

.menu {
  list-style: none;
  display: flex;
  align-items: center;
}

.menu li a {
  text-decoration: none;
  color: #111131;
  padding: 0.5rem 1rem;
  display: inline-block;
}

.menu li a:hover {
  background-color: #ff3d0d;
  color: #fff;
  border-radius: 4px;
}


/* Main Content */

main {
  padding: 2rem;
}


/* Container */

.container {
  max-width: 1000px;
  /* Adjust this value to match the desired width */
  margin: 0 auto;
  /* Center the container */
  padding: 0 1rem;
  /* Add padding for smaller screens */
}


/* Hamburger Icon */

.hamburger-menu {
  display: none;
  flex-direction: column;
  justify-content: space-between;
  width: 25px;
  height: 15px;
  cursor: pointer;
}

.hamburger-menu span {
  width: 100%;
  height: 3px;
  background-color: #111131;
}


/* Mobile Navigation Menu */

.mobile-menu {
  display: none;
  list-style: none;
  position: absolute;
  top: 100%;
  left: 0;
  background-color: #fff;
  width: 100%;
  padding: 1rem;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  z-index: 999;
}

.mobile-menu li {
  margin-bottom: 1rem;
}

.mobile-menu li a {
  text-decoration: none;
  color: #111131;
  font-size: 1rem;
}

.mobile-menu li a:hover {
  color: #5c5cff;
}


/* Responsive styles */

@media screen and (max-width: 1024px) {
  .hamburger-menu {
    display: flex;
  }
  .menu {
    display: none;
  }
  /* Show mobile menu when it's active */
  .mobile-menu.active {
    display: block;
  }
}

.mobile-menu {
  background-color: #fff;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.hamburger-menu.active span:nth-child(1) {
  transform: translateY(3px) rotate(45deg);
}

.hamburger-menu.active span:nth-child(2) {
  opacity: 0;
}

.hamburger-menu.active span:nth-child
/* this CSS makes an article picture responsive, In my articles.html, wrap the image with a <div> element using the class .article-image: */

.article-image img {
  width: 100%;
  height: auto;
  max-width: 768px;
  /* Limit the maximum width of the image */
  display: block;
  /* Remove inline spacing */
  margin: 0 auto;
  /* Center the image when the container is wider */
}
<link href="https://fonts.googleapis.com/css2?family=Lora&display=swap" rel="stylesheet" />
<header>
  <nav class="container">
    <!-- Logo -->
    <div class="logo">
      <a href="index.html">
        <img src="images/logo.png" alt=" Myfirstwebsitelogo" class="nav-logo" /> Myfirstwebsite
      </a>
    </div>
    <!-- Navigation Menu -->
    <ul class="menu">
      <li><a href="about.html">About</a></li>
      <li><a href="articles.html">Articles</a></li>
      <li><a href="use-cases.html">Use Cases</a></li>
      <li><a href="resources.html">Resources</a></li>
      <li><a href="blog.html">Blog</a></li>
      <li><a href="contact.html">Contact</a></li>
    </ul>
    <!-- Hamburger Icon -->
    <div class="hamburger-menu">
      <span></span>
      <span></span>
      <span></span>
    </div>
    <!-- Mobile Navigation Menu -->
    <ul class="mobile-menu">
      <li><a href="about.html">About</a></li>
      <li><a href="articles.html">Articles</a></li>
      <li><a href="use-cases.html">Use Cases</a></li>
      <li><a href="resources.html">Resources</a></li>
      <li><a href="blog.html">Blog</a></li>
      <li><a href="contact.html">Contact</a></li>
    </ul>
  </nav>
</header>

<footer>
  <!-- Add your footer content here -->
</footer>

I tried already to get help here, I received a slight improvement, but I was suggested to open a new topic Hamburger icon click does not display menus


Solution

  • add top to this section of css.

    .mobile-menu.active {
        display: block;
        top: 4%;
      }