Align dropdown navigation menu/sorting dropdown in the same position as the button

Exploring the world of dropdown menus in CSS has led me to encounter some challenges. I am striving to ensure that the dropdown boxes appear on the same line as the button that triggers them. Below is the CSS code I have been working with:

/* global styles */
* {
    margin: 0;
    border: 0;
    padding: 0;
}

/* styling for the first box */
#a1 {
    display: block;
    background-color: lightblue;
    height: 300px;
}

h1 {
    text-align: center;
    padding: 25px;
    font-size: 6em;
    font-family: Shrikhand;
    color: white;
    text-shadow: 5px 5px #000000;
}

/* styling for the second box */
#a2 {
    display: block;
    background-color: lightyellow;
    height: 300px;
}

#mbar {
    width: 100%;
    height: 50px;
    background-color: gray;
}

.li {
    height: 20px;
    float: left;
    padding: 15px;
    text-decoration: none;
    list- style: none;
    background-color: lightgray;
    border-left: 1px solid black;
}

.li2 {
    height: 20px;
    padding: 15px;
    text-decoration: none;
    list-style: none;
    background-color: lightgray;
    display: none;
    width: 130.981px;
}

.dd {
    width: 130.981px;
}

.dd:hover .li2 {
    display: block;
}

Here is the corresponding HTML code:

<div id="a2">
    <div id="mbar">
        <ul>
            <li class="li">index</li>
            <li class="li">gallery</li>
            <li class="li dd">
                dropdown
                <ul>
                    <li class="li2">dropdown1</li>
                    <li class="li2">dropdown2</li>
                </ul>
            </li>
            <li class="li">contact</li>
        </ul>
    </div>
</div>

You can find the source code on codepen.

If you click on the dropdown button, you'll notice that the dropdown navigation is not properly aligned with the button.

Answer №1

If you want to achieve the desired outcome, remember to incorporate positioning techniques: see demo

In summary, the solution involves:

.dd {
 position: relative;
}

.dd ul {
 position: absolute;
 left: 0;
 top: 50px;
}

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

Exploring the concept of 'Abstract classes' within the Svelte framework

As someone who is relatively new to Svelte and frontend development (with primary experience in Rust/C++/Python), I hope you can forgive me for asking what might seem like a basic question. My goal is to showcase different kinds of time-indexed data, with ...

Running a shell script on a website should always be done in a single instance

When I use html/php to call a bash script on a website, I have multiple html-buttons that initiate the same script with different arguments. The issue arises when each button click opens a new instance of the script. Is there a way to somehow resume the r ...

Is a specific format necessary for express next(err) to function properly?

Recently, while working on my express sub app that utilizes the http-errors module, I encountered an interesting issue. When passing new Forbidden() to the next() callback, it seemed to vanish without triggering any callbacks. However, passing new Error() ...

The asynchronous callbacks or promises executing independently of protractor/webdriver's knowledge

Could a log like this actually exist? 07-<...>.js ... Stacktrace: [31m[31mError: Failed expectation[31m [31m at [object Object].<anonymous> (...06-....js)[31m[31m[22m[39m It seems that something is failing in file -06- while I am processin ...

Issues with a jQuery ajax request

Recently delving into jQuery and finding it enjoyable so far. However, I've encountered an issue with an ajax call and could use some assistance. Here's the code snippet for my ajax call: // Initiating the ajax request $.ajax({ // PH ...

Tips for positioning three child divs horizontally within a parent div with flexible dimensions and arranging each one separately

Is there a way to stack three divs horizontally within a fluid-width container div, with each child div scaling proportionally when the browser is resized for responsive design? The left-most div should be on the left, the middle div centered, and the righ ...

Creating a stunning visual effect by using CSS to fade images in and out

My current issue involves a UX switch that controls the effect on and off of pictures. http://plnkr.co/edit/JcPXpP0jE5GysBbZbrkp I am trying to achieve a smooth fade transition from one picture to another over 1 second, but I am encountering difficulties ...

Tips on searching for an entry in a database with TypeScript union types when the type is either a string or an array of strings

When calling the sendEmail method, emails can be sent to either a single user or multiple users (with the variable type string | string[]). I'm trying to find a more efficient and cleaner way to distinguish between the two in order to search for them ...

Guide on extracting targeted information from a paragraph using htmlAgilitypack

When scraping data using htmlAgilityPack, everything seems to be working fine. However, I encountered a situation where I need to extract the description from a paragraph p tag. The paragraph p tag contains the following description: <p> <small& ...

What is the reason for the failure of the jQuery code to disable the submit button in the following snippet?

I am working on a feature to disable the submit button in a form when the username, email, password fields are empty. When all of them are filled, I want to enable the submit button. However, the current code is not disabling the submit button as expected. ...

Generating a Random Number Within a Specified Range

function generateRandomNumberBetween(start, stop) { var low = Math.ceil(low); var high = Math.floor(high); return Math.floor(Math.random() * (high - low + 1)) + min; } function testRandomNumberGeneration() { var start = parseInt(prompt("Enter low ...

I am seeing the object in req.body displayed in reverse order within my Node.js and Express backend application

I encountered an issue while developing a To-do list web application using the MERN stack with Redux. The problem arises when I try to send post data to the backend. In my form, the textfield area is named 'task' (e.g., ) and I input 'jonas& ...

Clear form fields and remove any validation errors upon closing a div with jQuery

A popup div is housing a form that I want to reset both errors and fields when the form is closed. My plan was to add code in the disablePopup function to reset the form. Some important things to mention are that this is a Django project and I am using Aja ...

Update the SQL database by transferring star ratings from a JSP file

Utilizing JSP to showcase information obtained from user queries, I have implemented a system where contextual data and the query itself are saved in a MySQL database using JDBC each time a new query is input. To enhance user interaction, I wanted to incor ...

Endless jasmine timeout

This question is a continuation of a discussion on the Remove timeout for single jasmine spec GitHub issue. Query: Can a single test be configured to never time out? The Issue: While it's possible to set a global timeout value using DEFAULT_TIMEOU ...

What is the best way to create space between floated elements and a cleared element?

I have a webpage with various floated boxes and a footer positioned at the bottom. The issue arises when trying to create space between the boxes and the footer without using margins. #main { max-width: 700px; margin: 0 auto; } .box { width: 46.6 ...

What is the method for ensuring the banner image is visible behind the transparent navigation bar?

I recently used Bootstrap 4 to write the code below. In the image provided, I have outlined what my goal is with this code. Thank you! <style> div.jumbotron { background: #458392 url("img/banner.jpg") no-repeat right; ...

Angularjs - Transferring the $scope object to a controller

While taking Angular's complimentary online course, I came across the following code snippet: app.controller('GalleryController', function(){ this.current = 0; this.setCurrent = function(imageNumber){ this.current = imageNumber || 0 ...

What could be causing the Angular form to refresh the entire page?

Two different forms are displayed on the same page: <form ng-submit="actionA()"> <input type="text"> <input type="submit" value="Submit"> </form> <form ng-submit="actionB()"> <input type="text"> <inp ...

What is the proper way to use an AND operation in jQuery on multiple attributes?

I have a list of DIV tags structured as follows: <div status="1" searchText="some text">...</div> <div status="2" searchText="more text">...</div> <div status="1" searchText="even">...</div> To toggle the visibility of ...