Moving the hamburger menu below the first row

I'm working on creating a responsive menu. Everything is functioning well, but I have encountered an issue when the screen size reaches 1024px. I want to be able to click on the hamburger menu and align the items in the center, with each item displayed on a separate row beneath the navbar. However, my items are not displaying on the second line after clicking the hamburger menu.

Here's the code snippet: https://codepen.io/S4UCY/pen/VwamrBb

I am aiming for something similar to this example: https://codepen.io/ladyareum/pen/eJVoPP

const hamburger = document.getElementById('hamburger');
const menuUL = document.getElementById('menu-list');

hamburger.addEventListener('click',  () => {
  menuUL.classList.toggle('show');
});
* GENERAL */
html, body {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

*, *:before, *:after {
    box-sizing: inherit;
}

/* * {
    outline:2px solid red;
} */

body {
    font-family: 'Montserrat',sans-serif;
  font-size: 100%;
  width: 100%;
}

img {
    max-width: 100%;
  height: auto;
}

.group:before,
.group:after {
    content: " ";
    display: table;
}

.group:after{
    clear: both;
}

.container {
    position: relative;
  margin-right: auto;
  margin-left: auto;
  padding-left: 15px;
  padding-right: 15px;
}

/* NAVBAR */
.navbar {
    border: none;
    font-size: 1.1428571428571428em;
    z-index: 1000;
    background-color: #cc9efd;
}
...
 <!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Lorem</title>
    <link rel="stylesheet" href="css/reset.css">
    ...
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    ...
</html>

Answer №1

I made some modifications to the code within

@media screen and (max-width: 1024px)
, specifically targeting .menu-list li:hover ul.subs, .menu, and removing position:absolute from .hamburger. I hope this adjustment resolves the issue you were experiencing.

const hamburger = document.getElementById('hamburger');
const menuUL = document.getElementById('menu-list');

hamburger.addEventListener('click',  () => {
  menuUL.classList.toggle('show');
});
* CUSTOM STYLES */
html, body {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

*, *:before, *:after {
    box-sizing: inherit;
}

/* Body styling */
body {
    font-family: 'Montserrat', sans-serif;
    font-size: 100%;
    width: 100%;
}

img {
    max-width: 100%;
    height: auto;
}

.group:before,
.group:after {
    content: " ";
    display: table;
}

.group:after{
    clear: both;
}

.container {
    position: relative;
    margin-right: auto;
    margin-left: auto;
    padding-left: 15px;
    padding-right: 15px;
}

/* Navbar styles */
.navbar {
    border: none;
    font-size: 1.1428571428571428em;
    z-index: 1000;
    background-color: #cc9efd;
}

.navbar {
    position: relative;
    margin: 0;
    padding: 0;
    border: 1px solid transparent;
}


/* Media queries for smaller screens */
@media screen and (max-width: 1024px) {
    .hamburger {
        /* Removed position: absolute */
        display: inline-block;
        right: 0;
        margin-right: 5px;
    }

    .menu {
        width: 100%;
    }

    .menu-list {
        display: none;
    }

    .menu-list li {
        float: none;
    }


}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    
    <title>Title of the document</title>

  </head>
  <body>
    <header>
      <div class="navbar">
        <div class="container">
          <div class="logo-place">
            <span class="site-title"><a href="index.html">LOREM</a></span>
          </div>
          <button class="hamburger" id="hamburger"><i class="fas fa-bars"></i></button>
          <div class="menu">
            <ul class="menu-list" id="menu-list">
                <li><a href="#">Home</a></li>
                <li><a href="#">About <i class="fas fa-caret-down"></i></a>
                    <ul class="subs">
                        <li><a href="#">About me</a></li>
                        <li><a href="#">Gallery</a></li>
                    </ul>
                </li>
                <li><a href="#">Tools <i class="fas fa-caret-down"></i></a>
                    <ul class="subs">
                        <li><a href="#">First mode</a></li>
                        <li><a href="#">Second mode</a></li>
                    </ul>
                </li>
                <li><a href="#">Back-up <i class="fas fa-caret-down"></i></a>
                  <ul class="subs">
                      <li><a href="#">1 month</a></li>
                      <li><a href="#">1 year</a></li>
                  </ul>
                </li>
                <li><a href="#">Contact</a></li>
            </ul>
          </div>
          <div id="second-menu-navi-header" class="header-right">

            <a id="tab-1-header" href="#"><i class="fab fa-instagram instagram-logo-footer"></i></a>
            <a id="tab-2-header" href="#"><i class="fab fa-youtube youtube-logo-footer"></i></a>
            <a id="tab-3-header" href="#"><i class="fab fa-twitter twitter-logo-footer"></i></a>

          </div>
        </div>
      </div>


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

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

Looking to create multiple image popups for different models using JavaScript? I am currently utilizing the W3Schools model image for this purpose

I am having trouble getting multiple image models to display, as only one pop-up is appearing. Can someone please assist me in figuring out how to show multiple pop-ups? // Obtain the modal element var modal = document.getElementById("myModal") ...

Creating a styled slider thumb using Styled Components

Having some trouble styling a slider using styled-components for React. Specifically, I am unsure how to style the thumb of the slider. Here is the CSS code I have: .faderInput::-webkit-slider-thumb { -webkit-appearance: none; width: 15px; heig ...

Navigating data within a JSON file with D3 javascript: a step-by-step guide

Currently, I am attempting to extract and manipulate data from a JSON file using D3 Javascript. Below is the content of the JSON file: { "Resources": [ { "subject": "Node 1", "group" : "1" }, { "predicate": ...

Ajax is failing to load unless the page is manually refreshed

Having trouble with this AJAX code, it only works after refreshing the page. Any ideas on what could be causing this issue? Thanks! $(document).ready(function() { fetch_data(); function fetch_data(){ var action = "fetch"; $.ajax( ...

What is the best way to choose a group of DOM elements to display after receiving data from an AJAX request?

I am facing an issue with my HTML code: <html> <head> <style> .selected { color: red; } #myContainer div { border: 1px solid #333; } </style> <script type="tex ...

Obtain the output of the msDropdown Jquery plugin

For my project, I am utilizing the json version of the msDropdown plugin which can be found at this LINK. I have a basic question regarding how to retrieve the value from the example page shown on that link. Although I am not very proficient in JavaScript ...

Can someone explain how to implement an onclick event for a div element inside an iframe using jquery or javascript?

Understanding the HTML Structure <iframe src="#" scrolling="no" id="gallery-frame"> <div class="gv_filmstrip"> <div class="gv_frame"> <div class="gv_thumbnail current"> <img src="images/grandstand-padang-right/1.jpg"> </d ...

How to customize text within an HTML <option> tag within a <select> element

Customizing the appearance of items in an option list can be easily achieved by using CSS: <select> <option>Option 1</option> <option style="color: #F00; font-weight: bold; padding-left:2em;">Option 2</option> <opt ...

React hooks are causing an endless fetching loop when receiving data from a child component, but it functions without any issues if the parent is a class component

One of my components is a SearchHeader component that features a controlled input, allowing it to pass data from the input to its parent components SearchHeader.js const SearchHeader = ({ onChangeSearch }) => { const [searchValue, setSearchValue] = ...

What is the best way to display data stored in local storage using React and Typescript?

Is there a way for the data to be displayed on the browser below the save button when I click save? My setup involves using React with a TypeScript template. function ButtonDefaultExample(props: IButtonExampleProps) { const { disabled, checked } = pro ...

Error: The outcome of the function is not being displayed

I've been encountering an issue with rendering the result of the function renderVerticalCards. Despite confirming that the function is indeed being called through the console, nothing is displayed in the DOM except for the outer divs from outside the ...

Assistance needed in clearing a float towards the left

My unordered list is floating left and has a CSS hover menu, but it's not properly clearing the next unordered list below it. I've attempted to use clear: both without success. Any other suggestions? I also tried using overflow auto on the div t ...

Having difficulty adjusting certain internal styles within Material UI's <Dialog> component

I want to customize the styling of my <Dialog> component by adding rounded corners using a border radius. Here is the inline style I am trying to apply to override the default styles of the <Dialog>: let overrideStyles = { padding: 0, marg ...

What are the steps for utilizing ReGex on a webpage?

Recently discovered Regex, so apologies if this is a beginner question, but I want to implement it on a webpage. I gave it a try, but it didn't work. Is there a specific library, SDK, or piece of code that I need to add? Here is the code I currently h ...

What is the best way to create a triangle shape that is responsive to elements?

I'm trying to make a background shape with a triangle inclusion using CSS. I want it to be responsive to all screen sizes, but I'm having some trouble. Any tips on how to achieve this? header { width: 100%; height:150px; background: #eee; } m ...

Can one integrate various angular components constructed using distinct versions of Angular?

Is it feasible to utilize various angular components (custom elements) created with different versions of Angular? I've heard concerns about zone.js causing global scope pollution. Appreciate your response! ...

Unexpected disconnection from Socket.io server

Utilizing node.js service and angular client with socket.io for extended duration http requests. Service: export const socketArray: SocketIO.Socket[] = []; export let socketMapping: {[socketId: string]: number} = {}; const socketRegister: hapi.Plugin< ...

Parameter within onClick function that includes a dot

I'm attempting to design a table that enables an onClick function for the Change Password column's items so my system administrator can adjust everyone's password. Each onClick triggers the "ChangePassOpen" function which opens a modal with ...

Upon postback, the Jquery Chosen plugin dynamically creates a new section

I've integrated the Jquery Chosen plugin into my MVC application and everything is loading correctly at first. However, when I refresh a div on the page with data from the server using an ajax call, it ends up creating a duplicate section. Is there a ...

refresh shopping cart page when coupons are added or removed in woocommerce

Is there a way to prevent ajax reloading for coupons on the cart page in woocommerce when adding or removing them? I'd like this to be done with a manual page refresh. Any tips on how to achieve this would be greatly appreciated. Thank you! ...