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

PHP for creating dynamic website search capabilities

As a beginner, I am working on developing an agriculture application to enhance my skills and take on a more advanced project. However, I am facing difficulties in implementing search functionality within the application. Here's what I have: - 4 sele ...

How to dynamically inject HTML content from a child component into a different component using Angular 5

Is it possible to customize the content of a reusable header section based on the current route data in Angular? The header currently displays a title and description pulled from the route data property. My concern is how to dynamically inject different H ...

My element is not being animated by Elementbyclass

Without Using JQUERY The animation I'm trying to create isn't functioning properly. I attempted to utilize document.getElementsByClassName, but it's not working as expected. There are no errors, but the element is not animating correctly. ...

The React component fails to render on the screen

Upon retrieving data from the database server, attempts to render it result in the data being shown in the console log but not displayed in the component. What could be causing this issue? useEffect(() => { readRequest().then(setTodos); c ...

Guide to successfully setting up a Json server and ReactJS to run simultaneously on the same port

Currently, I am facing an issue while attempting to run my React application with a JSON server as the backend API. The problem arises when trying to run them on different ports due to a CORS error. I am currently seeking advice and looking for a solutio ...

How can I display a nested div when the parent div's v-if condition is not met?

In my project, I am utilizing Laravel 6 in combination with Vue.js 2. To iterate through users and showcase their information within div elements, I am using a for loop outlined below. The Category names are stored in user.pivot.demo2, and the users are ...

Incorporate a Google map into a table row by extending it across all table data cells

Apologies for the vague title, I am having trouble articulating my issue. Nevertheless, I am dealing with a complex HTML table structure as displayed below: <div *ngFor="let item of result; let i = index"> <table ...

Vertical alignment issue with Primefaces ConfirmDialog after AJAX form update

One issue I am facing is with a form that expands using Ajax+Rendered when certain operations are clicked. The problem arises when my p:confirmDialog is not vertically centered as the form grows. It is perfectly centered when at its "normal size." I have ...

CombineReducers takes all the reducer content and unpacks it into a single reducer

As I work on developing a small application, I am contemplating the best strategy for organizing reducers within it. My objective is to retrieve JSON data from the application state and structure it as shown below: { "fruits": [ {"appl ...

Display or conceal div elements using JavaScript

Is there a way to hide certain divs when clicking on a specific div box? Clicking on the box will trigger the hiding of some application divs. See the first image below: When the boxes are hidden, the other divs will readjust in place of the current divs ...

Transmitting the Flag between PHP and JavaScript

I need help with setting a flag in PHP and accessing it in JavaScript. Currently, I have the following code: PHP if ($totalResults > MAX_RESULT_ALL_PAGES) { $queryUrl = AMAZON_SEARCH_URL . $searchMonthUrlParam . ...

floating downward inside the container

Check out my website at . Everything looks great in Firefox, but when I view it in IE, the "top" button that should be floated inside the h2 titled "Unit 301: Pre-production skills" is floating to the right and getting pushed outside of the heading. Any i ...

Transferring binary data from C# to Node.js for deserialization

I am facing a challenge where I have a byte array created in C# from an object type, which is then sent over the socket protocol to a Node.js app. However, I am unable to deserialize the data into a readable object. Can anyone suggest a way to decode this ...

Creating an unordered list navigation using jQuery and JSON data

Help needed in creating a dynamic menu using ul and li tags. The menu structure will be based on JSON retrieved from the web server. How can I implement this menu with jQuery? var data = [ { "MenuId": "4fde524c-9f8e-4fc4-a7c1-aea177090299", "Par ...

What is the best way to set up TypeScript to utilize multiple node_modules directories in conjunction with the Webpack DLL plugin?

Utilizing Webpack's DllPlugin and DllReferencePlugin, I create a distinct "vendor" bundle that houses all of my main dependencies which remain relatively static. The project directory is structured as follows: project App (code and components) ...

Fixing the height of the body padding with JS or JQuery for a

I have a rather straightforward question that pertains to an issue I am facing while building a website using BS4. Specifically, my problem revolves around a fixed-top navbar with the class "fixed-top". The challenge stems from not knowing the actual heig ...

The elements being parsed are appearing as undefined

Currently, I am attempting to analyze this JSON structure: { "customers": [ { "name":"joe" , "cars":[ {"name":"honda","visits":[ {"date":"01/30/14","Id":"201"}, {"date":"01/30/14","Id":"201"}, {"date":"02/12/14","Id":"109"} ...

The feature for favoriting or unfavorite a post is currently not functioning correctly on the frontend (react) side

I have been working on a social media website project for practice, and I successfully implemented the liking and disliking posts feature. However, I encountered an issue where when I like a post and the icon changes to a filled icon, upon refreshing the p ...

Clicking on an image in a jQuery autocomplete menu will trigger a data post to an Express

My jquery autocomplete menu is functioning properly, displaying a list of books with author, title, and book image. I am now looking to enhance it by allowing users to click on the book image and then have the book title posted to an express app.post metho ...

Combining strings in a JavaScript object

Can someone help me with concatenating strings within an object in JavaScript? I am parsing through an XML file to create a list of links in HTML. Each time I iterate through the loop, I want to add a new <li> element containing the link. How can I ...