The placement of the sliding navigation menu is consistently positioned beneath the header

I created a jQuery menu that's also available on JSfiddle here:

$(document).ready(function () {
    $(".navigation_button").on('click', function () {
    
    var $panel = $(this).next('.panel');
    if ($panel.is(':visible')) {
      $panel.add($panel.find('.panel')).slideUp(500).removeClass('active');
    } else {
      $panel.slideDown(500).addClass('active');
    }
    
    });
});
body {
  margin: 0;
}

.header {
  width: 80%;
  height: 20%;
  margin-left: 10%;
  display: flex;
  justify-content: space-between;
  position: fixed;
  top: 0;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: yellow;
}

.image {
  width: 30%;
  height: 100%;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: green;
}


/* Navigation Mobile */

.navigation {
  width: 70%;
  height: 100%;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: aqua;
}

.navigation>nav {

}


.navigation>nav>ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.navigation>nav>ul li a {
  display: block;
  text-decoration: none;
  color: black;
}


/* Navigation Button */

.navigation_button {
 width: 20%;
 height: 60%;
 float: right;
 cursor: pointer;
 
 box-sizing: border-box;
 border-style: solid;
 border-width: 1px;
 background-color: fuchsia;
}

.bar1, .bar2, .bar3 {
 width: 100%;
 height: 20%;
 margin: 4% 0;
 background-color: #333; 
}



/* Menu Content */

.menu_box {
 width: 100%;
 float: right;
 line-height: 2.0;
 
 box-sizing: border-box;
 border-style: solid;
 border-width: 1px;
}

.panel{ 
 width: 100%;
 padding-left: 0%;
 cursor: pointer;
 font-weight: bold;
 overflow: hidden;
 display:none;
}

.button_menu {
 padding-left: 1%;
 background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="header">

  <div class="image">
  Image
  </div>
  
  <div class="navigation">
  
    <div class="navigation_button">
    <div class="bar1"></div>
    <div class="bar2"></div>
    <div class="bar3"></div>
    </div>

   <nav class="panel">

    <ul class="menu_box">
      <li class="button_menu"> 1.0 Menu </li>
      <li class="button_menu"> 2.0 Menu </li>
      <li class="button_menu"> 3.0 Menu </li>
    </ul>

   </nav>

  </div>
  
</div>

The menu functions perfectly when the .navigation_button is the same size as the .navigation.

However, I adjusted the height of the .navigation_button to height: 60%; and now clicking the button causes the .panel to appear below the .navigation_button instead of beneath the .header.


To address this issue, I attempted adding position:absolute; to the .panel while giving it the same height as the .header, but this resulted in the panel sliding in from the top of the page.

Another solution was closing the .header <div> after the .navigation_button, however, this caused the animation to cease functioning.


What adjustments must be made in the code to ensure that the .panel consistently appears directly below the .header, regardless of where the .navigation_button is positioned within the .header?

Answer №1

To position the .panel below the .header, you can set its position as absolute and align it 0 from the left and 100% from the top of its first non-static parent, which in this case is the .header (with a position value of fixed). This will ensure that the .panel appears below the .header. However, in the provided example, the .panel does not expand to cover the width of the blue .navigation element. If you want this behavior, then set the position of the .navigation element as relative.

Check out the demonstration below:

$(document).ready(function () {
    $(".navigation_button").on('click', function () {
    
    var $panel = $(this).next('.panel');
    if ($panel.is(':visible')) {
      $panel.add($panel.find('.panel')).slideUp(500).removeClass('active');
    } else {
      $panel.slideDown(500).addClass('active');
    }
    
    });
});
body {
  margin: 0;
}

.header {
  width: 80%;
  height: 20%;
  margin-left: 10%;
  display: flex;
  justify-content: space-between;
  position: fixed;
  top: 0;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: yellow;
}

.image {
  width: 30%;
  height: 100%;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: green;
}


/* Navigation Mobile */

.navigation {
  position: relative; /* remove this if you want the panel to stretch further */
  width: 70%;
  height: 100%;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: aqua;
}

.navigation>nav {

}


.navigation>nav>ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.navigation>nav>ul li a {
  display: block;
  text-decoration: none;
  color: black;
}


/* Navigation Button */

.navigation_button {
 width: 20%;
 height: 60%;
 float: right;
 cursor: pointer;
 
 box-sizing: border-box;
 border-style: solid;
 border-width: 1px;
 background-color: fuchsia;
}

.bar1, .bar2, .bar3 {
 width: 100%;
 height: 20%;
 margin: 4% 0;
 background-color: #333; 
}



/* Menu Content */

.menu_box {
 width: 100%;
 float: right;
 line-height: 2.0;
 
 box-sizing: border-box;
 border-style: solid;
 border-width: 1px;
}

.panel{ 
 /* changes from here...*/
 position: absolute; 
 top: 100%;
 left: 0;
 /*...here*/
 
 width: 100%;
 padding-left: 0%;
 cursor: pointer;
 font-weight: bold;
 overflow: hidden;
 display:none;
}

.button_menu {
 padding-left: 1%;
 background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="header">

  <div class="image">
  Image
  </div>
  
  <div class="navigation">
  
    <div class="navigation_button">
    <div class="bar1"></div>
    <div class="bar2"></div>
    <div class="bar3"></div>
    </div>

   <nav class="panel">

    <ul class="menu_box">
      <li class="button_menu"> 1.0 Menu </li>
      <li class="button_menu"> 2.0 Menu </li>
      <li class="button_menu"> 3.0 Menu </li>
    </ul>

   </nav>

  </div>
  
</div>

Answer №2

Insert the following snippet into '.panel'

position: fixed;
top:50%;
left: 0;

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 native javascript modal fails to appear

I'm attempting to implement the functionality from this Codepen demo into my project. I've copied over the HTML, CSS, and JavaScript code: <!DOCTYPE HTML> <html> <head> <script> var dialog = docume ...

Troubleshooting issues with rowspan in a Datatable

I am currently utilizing jQuery DataTables to display my grid data and implementing the rowspan concept with the rowsGroup option. Initially, it works well by spanning some rows and looking visually pleasing, but eventually, it starts failing. Here are so ...

Obtain the image format from an HTML page using the Jsoup library on Android

<div class="_jjzlb" style="padding-bottom: 55.2778%;"><img alt="AT Dam party.. #nashik #big #dam" class="_icyx7" id="pImage_11" src="https://instagram.fbom1-2.fna.fbcdn.net/t51.2885-15/e35/17438694_254543168340407_6435023364997251072_n.jpg" style ...

jQuery fails to retrieve JSONP data from an external source

I want to determine if a stream is live on justin.tv. They offer an easy-to-use JSON API, where by querying http://api.justin.tv/api/stream/list.json?channel=channel_name it will provide specific JSON information if the stream is indeed live. var url = ...

Designing a webpage layout with DIV elements that span across multiple rows

I am attempting to create a layout that features two columns, with the right column spanning two rows. I am looking to accomplish this using only DIV tags: +-----------+-----------+ + + + + + + +-----------+ ...

Ways to verify if a div is empty following an ajax request

When trying to load a php page into a div, I encountered an issue where the content of the div appeared empty. Despite attempting various methods to check if the div contained any html content after loading, I was unable to find a solution. ...

"Troubleshooting a JSON structure containing a complex array of objects with multiple layers

I've structured a complex array with JSON objects to allow users to customize background images and create unique characters. Below is the main code snippet: var newHead; var newBody; var newArm; var masterList = [ { {"creatureName":"Snowman ...

What is the most effective method for establishing a notification system?

My PHP-based CMS includes internal messaging functionality. While currently I can receive notifications for new messages upon page refresh, I am looking to implement real-time notifications similar to those on Facebook. What would be the most efficient ap ...

What methods can I use to ensure my HTML/CSS code is compatible with all browsers? It functions properly on Chrome but encounters issues on Safari, Edge, and other

My hosting for the website is with bluehost, and interestingly when viewed on Chrome, everything appears perfect. However, upon switching to Safari, I noticed that the background of the about me section is not black as intended, and the footer seems to be ...

Resetting ajax success message when modal is closed

Currently utilizing w3.css and its modal feature, I am encountering an issue with clearing the message displayed after a successful ajax call once the window is closed. Even though I have implemented code to clear the message upon closing the window (by cl ...

Is there a way for me to adjust the image dimensions so that it doesn't surpass the width of its parent container?

When working with images, it can be tricky to set the original width while also ensuring it fits within a parent container. For example, if the parent container has a width of 1000px, you may want the image to have a max-width of 100%, but not exceed 1000p ...

Combining the power of Rails with the flexibility of Jquery

I was following this tutorial with a slight modification of adding gem 'jquery-rails' to my gemfile. However, I encountered an issue where the AJAX functionality wasn't working when clicking on "new post" and submitting the form to create a ...

Unique styling implementation for element situated underneath Angular 6 router-outlet

I'm currently working on implementing router transitions in my Angular application, and I've encountered an unusual problem. The styling for the router-outlet element is being applied to the element that comes after it in the DOM hierarchy. Here ...

Rotating through classes with JQuery click event

I'm attempting to create a toggle effect for list items where clicking on an item will change its color to green, then red, and then back to its original state. Some list items may already have either the green or red class applied. However, my curren ...

Whenever I press a button, the data fails to be sent to the changePassword.php file using ajax in jquery

I tried to use this form to send data to changePassword.php, but unfortunately, the data did not get sent. <form class="form-horizontal" method="post"> <div class="form-group"> <label for="inputName" class="col-sm-2 control-labe ...

Tips on using CSS to hide elements on a webpage with display:none

<div class="span9"> <span class="disabled">&lt;&lt; previous</span><span class="current numbers">1</span> <span class="numbers"><a href="/index/page:2">2</a></span> <span class="num ...

evt.target consistently returns the initial input within the list of inputs

My React file uploader allows users to attach multiple file attachments. Each time a user clicks on an input, I retrieve the data-index to identify the input position. renderFileUploader() { let file_attachment = this.state.file_attachment.map(fun ...

What could be causing the extra space at the top of my div element?

My goal is to accomplish the following: However, there appears to be an unnecessary gap above the Social icons div. You can view the live page at This is my current markup:- <header> <img src="images/home-layout_02.jpg" alt="Salem Al Hajri Logo ...

Styling tables within HTML emails for Gmail and then utilizing PHPMailer to send the emails

I've been racking my brain over this for hours with no luck! Objective: Implementing inline styles for table, td, th, p elements in an HTML email intended for Gmail using PHPMailer. Challenge: Inline styles not being rendered HTML Snippet: <sec ...

What is the best way to choose a single item from an array using jQuery?

Within an array, I have IDs and descriptions like: var tablica = [{"3":"asdasd asd"},{"19":"asddas fff"},{"111111":"adas asf asdff ff"},{"4":"re"},{"5":"asdasd"},{"6":"we"},{"7":"asdasdgg"},{"9":"asdasdasd"},{"16":"sdads"},{"10":"asdgg"},{"11":"ggg"}]; ...