Unusual spacing issue appears after hamburger menu due to absolute positioning

I'm looking to position my hamburger menu to the right of the browser, but I've noticed a strange gap when using absolute positioning for the icon and main menu. Here is an image reference and you can check out my code.

$(function () {
    $("#hamburger").on("click", function () {
      $(this).toggleClass("close");
      $("#nav").toggleClass("visible");
    });
  });
...

The issue persists even after adding overflow:hidden to both the icon and nav menu elements. Any suggestions on how to resolve this would be greatly appreciated.

Unfortunately, I'm at a loss for how to fix this problem.

Answer №1

To solve the issue with horizontal scrolling, include overflow-x: hidden in the style for the body element. The problem arises from the width of the nav element being set to 200px, which matches the width of the horizontal scroll. Even though the nav is positioned absolutely and removed from the document flow, it still contributes to the overall width calculation.

$(function() {
  $("#hamburger").on("click", function() {
    $(this).toggleClass("close");
    $("#nav").toggleClass("visible");
  });
});
/* ADDED */

body {
  overflow-x: hidden;
}

.myHeader {
  background: #2e3047;
}

#hamburger {
  height: 50px;
  width: 50px;
  background: #3bba9c;
  position: absolute;
  overflow: hidden !important;
  top: 20px;
  right: 20px;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 0 4px rgba(0, 0, 0, 0);
  transition: all 0.2s ease;
  -webkit-transition: all 0.2s ease;
  -moz-transition: all 0.2 ease;
  z-index: 9999;
}

#hamburger:hover {
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.1);
  transform: scale(1.1);
  -webkit-transform: scale(1.1);
  -moz-transform: scale(1.1);
  transition: all 0.2s ease;
  -webkit-transition: all 0.2s ease;
  -moz-transition: all 0.2s ease;
}

#hamburger span {
  display: block;
  background-color: #2e3047;
  position: absolute;
  height: 2px;
  width: 28px;
  left: 50%;
  transform: translateX(-50%);
  -webkit-transform: translateX(-50%);
  -moz-transform: translateX(-50%);
  border-radius: 10px;
  transition: all 0.15s ease;
  -webkit-transition: all 0.15s ease;
  -moz-transition: all 0.15s ease;
}

#hamburger span:first-child {
  top: 17px;
}

#hamburger span:nth-child(2) {
  top: 25px;
}

#hamburger span:last-child {
  top: 33px;
}

#hamburger.close {
  top: 10px;
  right: 10px;
  box-shadow: 0 0 8px rgba(0, 0, 0, 0.15);
}

#hamburger.close span:first-child {
  top: 15px;
  transform: translate(-14px, 10px) rotate(135deg);
  -webkit-transform: translate(-14px, 10px) rotate(135deg);
  -moz-transform: translate(-14px, 10px) rotate(135deg);
}

#hamburger.close span:nth-child(2) {
  left: -20px;
  opacity: 0;
}

#hamburger.close span:last-child {
  top: 15px;
  transform: translate(-14px, 10px) rotate(-135deg);
  -webkit-transform: translate(-14px, 10px) rotate(-135deg);
  -moz-transform: translate(-14px, 10px) rotate(-135deg);
}

#nav {
  height: 100vh;
  right: 0;
  top: 0;
  background: #3bba9c;
  color: #2e3047;
  width: 200px;
  box-shadow: 0 1px 20px rgba(0, 0, 0, 0.25);
  position: absolute;
  overflow: hidden !important;
  z-index: 1;
  transform: translateX(200px);
  -webkit-transform: translateX(200px);
  -moz-transform: translateX(200px);
  opacity: 0;
  transition: all 0.2s ease-in-out;
  -webkit-transition: all 0.2s ease-in-out;
  -moz-transition: all 0.15s ease-in-out;
}

#nav ul {
  list-style: none;
  padding: 0;
}

#nav ul li a {
  display: block;
  text-decoration: none;
  padding: 20px;
  color: #2e3047;
}

#nav ul li a:hover {
  color: #fafbff;
}

#nav.visible {
  transform: translateX(0);
  -webkit-transform: translateX(0);
  -moz-transform: translateX(0);
  opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="myHeader">
  <div id="hamburger" class="visible">
    <span></span>
    <span></span>
    <span></span>
  </div>

  <div id="nav" class="close">
...
  </div>
</div>

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

The CSS child selector seems to be malfunctioning as it is affecting all descendants of the specified type

Struggling with creating a menu containing nested lists. Attempted using a child selector (#menu-novo li:hover > ul) to display only immediate descendants, but all are still showing. Any suggestions or solutions for this issue? #menu-novo-container { ...

Adjust CSS style height dynamically

I am currently creating a leaderboard for users: <div class="user"> <ul id="jogadores" *ngFor="let u of sortedUsers$; let i=index;" (click)="routeToUser(u.id)"> <li class="user"> <img class="gravatar" src="https://www.gra ...

The HTML marquee element allows text to scroll sideways or

Has the html marquee tag been phased out? If so, what are the current alternatives available for achieving a similar effect on modern browsers? I am looking to implement a basic marquee effect on my Joomla page. ...

Returning a button to its original state

Currently, I am working on styling the video controls on my website and have run into a small issue. The video section consists of a video list and a video player. When an li element from the list is clicked, the source of the video player changes to the ...

Tracking changes in real time and calculating the sum with AJAX, PHP, and MySQL for efficient processing

Initially, I kindly request you to read this until the end. I am in dire need of assistance with my problem as I have searched for solutions but still remain clueless. https://i.sstatic.net/DAb1q.png Referring to the image provided, the first 'Produ ...

Having trouble with AJAX within AJAX not functioning properly?

Similar to the Facebook commenting system, I am aiming for comments to be appended to previous ones and displayed all at once. However, currently the comments only show up after the page is reloaded. Please Note: Each post initially loads with just two co ...

tinyScroll currently disabled

Attempting to implement the tinyscrollbar plugin found at Here is the code snippet: $('#nvl2 .content').html( '<div class="scrollbar">'+ '<div class="track">'+ ...

What is the most effective method for creating a dark background with light text in Bootstrap without using SASS?

I recently created a new website using Bootstrap 5 and set the background color to black (#000000) by adding some CSS styling. However, I noticed that this change had unintended consequences on the appearance of various elements such as drop shadows, modal ...

Can the load API in jQuery be utilized to load fragments using a class selector instead of an id?

I've been working on enhancing a website's dashboard by incorporating more widgets onto the page. Interestingly, these widgets are already present on another page within the same domain. After some research, I discovered that utilizing the load A ...

The CSS transition will only activate when coupled with a `setTimeout` function

After using JavaScript to adjust the opacity of an element from 0 to 1, I expected it to instantly disappear and then slowly fade back in. However, nothing seems to happen as anticipated. Interestingly, if I insert a setTimeout function before applying th ...

A Webpage that is permanently open, with no option to close it. The only option is to watch the video in

Currently, I am designing web pages for artists to showcase their work in an upcoming exhibition. The pages are ready, but now the artists are requesting that each piece be accompanied by a laptop displaying a single webpage with a video providing informat ...

Learn how to use onclick event in an anchor tag to send post values through Ajax!

Can I send post values via AJAX using an onclick event on an anchor tag? I've tried the following code but I'm not receiving any post values. How can I make it work? ............................................................................. ...

Unable to establish proper functionality of username variables in Node.js chat application

For my latest project, I am developing a chat application in node.js following a tutorial from socket.io. The main objective of the app is to allow users to input their username along with a message, which will then be displayed as "username: message" in t ...

Dealing with a routing issue in node.js/express involving JavaScript and CSS

I'm facing an issue. I need to set up a route from localhost.../cars to localhost../bmw/x1 On the localhost../cars page, there's a button that, when clicked, should load localhost../bmw/x1 This is the JavaScript code I have: const express = req ...

How can you prevent an element from overflowing when employing window.pageYOffset and using a switch case to alter the background color at specified pixel thresholds?

I want the <body> element's background-color to change every 500px as I scroll down. I've scoured the web for answers, but still can't figure out what's going wrong. Please review the code. However, Firefox Browser indicates that ...

Disable the ability to select text when double-clicking

Is there a way to prevent text selection on double click while still allowing selection on mouse drag? Whenever I try to remove selection on dblclick or mouseup, it flashes, which is not the desired outcome as shown in this jsfiddle. UPD: I am not lookin ...

Display images next to each other with no need for a scroll bar

I'm currently developing a Roulette website and I am struggling to make the roulette animation function properly. I have an image for the roulette wheel, but I need the image to vanish after reaching a certain point and then loop around to the left. ...

What could be the reason for the clone function not duplicating the data within the table

Recently, I utilized jQuery's clone method to duplicate and add an HTML table. The rows and cells of the table were added using jQuery. However, despite using clone, it only replicated the headers and CSS, not the appended data. This raises the ques ...

When the browser window is minimized, the @media CSS will wrap to the bottom line

When the browser has a standard resolution, the layout looks like this: https://i.sstatic.net/tNR6X.png However, on smaller screens, it appears like this: https://i.sstatic.net/U3RnZ.png Is there a way to adjust the positioning of these blocks using @m ...

Identify support for the :first-child pseudo-class

Is there a way to determine with JavaScript whether the browser is compatible with the CSS :first-child selector? ...