Ways to set the threshold for displaying a hamburger menu based on width limit

Hey there,

After watching a youtube tutorial, I got inspired to create a hamburger menu. Everything was working smoothly when I resized my PC browser window to a specific width, as shown in the first screenshot:

https://i.sstatic.net/shreo.png

However, the issue arose when I tried accessing the same page on my Android (Samsung Galaxy S20) smartphone - the "classic" menu bar appeared instead.

https://i.sstatic.net/9GaH1.png

I'm thinking of adding a function that is specifically tailored for smartphones. This way, I can achieve the same hamburger menu on my smartphone as seen in the first image. Any suggestions on where to start?

To assist with troubleshooting, I have attached my html, css, and javascript files below.

Your help is greatly appreciated. Thanks!

Here's a snippet of my html code:

<!DOCTYPE html>
  <head>
    <link rel="stylesheet" type="text/css" href="css/mystyle.css?rnd=999" />
    <script type="text/javascript" src="scripts/menu.js"></script>
  </head>
  <body>
      <header>
      <nav class="navbar">
        <div class="branding">
          <h2><a href="#" class="branding-logo">Welcome</a></h2>
        </div>
        ...

Below is a snippet of my css code (mystyle.css):

:root {
  box-sizing: border-box;
  ...

And here is a snippet of my javascript code (menu.js):

const hamburger = document.querySelector(".hamburger");
hamburger.addEventListener("click", function () {
  this.classList.toggle("close");
});

Answer №1

HERE'S AN EXAMPLE FOR YOU TO TRY:-

<!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>
      <!-- Font Awesome -->
<link
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
rel="stylesheet"
/>
<!-- Google Fonts -->
<link
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
rel="stylesheet"
/>
<!-- MDB -->
<link
href="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.6.0/mdb.min.css"
rel="stylesheet"
/>
</head>
<body>
    <nav class="navbar navbar-light bg-light">
        <div class="container-fluid">
          <button class="navbar-toggler ms-auto" type="button" data-mdb-toggle="collapse"
            data-mdb-target="#navbarToggleExternalContent3" aria-controls="navbarToggleExternalContent3"
            aria-expanded="false" aria-label="Toggle navigation">
            <i class="fas fa-bars"></i>
          </button>
        </div>
      </nav>
      <div class="collapse" id="navbarToggleExternalContent3">
        <div class="bg-light shadow-3 p-4">
          <button class="btn btn-link btn-block border-bottom m-0">Link 1</button>
          <button class="btn btn-link btn-block border-bottom m-0">Link 2</button>
          <button class="btn btn-link btn-block m-0">Link 3</button>
        </div>
      </div>

      <!-- MDB -->
<script
type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.6.0/mdb.min.js"
></script>
</body>
</html>

Answer №2

Don't forget to include this:

<meta name="viewport" content="width=device-width, initial-scale=1" />

Adding the "viewport" meta tag is crucial as it instructs the browser to display web pages based on the device's width.

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

Vue's v-on:click feature stops functioning correctly post-build

I have successfully integrated the Vue slide example from this link into my Angular template. Everything works fine when running ng serve, but after building with ng build and then trying to start it again with ng serve or from the dist folder using npm s ...

What You See Is What You Get editor options

CKEditor is a fantastic HTML editor, but I've encountered an issue when pasting HTML structure and switching to WYSIWYG mode. The editor automatically reformats the HTML structure, causing unexpected changes to the original layout. I'm intereste ...

Prevent jQuery from scrolling once the fixed image reaches the bottom of the page

I am struggling to figure out how to keep an image fixed at the top of the footer div while scrolling to the bottom of a webpage. My approach involved calculating the height of the footer in order to adjust the scrolling behavior by subtracting it from th ...

Discover the secret sauce to effortlessly adding text upon hovering over a button

I am completely new to CSS and only know the basics. Recently, I stumbled upon this code online that creates a button. However, I want to use an image inside the button, add a color overlay when hovered, and display text saying "LEARN MORE" upon hovering ...

Save the raw InputMask data in Formik with Material-UI

I currently have Input Mask implemented with a customized Material UI text field inside a Formik form: <InputMask mask="999-99-9999" maskChar="X" value={values.ssn} ...

Issue with Twitter Bootstrap 3 Sticky Footer functionality in IE11 and IE Edge

My website works perfectly on Chrome, but I'm having issues on Internet Explorer. I would really appreciate your help. The website I am working on is: I am trying to implement a sticky footer on IE, but for some reason, it's not working as expe ...

What is the consensus on utilizing a primary key as the unique identifier for an HTML element?

A common practice I follow is to match the ids of the <ul> items in my HTML list with the primary keys from the database table. However, there are concerns about exposing these primary keys. Are there more secure and efficient ways to handle this w ...

Animate hovering over a specific element within a group of similar elements using jQuery

Hi there! I recently started exploring Js and jQuery, and I was able to create a cool width change animation on a div when hovering over another. However, I ran into an issue when trying to implement this with multiple sets of similar divs. Whenever I hove ...

A <div> with 100% height nested within a full-page div positioned at top:0 and bottom:0

Here's a simple problem. I need a full-height div (#inner) inside another full-height div (#outer, but with padding). This code displays correctly in Firefox and IE8, but not in IE7 and IE6. Edit: In the specific context where I'm using this s ...

Can jQuery be used to target pseudo elements such as scrollbars for selection?

Can you use jQuery to target pseudo elements like these: ::-webkit-scrollbar ::-webkit-scrollbar-track ::-webkit-scrollbar-thumb Is it possible to manipulate them through jQuery? #scroller { background: #fff; height: 300px; wid ...

Preview multiple images while uploading in a React JS application

In order to achieve multiple image upload with preview using React JS, I attempted the code below. However, I encountered an error in the console: Uncaught DOMException: Failed to set the 'value' property on 'HTMLInputElement': This ...

How can I use TailwindCSS in NextJS to remove the "gap" className from a component?

Currently, I am working on button components in NextJS with Tailwindcss and I am encountering a problem with some variants. Everything works fine, except when I remove the children (button text), I notice something strange that I can't quite figure ou ...

The Angular service/value is failing to retrieve the updated variable from the $(document).ready() function

Currently, I'm facing an issue with my Angular service/value. It seems to be grabbing the original variable instead of the new one that is supposed to be inside $(document).ready(). Here's the relevant code snippet: var app = angular.module("app ...

organize the values based on their priority using reactjs

I'm facing a challenge involving two arrays of objects: array1 and array2. I need to display only three values from both arrays, with priority given to array1. The requirements are as follows: if array1 contains 3 elements, all three should be shown. ...

What is the best way to retrieve an array that was created using the useEffect hook in React?

Utilizing the power of useEffect, I am fetching data from two different APIs to build an array. My goal is to access this array outside of useEffect and utilize it in the return statement below to render points on a map. However, when trying to access it ...

What causes the glitch when rendering HTML generated by PHP?

I'm facing an issue where the php-generated HTML content is different from the hard-coded version, even though they are supposed to be identical. The image displayed below showcases the incorrect layout caused by a function. As visible in the image, ...

Learn how to create a visually stunning CSS menu by centering a background image from the website cssmenumaker

I found a ready-made dropdown menu on . The menu is working well, but I would like to center the buttons. Is there a way to do this? Here is the CSS code: @import url(http://fonts.googleapis.com/css?family=Open+Sans:600); /* Menu CSS */#cssmenu, #cssmenu ...

The problem encountered with the Enzyme mount API is a TypeError where it is unable to read the property 'find' of an undefined variable

After converting my component to a stateless one, I started encountering an error that says: TypeError: Cannot read property 'find' of undefined Previously, my tests were running smoothly before the switch. As far as I know, you can test functio ...

Which option's value was recently removed from the fetch command?

I created a function to handle duplicate selections in a select element. It is functioning properly, but I noticed that when I remove an option from the select, my array remains unchanged. If I remove an option, I want my code to detect the value of that ...

Utilize a unique font exclusively for Internet Explorer

I am encountering a significant issue trying to utilize custom fonts in Internet Explorer. Currently, I have implemented a specific font using @font-face and it works perfectly in modern browsers but fails to render properly in IE. The method I am curren ...