Why does the Hamburger Menu shift my website's logo when it opens?

I'm in the process of implementing a hamburger menu on my website.

My attempts have involved adjusting the positioning of both the #logo and .topnav elements.

Code Snippet source

function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
html {
  height: 100%;
  width: 100%;
}

body {
  display: block;
  background-color: #000000;
}

/* ... Other CSS rules here ... */

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

Answer №1

To maintain the position of the logo, you must adjust the position of both .topnav and .topnav.responsive to absolute, as shown below:

function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
html {
  height: 100%;
  width: 100%;
}

body {
  display: block;
  background-color: #000000;
}

.topnav {
  display: block;
  float: left;
  background-color: #000000;
  overflow: hidden;
  position: absolute; /*updated value*/
}

.topnav a {
  float: none;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.topnav a.active {
  background-color: #fee238;
  color: white;
}

section {
  background-image: url("https://i.postimg.cc/kGdygTgL/background-image.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  height: 100vh;
  display: block;
  width: 100%;
  top: 50%;
  text-align: center;
  line-height: 1.2em;
  position: relative;
  z-index: 2;
  font-weight: 500;
  color: #fff;
  letter-spacing: 5px;
  margin: 0px auto;
}

h1 {
  line-height: 85%;
  text-align: center;
  font-size: 40px;
  font-family: zeitung;
  color: #ffffff;
  margin-bottom: 30px;
}

.header_nav {
  margin: auto;
  width: 60%;
  text-align: center;
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  font-weight: bold;
  color: #ffe238;
  display: inline;
  justify-content: space-evenly;
}

.logo {
  display: inline-block;
}

#logo {
  width: 25%;
  height: auto;
  border: 5px solid #000000;
  background-color: #000000;
  ;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  margin: 0 auto;
  display: inline-block;
  z-index: 2;
}

@media screen and (max-width: 600px) {
  .topnav a:not(.icon) {
    display: none;
  }
  .topnav a.icon {
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {
    position: absolute; /*was relative*/
  }
  .topnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    display: block;
    text-align: left;
  }
}

@media screen and (min-width:600px) {
  .topnav a {
    float: left;
    display: block;
  }
  .topnav .icon {
    display: none
  }
  #logo {
    width: 10%
  }
}

a:link {
  text-decoration: none;
  color: #ffffff;
}

a:visited {
  text-decoration: none;
  color: #ffffff;
}

.current {
  text-decoration: underline;
}

ul {
  list-style-type: none;
  display: inline-flex;
  justify-content: space-evenly;
  width: 90%;
}

header {
  text-align: center;
  position: fixed;
  height: 75px;
  background: #000;
  width: 100%;
  right: 0;
  top: 0;
  z-index: 9998;
  -webkit-transition: all ease-in 200ms;
  -moz-transition: all ease-in 200ms;
  -o-transition: all ease-in 200ms;
  transition: all ease-in 200ms;
}

.heading {
  position: absolute;
  width: 300px;
  height: 200px;
  z-index: 15;
  top: 50%;
  left: 50%;
  margin: -100px 0 0 -150px;
}
<header>
  <div class="topnav" id="myTopnav">
    <a href="javascript:void(0);" class="icon" onclick="myFunction()">
      <i class="fa fa-bars"></i>
    </a>
    <a href="index.html" class="active">Home</a>
    <a href="menus.html">Our Menus</a>
    <a href="about.html">About</a>
  </div>
  <div class="logo">
    <img src="https://i.postimg.cc/d0mpFZ6s/logo.png" alt="" id="logo">
  </div>
</header>

<section></section>


<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

The reason for changing from relative to absolute is to prevent the width of .topnav from affecting the logo's positioning.

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

Using jQuery to slide in dynamically generated content

I am in the process of developing a straightforward content slider that has the capability to manage dynamically added content. Unfortunately, none of the supposedly "lightweight" plugins I came across offered this functionality or, if they did, it was not ...

Preventing default behavior in a JQuery post request

Encountering an issue with jQuery functionality on a mobile device. function send() { $.post("scripts/post.php", { username: $("input[name=username]").val(), password: $("input[name=password]").val() }, function(data) { if ($(".data div" ...

Issue with Submit Event in React - Enter Key Fails to Trigger

I'm currently experimenting with a small front-end react project that's using Soundcloud's API. The project is quite basic at the moment - it takes user input and queries the API for related songs. I've encountered an issue where the en ...

Discrepancy in Angular Material Buttons with theme design

I recently installed Angular 10 along with Angular Materials using the command ng add @angular/material I opted for the custom theme: Purple/Green The next step was to add a Toolbar by simply copying and pasting code from Google's site. However, I a ...

Error: The property 'length' cannot be read from an undefined parent causing Uncaught TypeError

Hey there, take a look at this cool stuff http://jsfiddle.net/J9Tza/ <form class="validation"> <div> <input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" pattern=".{3,200}" title="3 to 200 characters" r ...

The following error occurred: Next Auth TypeError: URL is not valid

Every time I run my Nextjs project, I keep encountering an issue related to Next Auth. Despite using version 4.3.1, I am unsure about how to resolve it. Any assistance with next-auth would be greatly appreciated. Although I attempted to update my next-aut ...

I am looking to gather user input in JavaScript and then showcase that input on the webpage. What would be the best

I am currently learning Java and I decided to challenge myself by creating a hangman game. The issue I am facing is that when the user inputs a letter, nothing happens - there is no output indicating whether the guess was correct or incorrect. I suspect th ...

Angular is unable to iterate through a collection of ElementRef instances

My list of ElementRef contains all my inputs, but adding listeners to them seems to indicate that textInputs is empty even though it's not. @ViewChildren('text_input') textInputs!: QueryList<ElementRef>; ngAfterViewInit(): void { ...

Exploring CryptoJS in a Vue.js project

https://github.com/brix/crypto-js I successfully installed CryptoJS using npm i crypto-js. However, I am facing difficulty in integrating it into my project. When I attempt to use the following code: // Decrypt var bytes = CryptoJS.AES.decrypt(cipher ...

jQuery will envelop the HTML elements in an inconsequential div

Imagine a website that is visually complex, with various styles and images positioned in different ways. What if we wanted to add a small overlay icon above each image? At first, the solution might seem simple - just use absolute positioning for a span el ...

Is it possible to share data from one controller in a JS file to another JS file? (using AngularJS)

I need to create a table in JavaScript. In one of my JS files, I have a controller with JSON data containing properties like width, height, color, etc. In another JS file, I am building the actual table structure. Here is an example of my AngularJS file: ...

Initially Missing Child Props in Parent Component

I am currently working on an application that utilizes a nutrition API to fetch information such as calories and more. One of the key features I am developing is the ability for users to set their daily calorie target along with the percentage breakdown fo ...

Using Angular to store checkbox values in an array

I'm currently developing a feature that involves generating checkboxes for each input based on the number of passengers. My goal is to capture and associate the value of each checkbox with the corresponding input. Ultimately, I aim to store these valu ...

What is the best way to retrieve the value of a selected radio button with YUI?

Here is the structure of my radio buttons... <div id="test"> <input name="test1" value="a" type="radio"> <input name="test1" value="b" type="radio"> <input name="test1" value="c" type="radio"> </div> What is the best w ...

Executing a Python script asynchronously from a Node.js environment

I am currently managing a node.js program that handles approximately 50 different python script instances. My goal is to implement a throttling mechanism where only 4 processes can run in parallel at any given time. Initially, I attempted to create a simp ...

Creating a Full-Width Form Using Bootstrap

I need help aligning 3 form boxes side by side with a total width of 100%. I want to be able to specify the width of each box, or have the last two boxes set to their default size while the first box fills up the remaining space. Here is my current code: ...

Challenge with Alignment of Fields in Bootstrap's Horizontal Form

I am working on an Angular project with a horizontal form using Bootstrap. I have encountered a slight alignment issue with one of the date fields. The IsMessedUp datepicker input field is not aligned properly, positioned slightly to the left compared to t ...

When trying to install express using Node.js npm, an error message 'CERT_UNTRUSTED' is preventing the installation of express

My goal is to set up express on Raspberry Pi for the purpose of using it in a REST API. However, I keep encountering an issue with SSL certification when trying to install it through npm. Despite attempting various solutions found on different forums, I ha ...

Error: Attempting to access the 'url' property of an undefined variable, despite specifically checking for its undefined status

Within my React application, I am utilizing the following state: const [functions, setFunctions] = useState([{}]); I have created a test to check if a specific property is undefined: if (typeof functions[functionCount].url !== "undefined") { ...

What is the reason for Cloud Firestore's realtime database fetching all items?

Currently, I am developing a project with vuejs and I am facing an issue where I need to fetch only the last created item from Firestore's realtime database. However, when I execute the following code snippet, it retrieves all items from the database ...