Is there a way to prevent the links from shifting downward when the subnavigation menu is displayed?

Currently facing a challenge with creating a drop-down navigation bar in HTML and CSS. Initially, struggled with setting submenus to the same width as the buttons they drop down from. Resorted to setting them to width:100% and the unordered list to position: relative which fixed the issue. However, encountered a new problem where all the links from the bar also drop down when submenus expand. To better understand the issue, view the fiddle here (Hover over Chapter or Tech Ed to see the problem). It seems like whenever one problem gets resolved, another one surfaces. Any insights on what is missing?

HTML:

<body>
<div id="wrapper">
<div id="headercont">

<div id="navmain">
    <ul>
        <li class="link_nav"> <a class="title_nav" href="">Home</a></li>
        <li class="link_nav"> <span class="title_nav">Chapter</span> 
            <ul class="sub">
                <li> <a href="">About</a></li>
                <li> <a href="">Officers</a></li>
                <li> <a href="">Advisor</a></li>
                <li> <a href="">News</a></li>
            </ul> </li>
        <li class="link_nav"> <span class="title_nav">Tech Ed</span>
            <ul class="sub">
                <li> <a href="">Classes</a></li>
                <li> <a href="">Teachers</a></li>
            </ul> </li>
        <li class="link_nav"> <a href="">Design Brief</a></li>
        <li class="link_nav"> <a href="">About</a></li>
    </ul>      
</div>
</div>
</div>
</body>

CSS:

#wrapper {
    width: 900px;
    margin-left: auto;
    margin-right: auto;
    min-height: 100%;
    position: relative;
}

#header #mainnav ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
}
#mainnav ul .nav_link {
    float: right;
    width: 10%;
    list-style-type: none;
}
#wrapper #mainnav {
    color: #2B3A42;
}
#wrapper #headercont {
    display: inline-block;
    margin-left: auto;
    margin-right: auto;
    float: left;
}
#navmain ul .link_nav {
    list-style-type: none;
    display: inline-block;
}
#wrapper #headercont #navmain {
    height: 30px;
    width: 900px;
    background-color: #151C23;
    margin-left: auto;
    margin-right: auto;
    display: inline-block;
    float: left;
    margin-top: auto;
    margin-bottom: auto;
}
#headercont #navmain ul {
    padding: 0;
    margin-left: auto;
    margin-right: auto;
    text-align: center;
    margin-top: 0px;
}
ul .link_nav a {
    text-decoration: none;
    color: #D9E5E8;
    font-size: 22px;
    padding-left: 30px;
    padding-right: 30px;
}
.title_nav {
    text-decoration: none;
    color: #D9E5E8;
    font-size: 22px;
    padding-left: 40px;
    padding-right: 40px;
    width: 100px;
    position: relative;
}

.sub {
    display: none;
    list-style-type: none;
    position: relative;
}

ul .link_nav:hover {
    background-color: #D9E5E8;
}

ul .link_nav:hover .title_nav {
    color: #151C23;
} 

ul .link_nav:hover .sub{
    display: block;
    background-color: #5C6E90; 
}

.sub li:hover {
    background-color: #D9E5E8;
}

.sub li:hover a{
        color: #151C23;
}

.sub li {
    width: 100%;
}

Answer №1

You're on the right track.

To prevent the dropdown list from moving everything around, set the .sub element to have a position:absolute; property. Experiment with adjusting the values of top:0; and left:0px on the .sub element to position it relative to its parent <li> element, creating the desired dropdown effect.

https://jsfiddle.net/JohnDevelops/WW3bh/21609/

I have applied position:relative; to the parent <li> element and

position:absolute;top:20px;left:0;
to the dropdown <ul> element.

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

Ways to center your attention on the Textbox

I am currently developing a chat program using JavaScript, HTML, and CSS. How can I set the focus on the input field for typing messages? Is it usually done with CSS code? Here is the current CSS styling for my message input field: Code: #messageField ...

Shopify code for a video with a responsive src attribute

This is what I possess Direct link to the website page: ...

Looping through Jquery mouse over and mouse out events

I'm a beginner with jquery and I stumbled upon this piece of code that changes the background of a div when you hover over it and revert to the original color on mouse out: (document).ready(function(){ $(".t8").mouseover(function(){ ...

Display an additional dropdown menu after the user has made a selection in the first dropdown

Initially, I must address a concern that has previously been resolved; however, the alternative options available on this platform do not meet my needs. Essentially, I aim to create a concise form with two dropdown menus. The first dropdown menu is always ...

Effects of jQuery Show / Hide on adjacent select box operations

I created a pair of select boxes where the visibility of one is controlled by the other. Initially, the controlled select box (#select02) works perfectly on page load as long as I don't show/hide it by selecting options in the controlling select box ( ...

Exporting jsPDF without maintaining the original CSS styling

I've got this code that I'm using to export HTML to PDF, and it's functional; however, the CSS is not being applied. <body> <div class="container"> <div style="background-color: yellow" id="pagina">Page to ...

Strange behavior observed while attempting to create a smooth CSS transition effect from the right side

Is there a way to create a hover effect in my navbar where a line appears from the bottom left and top right, without causing any size or alignment issues with the links? * { margin : 0; padding: 0; } nav{ background-color: black; width: 1200px; he ...

What can be done to address the narrowing of the left div element as the right div's child elements grow in size?

Utilizing the power of flexbox, I have organized the UI into left and right panels. The child elements within the right panel are stacked vertically by utilizing the white-space: nowrap; CSS property. However, there seems to be an issue where increasing t ...

Tips for ensuring that the lightbox scrolling feature only affects the lightbox and not the entire page

Currently, I am utilizing the lightbox_me jQuery plugin to trigger a lightbox when a user clicks on a product. The issue I am facing is that the lightbox content often extends below the visible area, causing the entire page to scroll when using the right s ...

Enclose the text within a <canvas> tag

I need help with adding text to an image using the <canvas> element. The image is successfully drawn, and the text is being added on top of it. However, I am encountering an issue where long text gets cut off at the beginning and end by the canvas. ...

Hover over a picture to reveal hidden text

Is there a way to create a text overlay on an image with the text box appearing directly over where the image is positioned, causing the image to disappear completely? When the mouse moves away, the image should reappear. I have looked everywhere for a s ...

Deactivate Pagination with a Button-Based Approach

After conducting thorough research online, I have experimented with various approaches but unfortunately, none have proven successful. view image description here I am seeking guidance on how to disable pagination when clicking on the replace button. Due ...

Third Party Embedded Video requiring SSL Certificate

I am currently facing an issue where I am trying to embed videos from a website that does not have an SSL certificate. This is causing my own website to appear insecure when the page with the embedded video loads, despite the fact that my website has its ...

How can I showcase the Junit HTML report in Karate DSL, as outlined in the official Karate documentation?

After following the Karate Doc and pasting the Junit HTML Report into my browser, I was surprised to see that the output was in plain text and did not look anything like what was described in the tutorial video. I attempted to view it on both Firefox 57.0. ...

Show a variety of pictures in a random arrangement?

Looking for some help here! I'm trying to shuffle my images in a random order each time the page is refreshed. It's like a game of musical chairs but with pictures! Does anyone know how to achieve this? I've done some searching, but haven& ...

Creating a Multi-Step Form and Transmitting Data to Various Endpoints using Vanilla JavaScript

My goal is to design a multi-step form that directs each step's information to a distinct endpoint, and I'm also interested in integrating validation processes to guarantee that users can only advance to the subsequent step if their data is accur ...

Having trouble with the nth-child CSS property in Bootstrap?

My cards need some styling adjustments - specifically, I want the first and third card in each row to have a top margin of 50px. Strangely, this doesn't seem to work with Bootstrap, but it does with pure CSS. Is there a specific class in Bootstrap tha ...

Guide to rendering a dynamic Thymeleaf tag retrieved from the backend

Is there a method to display Thymeleaf tags that are received from the back-end? For example, I have a JSON object called textObj as shown below coming from the back-end. textObj.json { "title" : "Page Title", "text" : "blah blah blah blah <div th: ...

Utilizing AJAX to showcase individual PHP files

My PHP elements are not being displayed in the code, and I need to figure out why. Below is the HTML code I am currently using: <div class="tablocation"> <ul class="css-tabs"> <li><a class="current" href="/wp-content/themes ...

Enhance the speed of webview on Android devices

I have been working on an application that utilizes a webview to load both HTML and JavaScript files. Unfortunately, I have noticed a significant decrease in performance when displaying the content. Despite trying various solutions such as: webVi ...