Hamburger menu for responsive navigation not showing up on the page

Despite my efforts, the "Hamburger menu" I created looks great but fails to function when clicked.

I have set up an event listener to detect any clicks on the button and toggle the class accordingly to show or hide the ul elements.

I am struggling to identify the mistake on my own. Is there a simpler solution available?

const toggleButton = document.getElementsByClassName('toggle-button')[0];
const navbarLinks = document.getElementsByClassName('navbar-links')[0];

toggleButton.addEventListener('click' ,function (){
    navbarLinks.classList.toggle('active')
});
/* Basic/Boiler css */
*{
  box-sizing: border-box;
}
body{
  margin: 0;
  padding: 0;
}
/* nav bar  */
.navbar{
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #000;
  color: white;
}
.navbar-logo{
  width: 10rem;
  height: 3rem;
  margin: 0.5rem;
  border-radius: 4px;
}
.navbar-links ul{
  margin: 0;
  padding: 0;
  display: flex;
}
.navbar-links li{
  list-style: none;
  transition: font-size 0.5s ease-out 100ms;
}
.navbar-links li a{
  text-decoration: none;
  color: white;
  padding-right: 1rem;
  display: block;

}
.navbar-links li:hover{
 font-size: 1.4rem;
}
/* Responsive Navbar */
.toggle-button{
  position: absolute;
  top: .75rem;
  right: 1rem;
  display: none;
  flex-direction: column;
  justify-content: space-between;
  width: 30px;
  height: 21px;
}
.toggle-button .bar{
  height: 3px;
  width: 100%;
  background: white;
  border-radius: 10px;
}
@media (max-width: 650px){
  .toggle-button{
      display: flex;
  }
  .navbar-links{
      width: 100%;
  }
  .navbar-links ul{
      flex-direction: column;
      width: 100%;
      display: none;
  }
  .navbar-links li a{
      padding: 10px;
  }
  .navbar-links li{
      text-align: center;
  }
  .navbar{
      flex-direction: column;
      align-items: flex-start;

  }
  .navbar-links.active{
      display: flex;
  }
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <meta http-equiv="X-UA-Compatible" content="IE=edge"></param>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="icon" href="/logo.png" type="image/jpg">
  <link rel="stylesheet" href="style.css">
</head>
<body>
<nav class="navbar">
    <div class="brand-titel"><img class="navbar-logo" src="/logo.png" alt="logo"></div>
    <a href="#" class="toggle-button">
      <span class="bar"></span>
      <span class="bar"></span>
      <span class="bar"></span>
    </a>
    <div class="navbar-links">
    <ul>
      <li><a href="">Home</a></li>
      <li><a href="">About</a></li>
      <li><a href="">Contact</a></li>
    </ul>
  </div>
</nav>
  <script src="front.js"></script>
</body>
</html>

The hamburger icon appears as expected, but remains unresponsive to clicks

Answer №1

You missed out on including the class .active in your CSS file. Additionally, ensure that your .navbar-links has display:none; instead of behaving as a list.

const toggleButton = document.getElementsByClassName('toggle-button')[0];
const navbarLinks = document.getElementsByClassName('navbar-links')[0];

toggleButton.addEventListener('click', function() {
  navbarLinks.classList.toggle('active');
});
/* Customized CSS */

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
}


/* Navigation bar styling */

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #000;
  color: white;
}

.navbar-logo {
  width: 10rem;
  height: 3rem;
  margin: 0.5rem;
  border-radius: 4px;
}

.navbar-links ul {
  margin: 0;
  padding: 0;
  display: flex;
}

.navbar-links li {
  list-style: none;
  transition: font-size 0.5s ease-out 100ms;
}

.navbar-links li a {
  text-decoration: none;
  color: white;
  padding-right: 1rem;
  display: block;
}

.navbar-links li:hover {
  font-size: 1.4rem;
}


/* Responsive Navbar styling */

.toggle-button {
  position: absolute;
  top: .75rem;
  right: 1rem;
  display: none;
  flex-direction: column;
  justify-content: space-between;
  width: 30px;
  height: 21px;
}

.toggle-button .bar {
  height: 3px;
  width: 100%;
  background: white;
  border-radius: 10px;
}

@media (max-width: 650px) {
  .toggle-button {
    display: flex;
  }
  .navbar-links {
    width: 100%;
    display: none;
  }
  .active {
    display: block;
    padding: 25px;
    color: white;
    font-size: 25px;
    box-sizing: border-box;
  }
  .navbar-links ul {
    flex-direction: column;
    width: 100%;
  }
  .navbar-links li a {
    padding: 10px;
  }
  .navbar-links li {
    text-align: center;
  }
  .navbar {
    flex-direction: column;
    align-items: flex-start;
  }
  .navbar-links .active {
    display: flex;
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="icon" href="/logo.png" type="image/jpg">
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <nav class="navbar">
    <div class="brand-titel"><img class="navbar-logo" src="/logo.png" alt="logo"></div>
    <a href="#" class="toggle-button">
      <span class="bar"></span>
      <span class="bar"></span>
      <span class="bar"></span>
    </a>
    <div class="navbar-links">
      <ul>
        <li><a href="">Home</a></li>
        <li><a href="">About</a></li>
        <li><a href="">Contact</a></li>
      </ul>
    </div>
  </nav>
  <script src="front.js"></script>
</body>

</html>

Answer №2

The ul style has been set to display: none. To override this when the active class is applied, add the following rule at the end of the media query:

Insert ul {display: flex} in your code like so:

@media (max-width: 650px){
  ...
  .navbar-links.active ul{
    display: flex
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to show a div element following an li element using only CSS?

My tab structure consists of the following elements: <ul> <li id="first">First header</li> <li id="second">Second header</li> <li id="third">Third header</li> <li id="fourth">Fourth header< ...

What is the best way to send a parameter from JavaScript to a UserControl?

Within my ASP.Net application, I have a UserControl named EmployeesUserControl that is displayed within a JQuery modal dialog. Prior to displaying the Employees, I need to provide a filter criteria indicating which entity the employee belongs to. My query ...

Updating Vue component property when Vuex store state changes: A step-by-step guide

As I work on developing a straightforward presentation tool using Vue js and Vuex to manage the app state, I am facing a challenge in implementing a feature that tracks changes in the presentation such as title modifications or slide additions/removals. Cu ...

Discover the steps to retrieve a fresh array in PHP after utilizing sortable.js

Hi there! I am currently utilizing the sortable javascript feature found on Although the script is working perfectly fine for me, being a non-expert in javascript poses a challenge when it comes to generating the necessary code to retrieve an array in php ...

There seems to be a mysql error with a syntax issue causing an ER_PARSE_ERROR with errno 106

Encountered a Syntax error: You have an error in your SQL syntax, please refer to the manual corresponding to your MySQL server version for the correct syntax to use near :data at line 1. errno: 1064 SqlState: '42000' This is The code used for ...

What are some methods for restricting my application to only one browser tab?

Currently, the functionality in v1.0 that requires three form submissions and uses session data to hold intermediate information is causing too much trouble. Users starting an operation, then opening a second tab and performing another operation which over ...

Despite returning an "OK" status, the jQuery Ajax Codebehind Post fails to function properly

Attempting to call a function in ASP.NET with jQuery Ajax: var params = "{'name':" + "\"" + name + "\"}"; $.ajax({ type: "POST", url: "CreateTopic.aspx/CreateNewTopic", data: params, ...

What could be causing this jQuery to malfunction?

Having trouble with the functionality of hasclass and this.addclass. if ($('.sidebar-menu-container li').hasClass('current-menu-item')) { $(this).addClass('expanded'); } Check out this CodePen for reference. ...

Unpack and retrieve multiple values

Currently diving into the world of JavaScript and exploring destructuring. My goal is to display the following in the console: "Name: Mike Smith, Father: Harry Smith" "Name: Tom Jones, Father: Richard Jones" Encountering an error ...

The onchange event for the input type=file is failing to trigger on Google Chrome and Firefox

Update: Solved: http://jsfiddle.net/TmUmG/230/ Original question: I am facing an issue with handling image/logo upload alongside a hidden input type=file: <form class="image fit" id="theuploadform"> <input title="click me to chan ...

What causes an Ajax call to be triggered when clicking on a tab that changes state but is not related to the Ajax functionality?

I currently have a basic layout set up. Within the <Navbar>, there is a <StateSelector> which, when clicked on, triggers a specific method. The innerHTML value of the button in <StateSelector> is passed as an argument to a function that ...

Revert TinyMCE container

I need assistance with clearing the content inside the editor box of TinyMCE when a button on my form is clicked. The editor I am using can be found here. Could you provide guidance on how to achieve this? ...

Creating a Date Scalar in GraphQL-JS: A Step-by-Step Guide

Currently, I am in the process of defining a custom scalar within GraphQL in order to access and manipulate Dates from my MongoDB collections. Although I am still grasping the concept of what a scalar is and its purpose, it appears to be a type that I can ...

Unable to display SVG icon in Highcharts rendering

Is there a way to add a specific symbol in SVG format to a graph using Highcharts? Highcharts.SVGRenderer.prototype.symbols.download = function (x, y, w, h) { var path = [ "M19 5v14H5V5h14m1.1-2H3.9c-.5 0-.9.4-.9.9v16.2c0 .4.4.9.9.9h16.2c.4 0 ...

PHP updating data in a MySQL database

I've been using easyPHP and encountering issues with updating records in my database. Whenever I try to update, the HTML form displays <?php echo $btitle; ?> and <?php echo $bauthor; ?> instead of updating the data. However, it does print ...

django project: template with missing node_modules in static folder

I have obtained a template example that I would like to use in my Django project. I have organized all of my static files within the static folder and made the necessary modifications in settings.py. However, I am encountering an issue with the following l ...

Error: Table 0 does not exist and cannot be found due to an Index

Dal code private DataSet GetLoginData(string username, string password, string email, string action) { SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand("", connection); comman ...

What is the best way to add two different texts (each with a distinct font) and an image into a NatTable cell while also adjusting the margins?

Just a couple of queries: 1) Is it possible to incorporate two different text fonts and an image into a NatTable cell? 2) How can the margins be set up like in the example image below? In different scenarios: a) Through Java 1.6 (without RichTexCellEdi ...

Keep the multiselect dropdown list of the select component open

I am currently utilizing the Select component from material-ui-next. Overall, it functions quite smoothly. One scenario where I implement it is within a cell element of an Ag-Grid. Specifically, in this use case, I am making use of the multiselect feature ...

Adding Material-UI icons dynamically in a React TypeScript project requires understanding the integration of imported icons

I have a collection of menu buttons with icons, stored in an array of objects. The icon names are saved as strings that correspond to Material UI icons: interface MenuButton { text: string, onClickFunction: Function icon: string } export defau ...