1

I'm trying to build my first website, and I am stuck on this problem. 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, nothing happens, the menus don't show. What can I try next?

Index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="scripts.js" defer></script>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="styles.css" />
    <link
      href="https://fonts.googleapis.com/css2?family=Lora&display=swap"
      rel="stylesheet"
    />
    <title>Myfirstwebsite</title>
    <link href="styles.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <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>

    <script src="scripts.js"></script>
  </body>
</html>

styles.css

/* 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 */
}

scripts.js

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);
  });
});

I tried the above code and I was expecting to see a responsive navbar visualizing the menus that I correctly see when I test the website in full screen. However, when I resize the screen, the hamburger icon correctly appears but when I click nothing happens.

halfer
  • 19,824
  • 17
  • 99
  • 186
Rob m2
  • 13
  • 3
  • A [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) presented as a StackSnippet would probably be helpful, specifically including the broken button – Colby Mar 21 '23 at 22:08
  • Agree with Colby - you've provided a very huge amount of code here, and it will take folks quite a while to read through it all. If you can give us a slimmed-down, functional example of the problem you are having, that will get you a much better answer. – Jake Mar 21 '23 at 23:03
  • Thank you both for the reccomendations, my apologies, it was my first post. I tried to reduce the code living the parts where the error might be. Thank you! – Rob m2 Mar 21 '23 at 23:32
  • 1
    @Joshua: re your edit. Just so you know, the general preference for titles is sentence case, rather than title case. I suspect this follows the general guidelines in typography - title case is retained for newspaper headings for legacy/stylistic reasons. – halfer Mar 22 '23 at 11:28

1 Answers1

0

You have loaded the JS file twice, so the event listener is being added twice as well. As a result, the first event handler adds the .active class name to the elements, but the second one removes it right after since you're using classList.toggle('active') in your event handler. Although the element is updated in the DOM (you can inspect this behavior in a browser's devTools) no class name is added in the end.

To fix this, simply remove one of the script tags from your HTML.

Joshua
  • 3,055
  • 3
  • 22
  • 37
  • Hi @joshua, many thanks for your answer. I removed one of the script tag from my HTML but the error persists, when I click on the hamburger menu, menus don't show, the three lines of the hamburger only transform in 2 lines :(. – Rob m2 Mar 22 '23 at 10:03
  • Yep. Unfortunately, there are still some issues in the current code when the menu is expanded. But that's how you asked in your original post and my solution does fix the issue so the hamburger menu now works as it should. I suggest you ask another question about your layout issue. The hamburger menu part is relatively easy to fix, but the where and how to place the navigation when displayed on a mobile device requires additional clarification. – Joshua Mar 23 '23 at 01:22
  • 1
    Thank you Joshua, I will ask a new question submitting the latest code as per your suggestion – Rob m2 Mar 23 '23 at 14:44