Fixing a dropdown menu with multiple levels

I am attempting to create a dropdown menu with multiple levels. However, when I hover over the first level, the second level appears slightly above and to the left of the first level. I want the second level to appear directly below the first level in the center. Additionally, I'm unsure how to position a third level menu to the right of the second level.

.middle,
.bottom {
  background-color: #1565c0;
  width: 100%;
  overflow: hidden;
}

.middle ul.top-level-menu li:after,
.bottom ul.top-level-menu li:after {
  height: 3px;
  width: 0px;
  content: '';
  display: block;
  margin: auto;
  background: transparent;
  transition: width 0.5s ease, background-color 0.5s ease;
}

.middle ul.top-level-menu li:hover:after,
.bottom ul.top-level-menu li:hover:after {
  width: 100%;
  background: #003c8f;
}

.middle ul.top-level-menu,
.bottom ul.top-level-menu {
  padding: 0;
  margin: 0;
  list-style-type: none;
}

.middle ul.top-level-menu>li,
.bottom ul.top-level-menu>li {
  width: 25%;
  padding: 10px;
  float: left;
  text-align: center;
  text-align: -webkit-center;
  text-align: -moz-center;
  z-index: 1;
}

.middle ul.top-level-menu>li:hover,
.bottom ul.top-level-menu>li:hover {
  cursor: hand;
}

.middle ul.top-level-menu>li:hover>ul.second-level-menu,
.bottom ul.top-level-menu>li:hover>ul.second-level-menu {
  display: block;
  position: absolute;
  padding: 10px 14px;
  list-style-type: none;
}

.middle ul.top-level-menu a,
.bottom ul-top-level-menu a {
  font-family: 'Kanit', sans-serif;
  font-size: 17px;
  color: #fff;
  text-decoration: none;
}

.middle ul-top-level-menu li ul.second-level-menu,
.bottom ul-top-level-menu li ul-second-level-menu {
  display: none;
  background-color: #1565c0;
  z-index: 1;
  emphasized text border-top: 5px solid #f00;
}

.middle ul top-level-menu li ul.second-level-menu li ul-third-level-menu,
bottom ul top-level-menu li ul.second-level-menu li ul-third-level-menu {
  display: none;
  z-index: 1;
}
<nav role="navigation">

  <div class="middle">

    <ul class="top-level-menu">
      <li>
        <a href="#!">About</a>
        <ul class="second-level-menu">
          <li><a href="https://en.wikipedia.org/wiki/History" target="_blank">History</a></li>
          <li><a href="{{ route('missionvision') }}">Mission and Vision</a></li>
          <li><a href="#">Organizational Chart</a></li>
        </ul>
      </li>
      <li>
        <a href="#!">Programs</a>
        <ul class="second-level-menu">
          <li><a href="#">Nursing</a></li>
          <li><a href="#">Computer Science</a></li>
          <li><a href="#">Culinary Arts</a>
            <li>
              <li><a href="#">Accounting</a></li>
        </ul>
        </li>
        <li>
          <a href="#!">Admissions</a>
          <ul class="second-level-menu">
            <li><a href="#">Chicago</a></li>
            <li><a href="#">Los Angeles</a></li>
            <li>
              <a href="#">New York</a>
              <ul class="third-level-menu">
                <li><a href="#">Information</a></li>
                <li><a href="#">Book a Meeting</a></li>
                <li><a href="#">Testimonials</a></li>
                <li><a href="#">Jobs</a></li>
              </ul>
            </li>
            <li><a href="#">Seattle</a></li>
          </ul>
        </li>
        <li><a href="#!">Calendar</a></li>
    </ul>
  </div>

</nav>

Below is an image showing the desired output:

https://i.sstatic.net/3z9Td.png

Answer №1

Here is a modified version of your code with several small adjustments.

Important points to keep in mind:

1). It is important that an absolute element has a positioned parent, usually "relative". Your child "ul" elements were set to absolute positioning, but the parent "li" elements were not set to "position: relative", which caused issues with proper positioning of the "ul" elements.

2). To center your 2nd level content, I assigned it a width of 70%, with 10% padding on both sides.

.middle,
.bottom {
  background-color: #1565c0;
  width: 100%;
  display: table;
}

.middle ul.top-level-menu li:after,
.bottom ul.top-level-menu li:after {
  height: 3px;
  width: 0px;
  content: '';
  display: block;
  margin: auto;
  background: transparent;
  transition: width 0.5s ease, background-color 0.5s ease;
}

.middle ul.top-level-menu li:hover:after,
.bottom ul.top-level-menu li:hover:after {
  width: 100%;
  background: #003c8f;
}

.middle ul.top-level-menu,
.bottom ul.top-level-menu {
  padding: 0;
  margin: 0;
  list-style-type: none;
}

.middle ul.top-level-menu>li,
.bottom ul.top-level-menu>li {
  width: 23%;
  padding: 10px 1%;
  float: left;
  text-align: center;
  position: relative;
}

.middle ul.top-level-menu>li:hover,
.bottom ul.top-level-menu>li:hover {
  cursor: pointer;
}

.middle ul.top-level-menu a,
.bottom ul.top-level-menu a {
  font-family: 'Kanit', sans-serif;
  font-size: 17px;
  color: #fff;
  text-decoration: none;
}

.middle ul.top-level-menu li ul.second-level-menu,
.bottom ul.top-level-menu li ul.second-level-menu {
  display: none;
  background-color: #1565c0;
  border-top: 5px solid #f00;
}

.middle ul.top-level-menu>li:hover>ul.second-level-menu,
.bottom ul.top-level-menu>li:hover>ul.second-level-menu {
  display: table;
  position: absolute;
  left: 10%;
  width: 70%;
  padding: 10px 5%;
  list-style-type: none;
  background: #333;
}

.middle ul.top-level-menu li ul.second-level-menu li,
.bottom ul.top-level-menu li ul.second-level-menu li{
  position: relative;
}


.middle ul.top-level-menu li ul.second-level-menu li ul.third-level-menu,
bottom ul.top-level-menu li ul.second-level-menu li ul.third-level-menu {
  display: none;
}

.middle ul.top-level-menu li ul.second-level-menu li:hover > ul.third-level-menu,
bottom ul-top-level-menu li ul-second-level-menu li:hover > ul-third-level-menu {
  display: block;
  position: absolute;
  left:95%;
  background:red;
  text-align:left;
  list-style:none;
  padding:10px 14px;
  top:0px;
}



<nav role="navigation">

  <div class="middle">

    <ul class="top-level-menu">
      <li>
        <a href="#!">About</a>
        <ul class="second-level-menu">
          <li><a href="https://en.wikipedia.org/wiki/History" target="_blank">History</a></li>
          <li><a href="{{ route('missionvision') }}">Mission and Vision</a></li>
          <li><a href="#">Organizational Chart</a></li>
        </ul>
      </li>
      <li>
         <a href="#!">Programs</a>
        <ul class="second-level-menu">
          <li><a href="#">Nursing</a></li>
          <li><a href="#">Computer Science</a></li>
          <li><a href="#">Culinary Arts</a></li>
          <li>
            <li><a href="#">Accounting</a></li>
        </ul>
        </li>
        <li>
          <a href="#!">Admissions</a>
          <ul class="second-level-menu">
            <li><a href="#">Chicago</a></li>
            <li><a href="#">Los Angeles</a></li>
            <li><a href="#">New York</a>
              <ul class="third-level-menu">
                <li><a href="#">Information</a></li>
                <li><a href="#">Book a Meeting</a></li>
                <li><a href="#">Testimonials</a></li>
                <li><a href="#">Job Opportunities</a></li>
              </ul>
            </li>
            <li><a href="#">Seattle</a></li>
          </ul>
        </li>
        <li><a href="#!">Calendar</a></li>
    </ul>
  </div>

</nav>

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

Center an absolutely positioned div using CSS

What is the best way to position an absolute div at the center? <div class="photoFrame">minimum width of 600px, positioned absolutely</div> jQuery var screenWidth = $(window).width(); $('.photoFrame').css({'margin-left': ...

Adjust the background color of a specific list item when hovering over another list item

My dilemma lies in my inadequate knowledge to solve this issue: I have a dropdown menu embedded within a website. (View screenshot here: [SCREENSHOT]) The desired outcome is for the background color of an icon on the main list to change when I navigate to ...

Prevent vertical scrolling on touch devices when using the Owl Carousel plugin

Is there a way to prevent vertical scrolling on Owl Carousel when dragging it horizontally on touch devices? It currently allows for up and down movement, causing the whole page to jiggle. If anyone has a solution, I can share the JavaScript file. Appreci ...

I am looking to specifically target and write to a particular line in a text file using PHP

What I am searching for: file.txt: Name: John email: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abcdefghij">[email protected]</a> Here is my code snippet: <?php $myFile = "file.txt"; $txt = &apos ...

Spacing between Div tags

Struggling with a tricky HTML cross-browser problem. The site was built by multiple people, making it quite messy, but needs to go live as soon as possible! Each of the 4 page layouts on the site are different and handled by separate classes. However, whe ...

Error: Unable to modify the div content - Uncaught TypeError: Unable to assign value to innerHTML of null

Attempting to update the contents of a div using JavaScript and innerHTML, but encountering an issue. After implementing the code to change the div's content, the functionality stopped working. The syntax has been double-checked. Tools being used in ...

What could be causing my PHP login page to malfunction?

I'm facing an issue with my code where nothing happens when I click the submit button. This is puzzling as the same code worked perfectly fine in a previous project. I'm unable to pinpoint where the problem lies. Here's the HTML form: < ...

Effortlessly glide to a specific element on an HTML page

I am working with XML data that contains a lot of Test information. I am using XSLT to display this data as HTML in a web browser. Each test includes a table with the test steps, and every time a test is executed, the table updates to show whether each s ...

Add CSS styles to the outermost container element when the slideshow is currently in use

On my homepage, I have a carousel featuring multiple slides. However, when the third slide appears in the carousel, the positioning of the carousel buttons within the div class="rotator-controls" shifts downward due to the space taken up by the image. My o ...

What is the simplest method to transfer a variable from an HTML file to a Python script?

Can someone show me the simplest method to pass a variable from HTML to Python? I'm using the "input" tag to get the variable, and I only need to send it to my Python script without any response back to the HTML. Appreciate your help! ...

The Toggle Functionality necessitates a double-click action

I'm currently working on implementing a menu that appears when scrolling. The menu consists of two <li> elements and has toggle functionality. Everything is functioning properly, except for the fact that the toggle requires two taps to activate ...

I can't seem to figure out the source of this unexpected padding/margin on my website

After creating a website for a church using Foundation, SASS, and Compass, I noticed a horizontal scroll issue when resizing the window. Adding overflow-x: hidden; seemed to fix it, but there was still about 20px of padding on the right side when testing ...

"Guide to triggering the display of a particular div based on its class when clicked

I have multiple div elements with the class name dis HTML: <div class="dis">Content</div> <div class="dis">Content</div> <div class="dis">Content</div> and so on ... Additionally, there are various images: <img sr ...

Dealing with missing image sources in Angular 6 by catching errors and attempting to reset the source

When a user adds a blob to my list, sometimes the newly added image is still uploading when the list is refreshed. In this case, I catch the error and see the function starting at the right time: <img src="https://MyUrl/thumbnails/{{ blob.name }}" widt ...

What is the best way to navigate a carousel containing images or divs using arrow keys while maintaining focus?

Recently, I have been exploring the Ant Carousel component which can be found at https://ant.design/components/carousel/. The Carousel is enclosed within a Modal and contains multiple child div elements. Initially, the arrow keys for navigation do not work ...

Display different website content in a div when the page loads

My current project involves creating a team website hosted on a LAMP server. This website includes various "team pages" dedicated to different age groups. I am facing the challenge of integrating "external" content (from another page within my domain) into ...

Animation that responds to scrolling movements

Is there a way to animate text based on scrolling? <p class="class">TEXT</p> transform:translateX(-500px);opacity:0; transform:translateX(0px);opacity:1; ...

Tips on eliminating borders in react-table components

Within my React.js component, I have implemented a table using react-table along with some material-ui components displayed alongside the table: import React from 'react' import { useTable, useGlobalFilter, usePagination } from 'react-table& ...

Develop dynamic and stylized CSS using PHP within the Laravel framework

I am experiencing an issue with creating CSS files in Laravel within my controller. When I try to return the result, it shows up as a normal text file instead of a CSS file. In my routes.php file, I have: Route::get('/css/{css}.css', 'File ...

Leveraging JSON for fetching distinct identifiers from a database

When a user hovers over a parent span containing an empty img tag, I use JSON and jQuery to retrieve the src of the image from the database. The issue arises when the retrieved src is applied to all looped span images on the same page, instead of each imag ...