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

Embedding a CSS file into a PHP class

I've encountered a little issue that I can't seem to solve on my own. I've put together a "webengine" using multiple classes. The primary class responsible for obtaining a theme's internals is called "Logic". This Logic class contain ...

Angular application's HTML Canvas unexpectedly changes to a black color when I begin drawing

Currently, I am developing an Angular application in which users can draw text fields on PDF pages. To achieve this functionality, I have integrated the ng2-pdf-viewer library and created a PDF page component that incorporates an HTML canvas element. Howe ...

To determine the class of an element and reveal its parent if the class is present

Typically, the parent element remains hidden by default. However, upon clicking a specific element, its child might gain a class called "selected". How can I check for this class and then display the entire list if it is present? <ul style="display: ...

Why are the random colors blending into the image?

After watching a tutorial on the YouTube channel Online Tutorials titled "Change Image Color Vanilla JavaScript," I am confused as to why some random colors from the click event are overlapping the image instead of just changing the color of the cat's ...

Creating a connection between my .html and .ejs files: A step-by-step guide

I have successfully created a pair of HTML files - index.html & contact.html. Within these files, I have implemented a navigation bar that allows for seamless transition between them. Recently, I delved into the realm of retrieving APIs and crafted an app ...

One way to trigger the same modal to open when clicking on a shared class using jQuery

I need some assistance with opening a model upon clicking the same link. Currently, when I click on the link, only the backdrop briefly appears and then the page reloads. Any help would be greatly appreciated. Thank you in advance. Below is the code snipp ...

How do I navigate to the homepage in React?

I am facing an issue with my routes. When I try to access a specific URL like http://localhost:3000/examp1, I want to redirect back to the HomePage. However, whenever I type in something like http://localhost:3000/***, I reach the page but nothing is dis ...

Certain content appears oversized on Android devices, yet appears appropriately sized on desktop computers

I am experiencing an issue with a webpage where the text appears normal on desktop but is too big on Android devices. Below is the code snippet causing the problem: <!DOCTYPE html> <html> <head> <title>ra ...

When viewed on mobile browsers, the side-by-side divs in a tabbed layout appear to be

Looking for some help with responsive design for the "PORTFOLIO ATTRIBUTES" tab on my website. On desktop, the content displays fine, but on mobile it overlaps and cuts off. Here's the link to the page in question: . Any suggestions on how to maintai ...

Repeated instances in a loop are strictly prohibited

I am facing an issue with AngularJS where I am unable to display JSON data in my HTML. Below is the code I am using: $('.loading').show(); $scope.posts=[]; $http.get("https://api.basebear.com/json/tables/20a3fb1d-42c7-45cb-9440-2c6d5d10403d/r ...

Retrieve SQL data and store it in a JavaScript variable

Need assistance with fetching data from SQL and storing it in a JavaScript variable. I have already connected PHPMyAdmin to my website. I am attempting to retrieve two variables (date) from my SQL table. JAVASCRIPT: var countdown_48 = new Date; countdow ...

Avoiding the opening of a select menu

Is there a way to prevent the dropdown menu from appearing when a select element is clicked in a form? I have attempted two methods but they did not work: $('select').click (function (e) { console.log (e); return false; }); and $(&apo ...

Having difficulty positioning breadcrumb items to float on the right side

As I create my Vue.js and Bootstrap application, I am working on a breadcrumb feature that presents some challenges. Specifically, I am trying to align the icons to the right while ensuring that the text "Files" remains floated to the left. <script s ...

Two separate entities link to a shared occurrence

Two namespaces are at play here: '/' and /notification If I trigger an event in the '/' namespace, how can I listen to the same event in the '/notification' namespace? It would be helpful if someone could provide an explanati ...

What is the best way to transfer variables to a different page through a pop-up window?

I'm working with a function that converts the Id of the clicked element into a variable, then opens a new window with a different page. How can I access or utilize this variable on the newly opened page? var idToWrite = []; $(function(){ $(".szl ...

End the child process.execution after a specific amount of time

I have been searching for information on child process waiting time or response waiting time, but I haven't found anything useful. I'm in need of something like that, so I hope someone can assist me. When I look at the code below, I am printing ...

What is the best way to print a canvas element once it has been modified?

My goal is to include a "Print Content" button on a webpage that will print a canvas element displaying workout metrics. However, the canvas content, which consists of a visual graph of workout data, changes based on the selected workout (bench, squat, etc ...

Style selector for dynamic drop-down menus

import React, { Component } from "react"; export default class FontChanger extends Component{ constructor(props){ super(props); this.state ={ selectedFont: "", currentFont: "", }; this.handleFon ...

When sending a request from Vue.js using Axios to PHP, the issue arises that the .value property is

There is a chat box with bb smileys underneath. Clicking on the smileys adds them to the text in the input box. However, when using axios to post, the array is empty. Manually entering the smiley bbcode works fine. <input id="txtName" @keyup.enter="ad ...

The styled-components seem to be having trouble injecting the necessary CSS to handle all the various props

I am curious to understand the potential reasons for styled-components not injecting all the necessary CSS into a page's header. In an existing project, I have defined a simple button like this: const Button = styled.button` background-color: ...