Creating submenus that cause the navigation menu to drop down

I created a basic navigation bar using HTML Lists. The initial structure looks like this:

<div id="menu">
    <nav>
        <ul>
            <li><a href="#">My Profile</a></li>
            <li><a href="#">Inbox</a></li>
            <li><a href="#">Notifications</a></li>
        </ul>
    </nav>
 </div>

When I attempt to add a submenu, the structure changes to:

<div id="menu">
    <nav>
        <ul>
            <li>
            <a href="#">My Profile</a>
            <ul>
                <li><a href="#">My Questions</a></li>
                <li><a href="#">Settings</a></li>
            </ul>
        </li>
            <li><a href="#">Inbox</a></li>
            <li><a href="#">Notifications</a></li>
        </ul>
    </nav>
 </div>

However, adding the submenu causes the design of the menu to be disrupted. Here is the relevant CSS:

#menu {
    float: right;
    margin-right: 5%;
}

#menu nav ul li {
    display: inline;
    padding: 5px;
}

#menu nav ul li a {
    color: black;
    text-decoration: none;
    padding: 2px;
}

#menu nav ul li a:hover {
    background: #eee;
    border: 0;
    border-radius: 3px;
    box-shadow: 0px 0px 2px 1px #000;
}

#menu nav ul li ul li {
    display: block;
}

How can I resolve this issue and maintain the aesthetics of the menu?

Here is a link to illustrate what I have attempted: Link.

This is the desired outcome:

Answer №1

To ensure proper display of parent list items inline, you can set the submenu with position: absolute to remove it from the main flow.

In order to align the submenu vertically, make sure to set the parent <li> element's positioning to position: relative. This will position any absolutely positioned children, such as the submenu, relative to that element. To keep the submenu on the left side of the parent, simply use left: 0.

Below is the code snippet to align submenus vertically beneath their respective parent list items:

#menu nav > ul{
    position: relative;
}

#menu nav > ul ul{
    position: absolute;
    left: 0;
}

For a live example, check out this link: http://jsfiddle.net/6Qtbj/2/

Answer №2

Can the 'inline-block' display property improve layering in this case?

#menu nav ul li {
    display: inline-block;
    padding: 5px;
}

check out the fiddle here

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

Using a single jQuery script, implement multiple modals on a webpage that are responsive on both desktop and mobile devices

My Modal is functioning well on desktop, but I'm facing an issue on mobile where the screen doesn't close when clicking outside the modal area. Is there a way to solve this for mobile without adding the 'X' button to close it? I attem ...

Ways to include "working hours" if statement a particular holiday dates are specified

Trying to update the code for an "if statement" to include specific dates that are official holidays when the business is not operational. The current code works if the day falls on Mon, Tue, Wed, Thu, or Fri and the time is between 09:00 and 15:00 in a 24 ...

Avoiding the overflow of the y-axis by applying a slight left margin to bootstrap columns

Context: I'm in the process of developing a collapsible sidebar navigation menu inspired by the bootstrap admin panel example found at: Issue: Upon collapsing the admin bar, a small bar of icons appears. To accommodate this, I added margin-left: 50 ...

Maximizing the number of divs arranged horizontally in HTML while utilizing the full line width

My website consists of several fixed-width div elements that are styled to flow inline using the display type inline-block. This layout creates empty space at the end of the line where subsequent div elements cannot be accommodated and have to wrap to the ...

The placement of toolbar buttons in HTML

My current dilemma involves the placement of a logo on the navigation bar of my website. When the logo is present, the text is not centered vertically, but when I remove the logo it centers correctly. * { margin: 0px; padding: 0px; } .logohead { d ...

Visual Latency when Quickly Toggling Between Images in Succession

I have a series of 50 images that need to be displayed sequentially inside a div. The time interval between displaying each image is initially set at 750 milliseconds and decreases with each subsequent image. To ensure all images are loaded before the an ...

Overlaying a parent container DIV on top of its child elements

My JavaScript scrolling view is quite intricate, enclosed within a DIV. The elements inside have event handlers for mouseover and mouseout to manage the scrolling behavior, as well as clickable links. Currently, I am in need of a semi-transparent image th ...

Selectors for fake elements and neighboring siblings

I'm struggling to show my menu when the toggle checkbox is selected. I know that my .menu-container is not a sibling of the toggle element, but I'm not sure how to make it work without moving the toggle outside of the div so that .menu-container ...

Incorporate an HTML form into the primary HTML webpage using AngularJS application technique

As a newcomer to AngularJS, I am attempting to grasp how it can be integrated with an ASP.NET-MVC5 application. For my initial test, I want to launch an AngularJS app from a basic HTML page (in this case index.html) where the app contains a form template i ...

How to Align HTML Menu in the Middle of the Page

I've been struggling to center my menu on the page. I tried setting the display to block and using margin auto for left and right, but it's not working as expected. You can see the issue in this JSFiddle. Any help would be appreciated. <ul id ...

Is there a way to ensure that all elements on a page display properly across all screen resolutions without being cut off?

My website consists of three main elements: pictures, Cards (containing pictures and various typography), and Buttons. These elements are arranged in a column layout. The issue I am facing is that the pictures and buttons get cut off on the screen due to ...

Is there a way to give a unique color to a single <a> element with a specific class, differentiating it from the

Apologies if my title is not precise enough. Describing this in just a few words for the title was a bit challenging. Context I am personalizing a template on wordpress.com. The website in question is: I have enclosed the DONATE menu item in a gold-col ...

SQL Delete rows from Table

I am dealing with an issue where a table displaying notices has a clear option next to each row, but when the clear button is clicked nothing happens. The setup involves a button that triggers a clear function. What could be causing this problem? <bu ...

Using jQuery and HTML to create numerous links that direct to a solitary iframe

I created a basic overlay setup using css, html, and jQuery. Here's how it looks: <a class="activator" id="activator" style="" href="#">hello</a> <div class="overlay" id="overlay" style="display:none;"></div> <div style="d ...

Make the bootstrap button group adjust its size to fit the container dimensions

My goal is to create a display for multiple images with descriptions using a button cycling system. However, I am facing an issue where the buttons overflow the container instead of fitting within it when I wrap the cycler in a container with a fixed width ...

Having trouble aligning a div vertically inside another div?

I am struggling to vertically align a div within another div. I have experimented with vertical-align, position: relative; top: 50%, and margin: auto; without success. Take a look at the code below: .main { font-family: "adobe-garamond-pro"; pad ...

I'm having trouble getting this button and icon to align properly. How can I fix this misalignment

Take a look at the HTML code snippet I'm currently working on... <div id="projects"> <H1 class="projects">PROJECTS</H1> <div class="box"></div> <div class="box"></div> <div class="box"></ ...

'OR' separated by the <hr> tag

This particular code functions correctly on the Firefox and Chrome browsers. However, there seems to be an issue with IE and Opera, where the 'OR' text is only visible within a 2px height area. hr { height: 2px; border: 0; background-color ...

Issue with the close button on ngb-alert not functioning properly

As I develop my website, I have incorporated an ngb-alert component to display an alert message to users upon login. While the alert itself is functioning properly, I have encountered an issue with the close button being misaligned, and I am struggling to ...

Data bindings encapsulated within nested curly braces

I am currently utilizing Angular2 in my application. Within my html file, I have a *ngFor loop set up like so: <div *ngFor="let element of array"> {{element.id}} </div> Next, I have an array containing objects structured as follows: some ...