The hamburger menu for mobile devices is not functioning properly on the website's mobile version, however it operates correctly when the screen is resized

Currently, I am facing an issue with the hamburger menu not responding on my mobile device. Oddly enough, it does work when I resize my browser window to mimic a mobile size. There seems to be a glitch happening, but I'm struggling to pinpoint the exact cause. It's important to note that due to this project being for school, I am limited in using a lot of classes or IDs.

Here is the code snippet in question:

 <nav class="navbar">
        <a href="index.html">
            <img src="images/spotify.svg" alt="Logo van spotify">
        </a>

        <!-- Use span for hamburger bars -->
        <a href="#" onclick="myFunction()" class="toggle-button">
            <span></span>
            <span></span>
            <span></span>
        </a>
        <div id="active" class="navbar-links">
            <ul>
                <li><a href="premium.html">Premium</a></li>
                <li><a href="premium.html">Help</a></li>
                <li><a href="premium.html">Downloaden</a></li>
                <li><a href="premium.html">Account</a></li>
                <li><a href="premium.html">Uitloggen</a></li>
            </ul>
        </div>
    </nav>`

JS code snippet:

`function myFunction () {
   document.getElementById("active").classList.toggle("show")
}`

What I've attempted:

I experimented with other JavaScript functions, but they mostly broke my code. One did seem to somewhat work though:

`function myFunction() {
  var x = document.getElementById("active");
  if (x.style.display === "block") {
    x.style.display = "none";
  } else {
    x.style.display = "block";
  }
}

Although this code appeared to function correctly on desktop and mobile devices, it caused my navbar links to disappear entirely. This unexpected behavior left me puzzled as to why precisely it was happening. As I am a novice in this field, any assistance would be greatly appreciated.

You can find my GitHub repository here: https://github.com/Koayz/Spotify-Bryan-van-de-Pol And my website is live at:

Answer №1

While the hamburger menu is functioning properly on Samsung Mobile Browser, it seems to be encountering issues on Chrome Mobile Browser. This indicates that there may be a CSS rule present that is not compatible across different browsers. I will further investigate your git repository to identify any potential inconsistencies.


Upon closer examination, it appears that the use of empty spans to replicate the functionality of the hamburger menu could be causing the problem. In Firefox web browser, clicking anywhere on the 'toggle-button' <a> tag activates the menu. However, in Chrome web browser, clicking directly on the spans does not trigger any action. This discrepancy suggests that only the spans are being clicked on in the mobile version.

My recommendation would be to replace the hamburger spans with an actual <svg> element (which can easily be sourced online) and associate the function with that element. Alternatively, you could attach the function to both the spans individually as well as to the container element.


If the nav elements do not have a specified position, they will naturally flow within the document structure. To address this issue, you can set the height of the navbar to 50px and then change the position of navbar-links to absolute, adding a margin-top of 50px. Remember to adjust or unset these properties for larger screen views accordingly.

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

Update a specific element within Angular framework

Just starting out with angular and facing a seemingly simple issue that I can't seem to solve despite trying various solutions found on SO. I have created a login component where upon submission, the user is redirected to their profile page. While I a ...

Tips for creating responsive emails

Yesterday, I posted about my email newsletter and received some helpful feedback on creating a good structure. I implemented the suggestions provided into my newsletter and also added media query code to make layout adjustments for supported email clients. ...

The Typescript compiler is throwing an error in a JavaScript file, stating that "type aliases can only be used in a .ts file."

After transitioning a react js project to react js with typescript, I made sure to move all the code to the typescript react app and added types for every necessary library. In this process, I encountered an issue with a file called HeatLayer.js, which is ...

Executing JQuery asynchronous calls sequentially

Recently delving into the world of Jquery, I've encountered a coding challenge: my code loops through an array and retrieves HTML from an ajax request for each iteration. $.each(arr, function (data) { $.get('/Quote/LoadQuoteItemCost', { ...

Guidelines for linking a promise function to a JSX component

How can we use React components to handle the result of a promise function and map it to JSX components? <Promise on={myFunc}> <Pending> ... </Pending> <Resolved> {(data: any) => ( ... )} ...

Having issues with RTL on MuiTextField when using special characters

Currently, I am working on a project where Mui is being used. I have successfully applied RTL according to the Mui guide. However, I am encountering a frustrating problem where special characters in MuiTextField are aligning to the left instead of the righ ...

Declarations and expressions in Node.js using JavaScript

Recently diving into Node Js, I stumbled upon the following code snippet in a tutorial const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const questions = [ " ...

Click the button in Javascript to add new content

I am currently experimenting with JavaScript to dynamically add new content upon clicking a button. Although I have successfully implemented the JavaScript code to work when the button is clicked once, I would like it to produce a new 'hello world&ap ...

Implementing a sibling combinator technique within Material UI's useStyles framework

Is there a way to change the background of one div when hovering over another using material ui? The traditional CSS method is: #a:hover ~ #b { background: #ccc; } Here is my attempt with Material UI: const useStyles = makeStyles(theme => ({ ...

Learn how to gradually make text disappear and reappear using JavaScript or jQuery

I am a beginner in JavaScript and jQuery, and I am trying to achieve a text fade-out effect. Once the text has completely faded out, I want it to return with the same fade-in effect. I have been exploring if statements and fadeIn effects, but I am strugg ...

Is it possible to adjust the timezone settings on my GraphQL timestamp data?

I've come across a lot of helpful information regarding the usage of Date() and timezones, but something seems to be off. In my GraphQL setup (sourcing from Sanity), I have configured it to use formatString in this manner: export default function Minu ...

Excessive whitespace bordering the perimeter of the webpage

I've recently delved into the world of HTML/CSS and I'm working on creating a simple website. However, I'm encountering some white space-related issues above the header/body and around the sides. Despite trying various adjustments with margi ...

When utilizing jQuery to add a <li> element, it suddenly vanishes

? http://jsfiddle.net/AGinther/Ysq4a/ I'm encountering an issue where, upon submitting input, a list item should be created with the content from the text field. Strangely, it briefly appears on my website but not on the fiddle, and no text is appen ...

Two nested divs positioned below text within a parent div

I am styling a content div with text and two child divs positioned next to each other within the content div. My goal is to have the child divs display below the text, rather than beside it. body { text-align: center; } #first, #second { border: so ...

Angular Material Popup - Interactive Map from AGM

I am in the process of developing a material dialog to collect user location information using AGM (angular google maps). I have successfully implemented a map on my main page, but when the dialog is opened, it only shows a blank space instead of the map. ...

Surprising Behavior of React's OnClick Event

Custom Component function ProductCard ({image, name, stats, id}){ let dispatch = useDispatch() let quantity = 1 return ( <> <div className="product-card" ...

Node.js backend includes cookies in network response header that are not visible in the application's storage cookies tab

I am currently working on creating a simple cookie using express's res.cookie() function. However, I am facing an issue where the cookies are not being set correctly in the application tab. My project setup includes a React frontend and a Node backend ...

Generating a new date instance using a specified date string and locale settings

After scouring SO and coming up empty, I'm forging ahead with my question: I've got a date string that's dependent on locale, along with the locale info itself. For example, dateStr = '06/07/2021' and locale='en-GB'. H ...

The dimensions of the box are not predetermined by the size of the photo

I'm attempting to develop a photo gallery that emulates the style of (using the Unsplash API -> ) However, the size of the container box does not adjust properly with the photos. <div className="imageGrid__container"> <di ...

What is the process for transferring a JSON data object to the server with JavaScript XMLHttpRequest?

When I attempt to send an XMLHttpRequest to a PHP server, I am encountering difficulties in getting the $_POST or $_REQUEST object to be filled with the data I am sending using JavaScript: var r = new XMLHttpRequest; r.open("POST", "http://url.com", true ...