Dynamic menu bar accompanied by primary content

I'm facing an issue with my active navigation bar. Whenever I open the hamburger menu, the main content remains fixed and does not move along with the open navigation menu. I tried searching online for a solution but couldn't find anything helpful. Below is the code snippet I'm working with. Any help in resolving this would be greatly appreciated. What steps can I take to fix it?

Thank you.

<!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
  <header>
    <div class="logo">Logo</div>
    <div class="hamburger">
      <div class="lines"></div>
    </div>
    <nav class="nav-bar">
      <ul>
        <li><a href="#">Test</a></li>
        <li><a href="#">Test</a></li>
        <li><a href="#">Test</a></li>
        <li><a href="#">Test</a></li>
        <li><a href="#">Test</a></li>
      </ul>
    </nav>
    <script>
      hamburger = document.querySelector(".hamburger");
      hamburger.onclick = function () {
        navBar = document.querySelector(".nav-bar");
        navBar.classList.toggle("active");
      }
    </script>
  </header>
  <main>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate nihil corporis ut praesentium accusantium possimus molestiae reprehenderit quam nostrum distinctio repudiandae iure aliquam repellat unde recusandae quas ipsam. Dicta animi.</p>
  </main>
</body>
</html>
* {
  margin: 0;
  padding: 0;
  list-style: none;
  text-decoration: none;
  font-family: "Roboto", sans-serif;
  box-sizing: border-box;
  -webkit-font-smoothing: antialiased;
}

body {
  background: #fff;
  font-size: 100%;
}
/* NAVIGATION */
header {
  width: 100%;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.25em 6.25em;
  border-bottom: 1px solid #000;
}
.logo {
  width: 50%;
}
.hamburger {
  display: none;
}
.nav-bar ul {
  display: flex;
}
.nav-bar ul li a {
  display: block;
  color: #000;
  font-size: 1.375em;
  padding: 0.4545454545454545em 0.6818181818181818em;
  border-radius: 50px;
  transition: all 0.5s ease-in-out;
  margin: 0 0.2272727272727273em;
}
.nav-bar ul li a:hover {
  color: #fff;
  background: #ffb038;
}

.fa-solid {
  margin-right: 0.6818181818181818em;
  vertical-align: middle;
}
@media only screen and (max-width: 1770px) {
  .nav-bar ul li a i {
    display: block;
  }
}
@media only screen and (max-width: 1400px) {
  header {
    padding: 0px 1.5625em;
  }
  .hamburger {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 80px;
    height: 80px;
    cursor: pointer;
    transition: all 0.5s ease-in-out;
  }
  .lines {
    width: 50px;
    height: 6px;
    background-color: #ffb038;
    border-radius: 5px;
    transition: all 0.5s ease-in-out;
  }
  .lines::before,
  .lines::after {
    content: "";
    position: absolute;
    width: 50px;
    height: 6px;
    background-color: #ffb038;
    border-radius: 5px;
    transition: all 0.5s ease-in-out;
  }
  .lines::before {
    transform: translateY(-16px);
  }
  .lines::after {
    transform: translateY(16px);
  }
  .hamburger.open .lines {
    transform: translateX(-50px);
    background: transparent;
  }
  .hamburger.open .lines::before {
    transform: rotate(45deg) translate(35px, -35px);
  }
  .hamburger.open .lines::after {
    transform: rotate(-45deg) translate(35px, 35px);
  }
  .nav-bar {
    height: 0;
    position: absolute;
    top: 65px;
    left: 0;
    right: 0;
    width: 100vw;
    background: #ffb038;
    transition: 0.5s;
    overflow: hidden;
  }
  .nav-bar.active {
    height: 28.125em;
    transition: all 0.5s ease-in-out;
    margin-top: 15px;
  }
  .nav-bar ul {
    display: block;
    width: fit-content;
    margin: 5em auto 0 auto;
    text-align: center;
    transition: all 0.5s ease-in-out;
    opacity: 0;
  }
  .nav-bar.active ul {
    opacity: 1;
    display: block;
  }
  .nav-bar ul li a {
    margin-bottom: 0.75em;
  }
  .nav-bar ul li a:hover {
    color: #000;
    background: white;
  }
}

Answer №1

When using a media query, applying position: absolute to .nav-bar removes it from the flow, preventing it from affecting other elements like main.

To maintain the flow, wrap .logo and .hamburger in a container while keeping .nav-bar as their sibling:

<!--Before:-->
<header>
  <div class="logo">Logo</div>
  <div class="hamburger">
    <div class="lines"></div>
  </div>
  <nav class="nav-bar"><!--...--></nav>
</header>

<!--After:-->
<header>
  <div><!--The wrapper-->
    <div class="logo">Logo</div>
    <div class="hamburger">
      <div class="lines"></div>
    </div>
  </div>
  <nav class="nav-bar"><!--...--></nav>
</header>

The new wrapper will reconfigure the header's CSS. Here's an example of how it might look:

const hamburger = document.querySelector(".hamburger");
const navBar = document.querySelector(".nav-bar");

hamburger.addEventListener("click", () => navBar.classList.toggle("active"));
.nav-bar {
  width: 100%;
  background: #ffb038;
  overflow: hidden;
}
.nav-bar:not(.active) {
  height: 0;
}

.nav-bar a {
  padding: 0.5em 0.7em;
  width: 100%;
  height: 48px;
  display: inline-block;
  box-sizing: border-box;
  font-size: 1.375em;
  text-align: center;
}

/*Wrapper (styled as before's header) - Mobile style*/
#wrapper {
  padding: 0px 1.5625em;
  border-bottom: 1px solid black;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/*Default style*/
* {
  margin: 0;
  padding: 0;
  list-style-type: none;
  text-decoration: none;
  font-family: sans-serif;
  color: black;
}
.hamburger {
  width: 80px;
  height: 80px;
  background-color: orange;
  cursor: pointer;
}
<body>
  <header>
    <div id="wrapper"><!--New wrapper-->
      <div class="logo">Logo</div>
      <div class="hamburger"></div>
    </div>
    <nav class="nav-bar">
      <ul>
        <li><a href="#">Link 1</a></li>
        <li><a href="#">Link 2</a></li>
        <li><a href="#">Link 3</a></li>
      </ul>
    </nav>
  </header>
  <main>
    Lorem ipsum set amet.
  </main>
</body>


To smoothly transition the height property, consider transitioning max-height using custom properties:

const hamburger = document.querySelector(".hamburger");
const navBar = document.querySelector(".nav-bar");

hamburger.addEventListener("click", () => navBar.classList.toggle("active"));

// Example of setting `--links` property:
navBar.style.setProperty("--links", navBar.querySelectorAll("a").length);
.nav-bar {
  --links: 3; /*Example of setting `--links` property*/
  --link-height: 48px;
  max-height: calc(var(--links, 0) * var(--link-height));
  transition: max-height .25s ease;
}
.nav-bar:not(.active) {
  max-height: 0;
}
.nav-bar a {
  height: var(--link-height);
}

/*Remove `height` declarations from .nav-bar and related; otherwise same as before:*/
.nav-bar {
  width: 100%;
  background: #ffb038;
  overflow: hidden;
}

.nav-bar a {
  padding: 0.5em 0.7em;
  width: 100%;
  display: inline-block;
  box-sizing: border-box;
  font-size: 1.375em;
  text-align: center;
}

#wrapper {
  padding: 0px 1.5625em;
  border-bottom: 1px solid black;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

* {
  margin: 0;
  padding: 0;
  list-style-type: none;
  text-decoration: none;
  font-family: sans-serif;
  color: black;
}
.hamburger {
  width: 80px;
  height: 80px;
  background-color: orange;
  cursor: pointer;
}
<body>
  <header>
    <div id="wrapper">
      <div class="logo">Logo</div>
      <div class="hamburger"></div>
    </div>
    <nav class="nav-bar" style="--links:3"><!--Example of setting `--links` property-->
      <ul>
        <li><a href="#">Link 1</a></li>
        <li><a href="#">Link 2</a></li>
        <li><a href="#">Link 3</a></li>
      </ul>
    </nav>
  </header>
  <main>
    Lorem ipsum set amet.
  </main>
</body>

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

Navigation isn't aligning correctly on the right side

I am having trouble with my navigation menu not aligning properly to the right. <div class="logo"> <h2><i class="icon-reorder"></i> Frosty</h2> </div> <div class="nav"> <a href="#">Home</a> </div& ...

Unable to access this context in Firefox debugger after promise is returned

I'm curious as to why the 'this' object is not showing up in the debugger in Firefox, but it does appear in Chrome's debugger. When I try to access 'this.myProperty', it shows as undefined. This is the code from my TypeScript ...

Having issues with implementing the Bootstrap form-check-inline feature

I have been attempting to utilize the form-check-inline feature from Bootstrap Forms without success, as it seems to be stacking checkboxes vertically instead of inline: This is the code snippet I am working with: <h1>Create Patient</h1> < ...

Conceal user input field in React using a hook

I am looking for assistance with a form that has 4 input fields: username, password, email, and mobile. My goal is for the email field to disappear if an '@' symbol is typed in the username field, and for the mobile field to disappear if any digi ...

What is the best way to connect a nested Vuex object to a Vue.js property within the Tiptap text editor?

I'm attempting to connect the nested Vuex state object to the editor property of the tiptap's editor-content component. The structure of the state is as follows: <template> <table :style="{ backgroundColor: element.options.ba ...

What is the best way to link one element to another element?

Apologies for the mismatched title, but here's my issue: I need to create hidden checkboxes for each Polymer's paper-button div in jsp/style related problems. <%int i; %> <form ACTION="Computation.jsp"> <% for(i=0; i<n; ...

Struggling to locate the src/site/globals/site.variables file to customize the font-family in Semantic UI React. Is there an alternative method to achieve this?

I've been attempting to modify the overall font-family for my application, but so far, I haven't had any luck. Since I'm working with Semantic-UI-React, I initially thought that could be the issue. However, when I tried to locate src/site/g ...

Tips for successfully including a forward slash in a URL query string

My query involves passing a URL in the following format: URL = canada/ontario/shop6 However, when I access this parameter from the query string, it only displays "canada" and discards the rest of the data after the first forward slash. Is there a way to ...

Understanding how to retrieve the value count by comparing strings in JavaScript

In my array object, I am comparing each string and incrementing the value if one letter does not match. If three characters match with the string, then I increase the count value; otherwise, it remains 0. var obj = ["race", "sack", &qu ...

Setting the thumbnail image for an HTML5 video: A step-by-step guide

Is it possible to set a thumbnail image for an HTML5 video? I would like to display an image before the video starts playing. Here is my current code: <video width="470" height="255" controls> <source src="video.mp4" type="video/mp4"> ...

Arrange the keys of a map in ascending order, prioritizing special characters and their precedence

JavaScript ES6 Map Example: const map = new Map(); map.set('first', ['1', '2']); map.set('second', ['abc', 'def']); map.set('_third', []); map.set(')(*', []); map.set('he ...

Ensuring Input Integrity: Utilizing HTML and Javascript to Block Unfilled Form Submissions

Currently, I am working on developing a registration page using HTML and JavaScript. Although I have added the 'required' tag in HTML to prevent users from submitting empty input fields, I noticed that users can bypass this restriction by simply ...

Securing User Profiles in Firebase

I am currently working on a coding issue that involves the security of user profiles. While it doesn't involve sensitive information like payment details or personal data, it does pertain to the ownership of a profile. Currently, I store users' ...

Obtaining Mouse Click X, Y, and Z Coordinates with Three.js

I am currently utilizing version 68 of three.js. My objective is to gather the X, Y, and Z coordinates upon clicking somewhere on the canvas. Despite following the steps outlined in this guide, I am encountering a consistent Z value of 0: Mouse / Canvas X ...

Implementing a hamburger menu across various webpages

I recently followed a tutorial on adding a hamburger menu from this YouTube video. The menu works perfectly on the INDEX.html page, but when I try to add the same code to other pages like "contact" or "about", none of the menu features seem to work. I rea ...

Creating a personalized message for a failed "Get" request using the HTTPS native module in combination with

Currently, I have implemented an Express application that includes an HTTP GET request to an external API. It functions properly when there are no errors; however, I am looking to customize the response sent to the client-side in case of failures: const ht ...

Rotating arrows enhance the functionality of the accordion menu

I have successfully implemented a basic accordion with rotating arrows on click. Everything is working smoothly except for one issue: When I collapse one item and then try to collapse another, the previous arrow does not return to its default state. Is ...

New data field is created with AngularFire2 update instead of updating existing field

I am facing an issue with updating a Firestore model in Angular 6. The model consists of a profile name and a list of hashtags. The "name" is stored as the value of a document field, while the "hashtags" are stored as keys in an object. However, every time ...

Warning: Knex was unable to acquire a connection due to a timeout, resulting in an UnhandledPromiseRejectionWarning

I'm having trouble connecting my Telegram bot to the database using knex. I am working with MySQL, and I have already created the database and tables which are visible on the /phpMyAdmin page. The credentials used for accessing the database in my code ...

Tips on ensuring Angular calls you back once the view is ready

My issue arises when I update a dropdown list on one of my pages and need to trigger a refresh method on this dropdown upon updating the items. Unfortunately, I am unsure how to capture an event for this specific scenario. It seems like enlisting Angular ...