Clicking a button causes the entire HTML body to shift

My current webpage setup is quite simple, featuring a drop-down menu that appears upon clicking a button. However, when the button is clicked, the drop-down menu suddenly shifts the entire contents of the page. I'm unsure of what's causing this issue and would greatly appreciate any assistance. Thank you in advance!

Please check out the live link:

This is my HTML code:

<!DOCTYPE html>
<html>
<head>
    <title>ManaBadi</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="./styles.css">
    <script src="./jquery_min.js"></script>
    <script src="./button_dropdown_menu.js"></script>
    <script src="./nav_hide_show.js"></script>
</head>
<body>
<div id="main_content_container">
    <header>
    <span>ManaBadi</span>
    <img src="./menu100.png" id="hamburger_menu_icon" alt="menu icon">
        <ul id="nav_ul">
            <li><a href="#">Home</a></li>
           ...
                "<li class="region_locations"><a href="#">Kingdom of Saudi Arabia</a></li>
            </ul>
        </li>
        
...

And here's my CSS:

/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (pub.*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}

/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}

...

Answer №1

Your issue lies in the positioning of your menu. It currently exists inline, causing it to push content down when expanded. To resolve this, consider changing its position from 'relative' to 'absolute'. This may require additional styling adjustments, such as adding a background color. Alternatively, you can use 'position: fixed;' to place the menu relative to the viewport.

For example, you can toggle between 'relative' and 'absolute' positions with buttons and see the impact on the layout:

$("#switch").click(function(){ $("#menu").toggleClass("absolute"); });
$("#show").click(function(){ $("#menu").toggleClass("show"); });
body #menu {
  height: 0;
  -webkit-transition: height 500ms;
  transition: height 500ms;
  background: blue;
  overflow: hidden;
  position: relative;
}
body #menu.absolute {
  position: absolute;
  top: 0;
  left: 0;
}
body #menu.show {
  height: 20%;
}

Note that elements positioned 'absolute' will be relative to the closest parent with positioning set. If you want the menu to be positioned according to the viewport, consider using 'fixed'. Keep in mind that 'fixed' positioning is minimally affected by surrounding elements unless specific coordinates like 'top' or 'left' are not defined.

Answer №2

To prevent the dropdown menu from impacting the layout of the page, include position:absolute in the CSS styling for .dropdown_menu.

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

What is the best way to align a div in the middle of an input-group div?

When dealing with a nested div, where the parent div is styled with an input-group class, my goal is to have the nested div centered within the available line or on the next free line space. This is the desired layout I want to achieve regardless of the n ...

What is the mechanism behind JQuery Ajax?

Currently, I am attempting to utilize JQuery Ajax to send data to a Generic Handler for calculation and result retrieval. Within my JQuery script, the Ajax request is contained within a for loop. The structure of the code resembles the following: function ...

"An error was thrown due to an illegal invocation while utilizing $.post, although $.ajax seems to be

I've been experimenting with FormData to send data to a different URL, but the code below doesn't seem to be working $("form").on("submit", function(e){ e.preventDefault(); var formData = new FormData(this ...

display/hide a div within a separate container

Greetings, I am seeking assistance in understanding why the second tab button is not functioning properly. I am attempting to create a layout with a wrapper div, a left side div containing label buttons, and a right side div for displaying content. When I ...

Arranging five divs within one main div horizontally with equal margins between them

I am currently working on a website system where I need to place 5 divs within 1 main div horizontally. However, I want the spacing between the 5 divs to be equal and fixed within the main div. Below is how my current page looks: https://i.stack.imgur.com ...

Exploring Techniques for Streamlining HTML/CSS Editing in Browser

Is there a way to automatically edit specific lines on websites right after they load? Specifically, I want to change <div class="1"> to <div class="1" style="display:none">, and <div class="2" style ...

The body can only stretch up to a certain percentage of its width, not a full 100%

Recently, I've been encountering an issue with my webpage where the body does not stretch 100% the width of the screen. I've tried numerous CSS rules to fix it, but nothing seems to be working. When you view the page in a browser, it appears to c ...

Obtain Attribute Value Using JQuery

I am trying to extract the value from this attribute element, but I am unsure of the correct way to do it. Can someone help me out? Here is the code snippet: <select name="Hello/1.0/TestWorld" size="1" disabled="disabled" ...

PHP function json_encode() is causing data to become null

Here is the code snippet I am working with: case 'resource_list': if(file_exists('content.php')){ include('../ajax/content.php'); } else { die('does not exist'); } ...

Having trouble getting Django to display images using jQuery? Uncaught Reference Error may be preventing it

Currently, I am experimenting with implementing a jquery example on my django-based website. As I am still in the learning phase, I kindly ask for your patience. This is a snippet of what my code looks like at this point: {% extends "base.html" %} {% loa ...

Introduce a transition effect to the MUI Chip component to enhance user experience when the Delete Icon

When the delete icon appears, it is recommended that the overall width of the chip increases smoothly rather than instantly. Here is the current code structure: CSS: customChip: { "& svg": { position: "relative", display: &quo ...

Having a jammed cursor with Knockout + draggable/droppable extensions, anyone else experiencing this issue?

Trying to alter the cursor of a dragged element using the conventional method: $(element).draggable({ revert: "invalid", revertDuration: 0, cursor: "move" }); but for some reason, when an element is dropped ...

Troubleshooting the problem of fast rotation and scrolling of text in the middle but slow movement at the end with

Currently, I am utilizing the jquery animate function for animating text while also aiming to rotate the text if a specific degree is passed. In order to achieve this, I have implemented the following code snippet: var leftCss = "-"+$('#mydiv'). ...

Guide on implementing jQuery WYSIWYG HTML editor on an ASP.net webpage

Currently, I am developing a blogging application and aiming to incorporate the jQuery HTML editor into my ASP.net application. While I am aware of the ASP.net AJAX toolkit editor as an alternative, it is not as lightweight as the jQuery editor. Therefore, ...

ReactJS not responding to Bootstrap toggler button

My Bootstrap toggler button is not working in ReactJS. I did not include the jQuery scripts because I imported Bootstrap at the top. The button should only appear and function on small devices. import React from 'react'; import 'bootstrap/d ...

When data is returned empty, DataTable does not get initialized

Our DataTable setup is dynamic, initializing even when the data is empty and waiting to be filled in once a specific option is selected. Recently, we made updates to jQuery version 3.7 and DataTable v2.0.6. Now, the table seems to be stuck in a loading pr ...

JQuery updates the sum within the input element, but it does not affect the sum inside the span elements when using the .val and .html methods

My goal with this code is to calculate the total value of my checkboxes. The code works when the result is displayed in an input element. <input type="text" id="total1" class="container-input noborder"> However, I want ...

Trigger the callback function once the datatables DOM element has finished loading entirely

Hello there! I have a question regarding datatables. Is there any callback function available that is triggered after the datatables DOM element has finished loading? I am aware of the callbacks fnInitComplete, but they do not serve my purpose. Specificall ...

"Exploring the Possibilities with WordPress and Waypoint Js

After exploring various resources and tutorials online, I am struggling to implement Waypoints with WordPress. Despite placing the necessary files like waypoints.min.js and waypoints.js in the designated download folder within the js directory of my templa ...

Utilize multiple background images in CSS with a set of three unique images

I have inserted three images into the background of my website's body. Two of them are displaying correctly (one with repeat-y), but the third one is not working. I've tried searching a lot but couldn't find a solution. body { backgr ...