The div element moves to the right as soon as the drop-down menu pops up

Currently, I am facing an issue with aligning an image inside a div that is centered in an outer-wrapper. The outer-wrapper contains a horizontal menu at the top with 5 sub divs displayed inline. I have implemented CSS for a drop-down menu that appears when hovering over the first sub div in the line of 5. However, when the drop-down menu displays, it causes the image to shift to the right unexpectedly. Despite my attempts to adjust the z-index property, I'm struggling to find the correct solution. It seems like there might be some misunderstanding on my part about how z-index works and its proper usage.

HTML:

<div class="wrapper">
     <div id="topmenu">
        <div id="home"><a href="index.html">Home</a>
            <ul>
                 <li>item 1</li>
                 <li>item 2</li>
                 <li>item 3</li>
            </ul>   
        </div>  
     </div>

     <div id="logo">
        <img src="image.jpeg" />
     </div>
</div>

CSS

.wrapper{
    position:relative;
    width:960px;
    height:905px;
    margin-top:5px;
    margin-left:auto;
    margin-right:auto;
    /*text-align:left;
    border:2px solid red;*/
    background-color:#FFFFFF;
}

#topmenu{
    position:relative;
    border-bottom:2px solid #164207;
    height:30px;
    background-color:#ffffff;
    z-index:3;
} 

#logo{
    position:relative;
    border-bottom:2px solid #164207;
}


#logo img{
    position:relative;
    height:350px;
    width:500px;
    z-index:1;
}

#home{
    display:inline-block;
    float:left;
    margin-top:5px;
    margin-left:15px;
    width:169px;
    color:#164207;
    font-family:serif;
    font-weight:bold;
    font-size:20px;
    text-align:center;
    border-right:2px solid #164207;
}

#home:hover .sub-menu {display:inline-block;}

.sub-menu {
    overflow:hidden;
    display: none;
    width: 169px;
    background-color: #164207;
    color:#FFFFFF;
    margin-top: 5px;
    border: 1px solid #fff;
    -webkit-box-shadow: 0px 13px 25px rgba(0,0,0, 0.2);
    -moz-box-shadow: 0px 13px 25px rgba(0,0,0, 0.2);
    box-shadow: 0px 13px 25px rgba(0,0,0, 0.2);
}

.sub-menu li {
    position:relative;
    list-style-type: none;
    display: block;
    border-bottom: 1px solid #FFFFFF /*#eaeaea*/;
    font-family:serif;
    font-size: 12px;
    height: 15px;
    padding: 8px 0;
}

Answer №1

To create a stacking context for .sub-menu, make sure to include position: absolute; in the CSS.

Check out this jsFiddle link

#home{
    display:block;
    float:left;
    margin-top:5px;
    width:184px;
    color:#164207;
    font-family:serif;
    font-weight:bold;
    font-size:20px;
    text-align:center;
    border-right:2px solid #164207;
}

#home:hover .sub-menu {display:block;}
#course:hover .sub-menu{display:block;}
#leagues:hover .sub-menu{display:block;}
#events:hover .sub-menu{display:block;}
#about:hover .sub-menu{display:block;}

    .sub-menu {
        overflow:hidden;
        display: none;
        width: 182px;
        background-color: #164207;
        color:#FFFFFF;
        /*padding: 10px 10px;
        margin-left: 0px;*/
        margin-top: 5px;
        border: 1px solid #fff;
        -webkit-box-shadow: 0px 13px 25px rgba(0,0,0, 0.2);
        -moz-box-shadow: 0px 13px 25px rgba(0,0,0, 0.2);
        box-shadow: 0px 13px 25px rgba(0,0,0, 0.2);
        position:absolute;    /**************************** Important Bit*/
        top:24px;
    }

For alignment adjustments- Remove margin-left:15px; from #home, #course, #leagues, #events, and #about. Make sure to adjust widths accordingly and update the width of .sub-menu. Refer to the updated jsFiddle link above for detailed instructions and a functioning model.

Here is an example with navigation set up using unordered list <ul>. No need for redundant id's and CSS.

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

Set a dynamic Active Class on various divisions by utilizing their respective indexes

I have two divs with the same class. When I click on an anchor tag within one of the elements, I want to add a class to the corresponding position in the second div as well. Mirror: JSFiddle Here is the jQuery code snippet: $(document).on('click ...

Achieving dynamic HTML element addition through JavaScript while maintaining their record in PHP

There is an input box where the user specifies the number of new DIV elements to be added dynamically. This request is then sent to PHP via Ajax, where it prepares an HTML DIV with some inner input tags. Each DIV has a unique id attribute generated increme ...

Upon loading a website, the Chrome browser will automatically set the zoom level to 80%

Recently, I've encountered a perplexing issue with Chrome where my website is automatically zoomed out from 100% to 80%. It seems that browsers cache zoom settings based on the domain and apply them when users revisit the site. However, in this case, ...

Animate CSS during page load

Currently, I am implementing AJAX to dynamically load pages on my website. During the loading process, I wish to incorporate a spinning animation on the logo to indicate that content is being fetched. The jQuery script (although I'm still learning an ...

Ways to dynamically modify the CSS attributes of a webpage during the page load event

I'm looking to incorporate the background:rgba(226, 93, 141, 0.76) attribute into the body:after psuedo-element once my page has finished loading. Below is my HTML and CSS code: <style runat="server" id="myStyle" > body:after{ position:fix ...

Determining the quantity of displays

Can we detect the number of screens in use using JavaScript or jQuery? We are working on an application that should not be displayed on multiple screens, even if the hardware allows for it. Appreciate any assistance with this matter. Thank you. ...

Identifying and detecting Label IDs when clicked using the for tag

I am facing an issue with labels and input fields in my code. I have multiple labels that trigger the same input field, but I want to know which specific label triggered the input field. <label id="label1" for="input1">Document1</label> <la ...

Having trouble showing table data in Angular

My goal is to showcase data from a table created using Spring-Boot Below is my model.ts: export class Quiz1 { QuestionId?: any; Question?: string; OptionsA?: string; OptionsB?: string; OptionsC?: string; OptionsD?: string;} He ...

Styling with CSS to format a table so that each cell occupies one row when viewed on narrower

I've come across some HTML that resembles this structure <html><body><table> <thead><tr><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th></tr></thead ...

I am working with three columns and I am looking to keep the center column fixed in place while scrolling

Check out this CSS snippet: #container { margin: 0 auto; width: 1200px; } #col1 { float: left; width: 700px; } #col2 { float: left; width: 100px; padding: 0 17px; } #col3 { float: left; width: 400px; } I am trying t ...

The alignment of the image background is malfunctioning on the iPad 3 device

Hey there, I need some assistance resolving an issue with a header background image on iPad3. The problem is that the image is not displaying correctly on iPad3 browsers, even though it works fine on desktop browsers. Below you can find my CSS and HTML cod ...

Enhance your Angular material datepicker with additional content such as a close button

I'm currently working on customizing my Angular Material datepicker by adding a button. The goal is to have this button placed in the top right corner and enable it to close the datepicker when clicked. In addition, I also want to include extra conte ...

Unable to delete a dynamically inserted <select> element by using the removeChild method

As someone who is new to coding web applications, I am currently working on a project that involves adding and deleting dropdowns dynamically. Unfortunately, I have run into an issue where the delete function does not work when the button is pressed. Her ...

Executing JavaScript code within ASP.NET Visual Basic

My current web application uses jQuery and JavaScript, but I want to add more features that are supported in ASP.net VB. However, I am unsure if the JavaScript can run after the VB code. Essentially, I would like the web app to follow this sequence: ...

Tips for showcasing information beneath a title on a webpage in Yii2 without duplicating the header

Looking to pass an array to my view that includes headings and corresponding content lists. The structure of my array is as follows: Array ( [0] => Array ( [trimester] => Trimester1 [subject] => ABC ) ...

Prerender is running independently of the dynamic page title and meta tags rendering process

As part of a POC, I was integrating prerender.io with an angular-node application for SEO purposes. My application can be found HERE. The good news is that all three links are being crawled successfully, as confirmed by receiving a 200 OK status for all li ...

Is there a proper method for populating an HTML text with values from a form?

As a newcomer, I could really use some guidance here :) I'm trying to populate text with various words, numbers, and calculation results from a form. This is my initial attempt for just one field/word, and it appears to be functioning correctly. Do y ...

Trigger a fire event upon entering a page containing an anchor

My query is similar to this particular one, with slight differences. I am working on a page that includes an anchor, such as page.html#video1. Within this page, there are multiple sections identified by ids like #video1, #video2. Each section comprises of ...

What is the best approach to animating a specified quantity of divs with CSS and javascript?

How neat is this code snippet: <div class="container"> <div class="box fade-in one"> look at me fade in </div> <div class="box fade-in two"> Oh hi! i can fade too! </div> <div class="box fade-in three"& ...

"Enhabling tablesorter pagination to ensure that buttons always stay in sync with

I am experiencing an issue with the pagination buttons staying at the bottom of my page, even when there are only 2 entries left on the last page. Check out my table here: Is there a way to make the pagination buttons dynamically move to the top based on ...