Problem with the menu button overflowing

I'm exploring the process of designing burger menus for the mobile version of my site. I've successfully created a basic burger menu with some JavaScript assistance, but I encountered an overflow issue. The burger menu functions properly, but there is a horizontal scroll when it's not in use. I know that using position: relative and overflow: hidden could resolve this problem, but I'm unsure which element to apply these properties to. When I attempted to apply them to the parent header element, the burger menu expanded to the full height of the header. I'm struggling to find a solution to prevent this from happening.

HTML:

`<header class="header">
  <div class="header__container">
    <a href="#"
      ><div class="logo__block">
        <img src="img/logo.svg" alt="Logo" /></div
    ></a>
    <nav class="header__nav nav">
      <ul class="nav__list">
        <div class="burger-menu__exit">X</div>
        <li class="nav__item">
          <a href="#" class="nav__link">Home</a>
        </li>
        <li class="nav__item">
          <a href="#" class="nav__link">Features</a>
        </li>
        <li class="nav__item">
          <a href="#" class="nav__link">Pricing</a>
        </li>
        <li class="nav__item">
          <a href="#" class="nav__link">Blog</a>
        </li class="nav__item nav-btn">
      </ul>
    </nav>
    <a class="header-btn" href="#">Get Started</a>
    <div class="burger-menu">
        <div class="line line1"></div>
        <div class="line line2"></div>
        <div class="line line3"></div>
    </div>
  </div>
</header>`

CSS:

`body {
  font-family: "Libre Franklin", sans-serif;
}

[class$="__container"] {
  max-width: 1270px;
  margin: 0 auto;
  padding: 0 15px;
}

.header__container {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  line-height: 60px;
  padding-top: 40px;
  padding-bottom: 60px;
}

.header__container:before{
    position: relative;
}

.logo__block {
  display: flex;
  align-items: center;
}

.nav {
  margin-left: auto;
  
}

.nav__list {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
}

.nat__item {
}

.nav-btn {
  display: none;
}

.nav__list > li:not(.nav__item:last-child) {
  margin-right: 100px;
}

.nav__item:last-child {
  margin-right: 79px;
}

.nav__link {
  font-style: normal;
  font-weight: 400;
  font-size: 20px;
}

.nav__link:hover {
  border-bottom: 2px solid black;
}

.btn__block {
  max-width: 286px;
}

.header-btn {
  width: 211px;
  text-align: center;
  border-radius: 10px;
  font-style: normal;
  font-weight: 500;
  font-size: 20px;
  color: white;
  background-color: #ff7143;
  transition: 0.3s;
}

.header-btn:hover {
  background-color: #d1542b;
}

.burger-menu {
  display: none;
  margin-left: 10px;
  cursor: pointer;
}

.burger-menu__exit {
  display: none;
}

.line {
  width: 35px;
  height: 2px;
  background-color: black;
  margin-top: 10px;
}
.line1 {
  margin-top: 0;
}

@media (max-width: 1309px) {
  [class$="__container"] {
    max-width: 1103px;
  }
}

@media (max-width: 1129px) {
  [class$="__container"] {
    max-width: 1000px;
  }

  .nav__list > li:not(.nav__item:last-child) {
    margin-right: 50px;
  }

  .nav__item:last-child {
    margin-right: 79px;
  }
}

@media (max-width: 988.98px) {
  [class$="__container"] {
    max-width: none;
  }

  .burger-menu {
    display: block;
  }

  .header__nav {
    background-color: #5454d4;
    z-index: 9999;
  }
  .nav {
    position: absolute;
    top: 0;
    left: 100%;
    right: -100%;
    z-index: 999;
    min-width: 375px;
    height: 100vh;
    padding-top: 30px;
    color: #ff7143;
    transition: 1s;
  }

  .nav__list {
    position: relative;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  .nav__list a {
    color: #ff7143;
  }

  .nav__list a:hover {
    border-bottom: 2px solid #ff7143;
  }

  .nav__list > li:not(.nav__item:last-child) {
    margin-right: 0;
  }

  .nav__item:last-child {
    margin-right: 0;
  }

  .header-btn {
    margin-left: auto;
  }

  .burger-menu__exit {
    display: block;
    cursor: pointer;
    position: absolute;
    top: 0;
    right: 18px;
    font-size: 40px;
  }
}

@media (max-width: 512.98px) {
  .header-btn {
    display: none;
  }
}

@media (max-width: 369.98px) {
  .nav {
    min-width: 100%;
  }
}`

JS:

    const mobileNav = document.querySelector(".nav");
const headerButton = document.querySelector(".header-btn");
const headerContainer = document.querySelector(".header__container");

document.querySelector(".burger-menu").addEventListener("click", () => {
  mobileNav.style.right = 0;
});

document.querySelector(".burger-menu__exit").addEventListener("click", () => {
  mobileNav.style.right = -100 + "%";
});

Appreciate the assistance!

Answer №1

The problem was resolved by implementing a separate navigation for the burger menu outside of the header section and including the CSS code

position: relative; overflow: hidden;
in the html and body elements.

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

PHP XML loop encountering issues with functionality

Hi there, I'm a newcomer to stackoverflow, but I've been following this site for quite some time. Currently, I'm working on a simple web application that will serve as a catalogue for music tracks, allowing users to add their own tracks afte ...

The caption below the image is not functioning correctly when hovering over it

I'm having trouble getting the text to appear correctly underneath the image. Whenever I hover over the image, the text seems to overlap it. I am sure there is a simple solution to this issue, but I can't seem to figure it out. Removing the inlin ...

How can one retrieve elements from a loaded webpage?

I've been using the jQuery function .load() to load a page and I'm trying to find a way to access HTML elements within the loaded page from the source page once it's finished loading. The only method I've come across so far is to add pa ...

What is the best way to incorporate parallax scrolling in the center of a webpage?

I am trying to implement a parallax scrolling effect in the middle of my page, but it seems to be causing issues once I reach that section while scrolling. I attempted to use intersection observer, but unfortunately, it did not resolve the problem. const ...

Drag Bootstrap panels to the edge of the column

I need to position two panels in a column, with one on the left and the other on the right. Currently, they are not aligned properly, as shown in this image: https://i.stack.imgur.com/RU636.png The first panel should be moved to the left to align with the ...

Switch between different table rows

I have here a table that is used for displaying menu and submenu items. It's a mix of PHP (to fetch the menu items and their respective submenus) and HTML. What I am trying to figure out is how to toggle the visibility of only the submenu items under ...

employing rowspan functionality within a JavaScript application

How can I achieve a table layout where the first two columns are fixed for only one row and the remaining rows are repeated 7 times? Additionally, is there a way to make the bottom border of the last row thicker? Check out the expected table for reference. ...

Differences Between Mobile and Desktop Browser Media Queries

As I work on creating a responsive website, I have come across various examples of sites that adapt well to both desktop and mobile browsers. Currently, my stylesheet setup includes different media queries for various screen sizes. However, I've notic ...

Verify the presence of a particular attribute in an HTML tag using Capybara and polling techniques

In my HTML code, the attributes of buttons (such as "AAAAA") will change based on external events. This real-time update is achieved through AJAX pooling. <div class="parent"> <div class="group"><button title="AAAAA"/></div> <di ...

Angular ensures that the fixed display element matches the size of its neighboring sibling

I have a unique challenge where I want to fix a div to the bottom of the screen, but its width should always match the content it scrolls past. Visualize the scenario in this image: The issue arises when setting the div's width as a percentage of the ...

Showing the image as a backdrop while scrolling through text

How can I create an effect that continuously displays the image while scrolling text in Internet Explorer without using position: sticky or position: fixed? var sticky = document.querySelector('.sticky-container'); var img = document.querySele ...

Adjust the CSS to ensure that all elements have the height of the tallest element in the set

Can anyone help me solve a design issue I'm facing? I have a div with three floating buttons, but one of the buttons is taller than the others due to more content. I'm looking for a way to ensure that all buttons are the same height as the talle ...

Is there a way to position a ListItem (using React) in the lower left corner in this specific case?

import * as React from 'react'; import { styled, useTheme } from '@mui/material/styles'; import Box from '@mui/material/Box'; import MuiDrawer from '@mui/material/Drawer'; import MuiAppBar from '@mui/material/Ap ...

Email Template not rendering properly across various platforms

Dealing with a frustrating issue here. I created a template using Foundation's E-mail features and it looks perfect in Chrome's Device mode as well as in MailChimp. However, when testing it in Litmus and sending it to my own email, things start t ...

Tips for ensuring that 3 divs fill the height of their parent container

I'm facing an issue with 3 dynamic content divs inside a parent container. I want them to all have equal heights and fill the parent container. To demonstrate my problem, I've set up a simple example on jsfiddle. .parent { overflow: hidden; ...

Steps on how to trigger an onmouseover event for entire blocks of text:

I'm currently in search of an onmouseover code that seems to be elusive on the vast internet. A CSS box format has been successfully created: .box { float: left; width: 740px; height: 300px; margin-left: 10px; margin-top: 10px; padding: 5px; border: ...

I am looking to integrate my information into the user interface using Angular

import { Component, ViewEncapsulation } from '@angular/core'; import { Router } from '@angular/router'; import { Batch } from '../../../config/batchnew/batch.model'; import { BatchService } from '../../../config/batchnew ...

Element failing to adhere to CSS grid layout

I am currently working with grid layout and aiming to position the following content at the bottom and center, but it is currently aligning to the left: .body { margin: 0; background-color: rgb(255, 237, 237); display: grid; grid-template-rows: ...

What is the best way to display long text in a browser alert without it being cut off?

Seeking to display a large amount of data in an alert box, but only a portion is currently visible. The following text is all that can be seen: ["19467,1496257152583","19227,1496256651094","19469,1496257033968","17285, ...

Extract the content from the span element on Whatsapp Web

Currently, I am in the process of learning Python along with Selenium and have encountered a challenge. I am attempting to extract the username from the most recent message in a WhatsApp group conversation. Despite several attempts, I have been unsuccessfu ...