Non-sticky hamburger menu is not fixed in place

Having trouble with the hamburger menu staying sticky? The hamburger icon remains in the corner as you scroll down, but the menu stays fixed at the top of the page, making it hard to access. You tried tweaking the code you found on Codepen, but couldn't get it to work.

If you want to check out the original code on Codepen, here is the link: https://codepen.io/CopyPasteLtd/pen/BaxQeGw

Take a look at the code snippet below.

const toggleButton = document.querySelector('.toggle-menu');
const navBar = document.querySelector('.nav-bar');
toggleButton.addEventListener('click', () => {
  navBar.classList.toggle('toggle');
});
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@200;300;400&display=swap');

*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: inherit;
}

html {
  box-sizing: border-box;
  font-family: 'Montserrat', sans-serif;
  font-size: 10px;
}

.container {
  width: 100%;
  height: 100vh;
  /* background: linear-gradient(-45deg, #ee7752, #e73c7e);
  background-size: 400% 400%; */
  position: relative;
}

.nav-bar {
  position: absolute;
  background-color: #122;
  top: 0;
  left: -25rem;
  height: 100vh;
  width: 25rem;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: 0.5s ease-out;
}

.toggle {
  left: 0;
  box-shadow: 1px 0 15px 2px rgba(0, 0, 0, 0.4);
}

.toggle-menu {
  background-color: rgba(0, 0, 0, 0.2);
  position: fixed;
  top: 2rem;
  left: 2rem;
  width: 4rem;
  height: 3rem;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  padding: 0.2rem 0.5rem;
  border-radius: 0.5rem;
}

.line {
  width: 100%;
  height: 4px;
  border-radius: 5px;
  background-color: #fff;
  transition: 0.4s ease-out;
}

.toggle .line1 {
  background-color: #c91919;
  transform: scale(0.9) rotateZ(-45deg) translate(-6px, 4px);
}

.toggle .line2 {
  display: none;
}

.toggle .line3 {
  background-color: #c91919;
  transform: scale(0.9) rotateZ(45deg) translate(-6px, -4px);
}

.toggle .toggle-menu {
  background-color: white;
}

.nav-list {
  list-style: none;
}

.nav-list-item {
  text-align: center;
  padding: 1rem 0;
}

.nav-link {
  color: #fff;
  font-size: 2.2rem;
  text-decoration: none;
  position: relative;
  padding-bottom: 0.4rem;
}

.nav-link::before {
  position: absolute;
  content: '';
  left: 0;
  bottom: 0;
  width: 100%;
  height: 1px;
  background-color: #fff;
  transform: scaleX(0);
  transition: 0.4s ease-in-out;
  transform-origin: left;
}

.nav-link:hover::before {
  transform: scaleX(1);
}
<div class="container">
  <nav class="nav-bar">
    <div class="toggle-menu">
      <div class="line line1"></div>
      <div class="line line2"></div>
      <div class="line line3"></div>
    </div>
    <ul class="nav-list">
      <li class="nav-list-item"><a href="" class="nav-link">Home</a></li>
      <li class="nav-list-item"><a href="" class="nav-link">About</a></li>
      <li class="nav-list-item">
        <a href="" class="nav-link">Projects</a>
      </li>
      <li class="nav-list-item"><a href="" class="nav-link">Clients</a></li>
      <li class="nav-list-item">
        <a href="" class="nav-link">Contact Me</a>
      </li>
    </ul>
  </nav>
</div>

Answer №1

Modify the code from .nav-bar { position: absolute; } to .nav-bar { position: fixed; }.

Check out the code snippet provided below.

const toggleButton = document.querySelector('.toggle-menu');
const navBar = document.querySelector('.nav-bar');
toggleButton.addEventListener('click', () => {
  navBar.classList.toggle('toggle');
});
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@200;300;400&display=swap');

*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: inherit;
}

html {
  box-sizing: border-box;
  font-family: 'Montserrat', sans-serif;
  font-size: 10px;
}

.container {
  width: 100%;
  height: 100vh;
  /* background: linear-gradient(-45deg, #ee7752, #e73c7e);
  background-size: 400% 400%; */
  position: relative;
}

.nav-bar {
  position: fixed;
  background-color: #122;
  left: -25rem;
  height: 100vh;
  width: 25rem;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: 0.5s ease-out;
}

.toggle {
  left: 0;
  box-shadow: 1px 0 15px 2px rgba(0, 0, 0, 0.4);
}

.toggle-menu {
  background-color: rgba(0, 0, 0, 0.2);
  position: fixed;
  top: 2rem;
  left: 2rem;
  width: 4rem;
  height: 3rem;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  padding: 0.2rem 0.5rem;
  border-radius: 0.5rem;
}

.line {
  width: 100%;
  height: 4px;
  border-radius: 5px;
  background-color: #fff;
  transition: 0.4s ease-out;
}

.toggle .line1 {
  background-color: #c91919;
  transform: scale(0.9) rotateZ(-45deg) translate(-6px, 4px);
}

.toggle .line2 {
  display: none;
}

.toggle .line3 {
  background-color: #c91919;
  transform: scale(0.9) rotateZ(45deg) translate(-6px, -4px);
}

.toggle .toggle-menu {
  background-color: white;
}

.nav-list {
  list-style: none;
}

.nav-list-item {
  text-align: center;
  padding: 1rem 0;
}

.nav-link {
  color: #fff;
  font-size: 2.2rem;
  text-decoration: none;
  position: relative;
  padding-bottom: 0.4rem;
}

.nav-link::before {
  position: absolute;
  content: '';
  left: 0;
  bottom: 0;
  width: 100%;
  height: 1px;
  background-color: #fff;
  transform: scaleX(0);
  transition: 0.4s ease-in-out;
  transform-origin: left;
}

.nav-link:hover::before {
  transform: scaleX(1);
}
<div class="container">
  <nav class="nav-bar">
    <div class="toggle-menu">
      <div class="line line1"></div>
      <div class="line line2"></div>
      <div class="line line3"></div>
    </div>
    <ul class="nav-list">
      <li class="nav-list-item"><a href="" class="nav-link">Home</a></li>
      <li class="nav-list-item"><a href="" class="nav-link">About</a></li>
      <li class="nav-list-item">
        <a href="" class="nav-link">Projects</a>
      </li>
      <li class="nav-list-item"><a href="" class="nav-link">Clients</a></li>
      <li class="nav-list-item">
        <a href="" class="nav-link">Contact Me</a>
      </li>
    </ul>
  </nav>
</div>

<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

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 an Angular route to trigger the resolution of data from a service

I'm having trouble figuring out how to implement Angular route resolve. The documentation does not provide much help for more complex aspects of the javascript framework like this. Here is the service I am using: app.service("AuthService", ["$http", ...

Ways to troubleshoot the MudTabPanel during scrolling

I am trying to create a fixed text at the top of a scrollable MudTab. As I scroll down, I want the text to remain visible. Here is an example of what I have done: <MudTabs Position="Position.Top" PanelClass="pt-2" Style="hei ...

Alert in JavaScript if the sentence does not function properly

I need to validate the input value to ensure it only contains "o" or "x". If the input value is anything else, I want to display an alert. Currently, the alert pops up even when I enter "o" or "x". Any assistance would be greatly appreciated. Here is the c ...

Using preventDefault in the compositionend event does not make any difference

var inputNode = document.getElementById('view_1'); inputNode.addEventListener('compositionend', function(e) { console.log(e.cancelable); // true e.preventDefault(); }); <div id="view_1" class="view" contenteditable="true> &l ...

How can I store the city name instead of the city id in the database when submitting data from a dependent dropdown select in Ajax PHP?

For the past few days, I have been unable to solve a problem. The issue is as follows: I have a web form with dependent dropdown select boxes where users can choose their city, zip code, and street. However, when the form is submitted, only the city_id ( ...

What is the best choice for managing state storage and retrieval when working with React, React Router, and React Relay?

As I navigate through my app using React, React Router, and React Relay, I encounter different ways of handling data updates. React offers useState, React Router has its pushState wrapper (location.state), and Relay utilizes the updater() method. However, ...

What is the most effective way to navigate to a different webpage?

I'm facing a challenge with redirecting to another page after inserting data. I've tried various methods, but nothing seems to work. Can anyone guide me on how to achieve this successfully? let express = require("express"); let app = ex ...

Tips for crafting paragraphs that double as sieves

I'm trying to simplify and shorten this code. I have three HTML paragraphs acting as filters: "all", "positive," and "negative", referring to reviews. Each has a corresponding div for reviews: "allcont", "poscont", and "negcont". Clicking on any of th ...

Update array of hotel room reservations using Mongoose

I am currently developing a hotel room reservation system and have defined the attributes in the Room model as follows: rId: String, allocation: [{ date: Number, // 210403 == 2021-04-03 slots: [{ type: mongoo ...

Struggling to grasp the concept of PHP LZW decompression function within JSend framework

I am currently working on converting the LZW decompressor from PHP to JavaScript and I have encountered a function that is giving me some trouble. function decompressLZW(aCodes) { var sData = ''; var oDictionary = []; for (var i = 0; i &l ...

Is there a way to eliminate the scrollbar from the purecss navbar?

My website utilizes pure.css and the navbar has more elements than can fit on a small screen without scrolling. This results in an unwanted scrollbar appearing. I want the navbar to remain at the top so that it scrolls along with the content. However, when ...

What causes the image to vanish once the CSS animation is activated?

Is there a reason why the image disappears after the animation completes? :/ .coin { width: 200px; height: 200px; background-size: cover; animation: CoinflipRoll 6s steps(199); animation-delay: .5s; animation-fill-mode: forwards; ...

Tips for modifying the properties of variables within an array using JavaScript

I have an array that holds variables used to control a specific template. divisionsListToManipulate: ['showActivitiesSection', 'hideAssignActionplanDiv', 'displayProp ...

embedding a button alongside the pager in an HTML document

I am encountering an issue with the positioning of a button in my paginated table setup. The button is currently displaying below the pager instead of being aligned on the left side of the page along with the pager. https://i.stack.imgur.com/gUiB9.png To ...

Converting user's birthdate input from HTML to their age using PHP

I am facing an issue with retrieving the correct date from a date-time-picker named "dob" and passing it to PHP. When I try to post the date into the database user_age column, it only displays '2017'. However, if I manually set $dob to '10/0 ...

The jQuery selectors are not able to identify any dynamically generated HTML input elements

After successfully injecting HTML code into the DOM using Ajax, I encountered an issue where my jQuery selector was not working for a specific HTML input element. For example, when attempting to use the following jQuery code: $("input[id*='cb_Compare ...

ID of the Radio button that was most recently selected

My table contains multiple rows, each with a Radio button in the first column: <table class="table table-condensed span8" id="AllocationTable"> <thead> <tr> <th title="Entity to Allocate" style="width: 30%"> ...

Implement a FOR loop in conjunction with an AJAX request

My goal is to send an AJAX request with multiple form fields and use an array for validations. I would like to also pass these values via AJAX: Although I haven't used a for loop in JavaScript before, it looks familiar. The current approach of the l ...

Creating a copy of a div using jQuery's Clone method

I need help figuring out how to clone a div without copying its value. I've attempted various methods, but they all seem to include the value in the cloned element. This is the jQuery function I am currently using: $('#add_more').click(fu ...

Position the Material UI Box element to the bottom of its parent container

Hello there, I've come across the following layout: <Box width="100%" height="100%" p={1}> <Box my={1} display="flex" justifyContent="center"> </ ...