When the dropdown list appears in the navbar, it causes other elements to shift and move around

Forgive me for any CSS mishaps as I'm still learning the ropes.

Currently, I'm working on a website with a navbar consisting of several divs. Some of these divs reveal a dropdown list upon hover. However, the issue arises when this action expands the width of the parent div, causing surrounding divs to shift positions. This unwanted resizing seems to match the lengthiest element within the dropdown list. Despite my attempts to adjust positioning properties, nothing has resolved the problem. My main goal is to prevent adjacent divs from moving unexpectedly.

HTML:

<body>
<div  class="header" id="banner"></div>
<div class="header" id="navbar">
    <div class="nb_item">HOME</div>
    <div class="nb_item">ABOUT US
        <ul id="abtus_menu">
            <li>LA MARTINIERE COLLEGE, LUCKNOW</li>
            <li>LA MARTINIERE MODEL UNITED NATIONS</li>
            <li>SECRETARIAT</li>
        </ul>
       </div>
    <div class="nb_item">REGISTER
        <ul id="rgstr_menu">
            <li>INDIVIDUAL</li>
            <li>DELEGATION</li> 
        </ul>
    </div>
    <div class="nb_item">COMMITTEES
        <ul id="comt_menu">
            <li>COMMITTEE 1</li>
            <li>COMMITTEE 2</li>
            <li>COMMITTEE 3</li>
            <li>COMMITTEE 4</li>
            <li>COMMITTEE 5</li>
            <li>COMMITTEE 6</li>
        </ul>
    </div>
    <div class="nb_item">RESOURCES</div>
    <div class="nb_item">EVENTS
        <ul id="evnt_menu">
            <li>KEYNOTE SPEAKERS</li>
            <li>SOCIALS</li>
        </ul>
    </div>
    <div class="nb_item">SPONSORS</div>
    <div class="nb_item">CONTACT US</div>

CSS:

.html
{
    background-color: #FFFFFF;
}

body
{
    margin: 0;
    padding:0;
}

.header
{
    width: 80%;
    margin:auto;
    padding:0;
    display:flex;   
}

#banner
{
    height: 200px;
    width: 80%;
    background-color: #EEEEEE;
}

#navbar
{
    background-color: #70A5DA;
    height: 28px;   
}

.menu
{
    list-style-type:none;
}

.nb_item
{
    display:inline-block;
    width:auto;
    padding:0px 10px;
    color:#FFFFFF;
    margin:auto;
    text-align:center;
    line-height:28px;
}

.nb_item:hover
{
    color: #DDDDDD;
    cursor:pointer;
}

#abtus_menu li
{
    list-style-type:none;
    display:none;

}
.nb_item:hover #abtus_menu li
{
    display:block;
    color: #DDDDDD;
}

#comt_menu li
{
    list-style-type:none;
    display:none;
}

.nb_item:hover #comt_menu li
{
    display:block;
}

#rgstr_menu li
{
    list-style-type:none;
    display:none;
}

.nb_item:hover #rgstr_menu li
{
    display:block;
}

#evnt_menu li
{
    list-style-type:none;
    display:none;
}

.nb_item:hover #evnt_menu li
{
    display:block;
}

.menu li
{
    display:inline;
}

View my JSFiddle here: JSFiddle (Zoom out to 50% if necessary)

Answer №1

    .nb_item {
        position: relative;
        display:inline-block;
        width:auto;
        padding:0px 10px;
        color:#FFFFFF;
        margin:auto;
        text-align:center;
        line-height:28px;
    }
    ul {
        padding-left: 0;
        position: fixed;
    }
    .nb_item ul li {
        text-align: left;
    }

To change the ul position from absolute to fixed, you do not need to assign any specific width to any element.

Answer №2

The reason the DIVs impact the document flow is because they are inherently part of it. To prevent this, you can use absolute positioning on them as shown below:

.nb_item {position: relative}
/* This enables child elements with absolute positioning to relate to their parent's position (.nb_item) */

Subsequently:

.nb_item ul {position: absolute; top: 100%; left: 0}
/* By using absolute positioning, the child (dropdown menu) element is removed from the document flow without affecting other elements' positions */

Hopefully, this explanation clarifies things!

UPDATE: To avoid issues with width when using absolute positioning, define a specific width like so:

.nb_item ul {width: 200px; border: 1px solid #000}
.nb_item ul li {text-align: left}
.nb_item ul li a {padding: 10px}

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 send a variable to a custom component without having it executed or processed upfront?

I recently created a unique custom component that includes a form group with specific functionality. <template> <div class="form-group"> <label for="desc">{{label}}</label> <input id="desc" type="text" class="form-cont ...

Understanding the process of extracting HTML tags from an RSS feed using Python

I am looking for a way to create a plain text readout of an RSS feed using a small utility. Below is the code I have: #!/usr/bin/python # /usr/lib/xscreensaver/phosphor -scale 3 -program 'python newsfeed.py | tee /dev/stderr | festival --tts' ...

Troubleshooting problem with Laravel and Vue integration

I have experience working on a Laravel and Vue.js project. Here is the code snippet for the Laravel view: @extends('layouts/app') @section('content') <div id="app"> </div> @endsection And here is the ...

JavaScript function occasionally experiences loading issues with CSS images

Have you ever encountered issues with a CSS background image failing to load? What might be the underlying cause? I've written some Javascript code that looks like this: function progressBar(file_id) { pbar = ($("<div />").attr('id&a ...

In Chrome browser, the orientation of the options in the datalist remains consistent

Is there a way to change the direction of the datalist's options to RTL? While I'm able to change the direction of the input element, the same doesn't apply to the options of the datalist. I've attempted setting the dir attribute to r ...

What is the best way to change an HTML string into an HTML DOM element?

I am struggling with a webservice that sends back an HTML string as the response. My goal is to show this response within a div element. The current response from the service appears like this: <span style="text-indent: 29px;padding-top:35px;font-size: ...

Verify Radio Buttons in AngularJS

I thought validating a selection from a radio group would be simple, but I'm struggling to find the right solution. In my form, I need to ensure that at least one option is selected from the radio buttons. Using the 'required' attribute on e ...

Incorporate dynamic 3D elements into your website for an engaging

I've been tasked with creating a website for a residential building project. The client requested an interactive 3D view be embedded into the site, where all available flats are colored differently from those that have already been booked. Hovering ov ...

Color-coding HTML table cells depending on the SQL flag value

After successfully constructing a SQL query that incorporates three conditions from my database table and sets a flag based on them, I have made progress in my project. The query looks like this: UPDATE staging SET `miuFlag` =1 WHERE `lowSideMIUNumAr ...

Utilizing a background image in the slick slider

<div id="largeCarousel" style="display:inline-block; float:right;"> <div class="homepage-item" style="padding-bottom:52%; position:relative; box-sizing:border-box;"> <div id="largeCarouselContent" style="position:absolute ...

The method window.getComputedStyle retrieves the CSS max-height property containing an unresolved calc() function

Has anyone encountered a challenge with a function related to Angular in their project? Here is a snippet of the code causing issues: console.log(window.getComputedStyle(this.frontLayer.nativeElement).maxHeight); And within the component template: <di ...

I utilized the "data-target" attribute to ensure the active link retains its style. Is there a way to preserve the style when navigating away from an active link?

Can someone assist me with this re-upload? I need help tweaking my code to maintain style when navigating between pages using the "data-target" attribute. Currently, the style is being lost when moving from the original link (e.g., "link/sub01.html") to a ...

The CSS style of the Div element is not being displayed properly when embedded with JavaScript

Currently, I am working on a simple page for practice purposes. The main issue I am facing is with a div element that has a red border and a blue background. Inside the div, there is a script tag calling an external JavaScript file. Surprisingly, the JavaS ...

What are the steps to personalize Twitter Bootstrap with Less for changing the height of the Navbar?

I recently stumbled upon some interesting articles that explain how to customize various elements of Twitter Bootstrap using LESS. You can check out one of the articles here and find more information about Twitter Bootstrap here. However, I am still unsure ...

Issue with Colspan in Nested Tables in Bootstrap 4.1.3

I'm dealing with an issue related to colspan within a nested table. I've tried various solutions, including wrapping the table in a div, but nothing seems to be working as expected. My goal is to have the nested table span across the entire width ...

Restricting the embedding of my website to a select few domains

Looking to embed my web app on various websites, but I want to restrict access based on domain. For example, allowing and to embed without issues, while blocking . Is there a way to achieve this? Using iFrames for the embedding process. ...

Whenever I click on <a href="whatever.aspx"></a>, I would like the page to be displayed within the current page

This is the code I am working with: <asp:DataList ID="dlGallery" runat="server" RepeatLayout="Flow" Width="100%" CellPadding="4" ForeColor="#333333"> <AlternatingItemStyle BackColor="White" ForeColor="#284775" /> <FooterStyle BackColor ...

Adjusting the size of text according to the image and screen dimensions with Bootstrap

If you want to take a look at my SSCCE, here is the jsFiddle link. However, I'll provide an overview of my code below. My page is created using Twitter Bootstrap 4.0: <!DOCTYPE HTML> <html> <head> <title>FooBar</title&g ...

Forms gently float alongside one another

My goal is to have my two forms displayed side by side, but currently they are appearing stacked on top of each other. Below is the code I am using: <id ='AvailabilityFormAM'><form action='availability.jsp' name='availFo ...

Calculate the position of an element's top property using dynamic JavaScript calculations

I am currently working on a script to control the scrolling speed of elements on a webpage relative to their parent div. The code I have written so far is as follows: $('#two').css({'top' : 600-($(this).scrollTop() / 1.2)+"px"}); The ...