What is the best way to include a drop-right menu within a dropdown menu?

Hey there! I'm working on a page that features a dropdown navbar. My goal is to create a secondary dropdown that appears to the right when hovering over an li element within the initial dropdown. Any tips on how to accomplish this? Thanks in advance!

I copied the code from w3schools for reference.

Here's my current code:

ul {            
    list-style-type: none;
    top: 0;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #2d2d2d;
}

li {            
    float: left;
}

li a, .curso {  
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

li a:hover, .desplegable:hover .curso {
    background-color: #c40105;
}

li.desplegable {
    display: inline-block;
}

.grupo-desplegable {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.grupo-desplegable a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.grupo-desplegable a:hover {background-color: #f1f1f1} 

.desplegable:hover .grupo-desplegable { 
    display: block;
}

Below is the HTML structure:

<ul>
    <li class="desplegable">
        <a href="#" class="curso">1º ESO</a>
        <div class="grupo-desplegable">
            <a href="#">Grupo A</a>
            <a href="#">Grupo B</a>
            <a href="#">Grupo C</a>
            <a href="#">Grupo D</a>
        </div>
    </li>
    <!-- Other similar list items -->
</ul>

The desired hierarchy is as follows:

  • 1º ESO
    • Grupo A
      • Subitem A
      • Subitem A
      • Subitem A

Answer №1

To achieve the desired layout for your dropdown menu, you can follow the same steps as you did with the initial dropdown. This time, ensure that it is positioned to the top left of the li:

/* General */
ul,
li {
  list-style-type: none;
  text-decoration: none;
  margin: 0;
  padding: 0;
}

ul li {
  display: inline-block;
  vertical-align: top;
}

ul li a {
  display: inline-block;
}

ul.main {
  background-color: #2d2d2d;
  overflow: hidden;
}

ul.main > li.desplegable > a {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

ul.main > li.desplegable:hover {
    background-color: #c40105;
}

/* Group Dropdown */
ul.main > li.desplegable > ul.dropdown-group,
ul.dropdown-group > li > ul.sub-dropdown-group {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

ul.main > li.desplegable:hover > ul.dropdown-group { 
  display: block;
}

ul.main > li.desplegable > ul.dropdown-group > li,
ul.dropdown-group > li > ul.sub-dropdown-group > li {
  display: block;
  position: relative;
}

ul.main > li.desplegable > ul.dropdown-group > li:hover,
ul.dropdown-group > li > ul.sub-dropdown-group > li a:hover {
  background-color: #f1f1f1;
}

ul.main > li.desplegable > ul.dropdown-group > li > a,
ul.dropdown-group > li > ul.sub-dropdown-group > li > a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

/* Sub Dropdown Group */
ul.dropdown-group > li > ul.sub-dropdown-group {
  top: 0;
  left: 100%;
  z-index: -1;
}

ul.dropdown-group > li:hover > ul.sub-dropdown-group {
  display: block;
}
<ul class="main">
  <li class="desplegable">
    <a href="#" class="course">1st Grade</a>
    <ul class="dropdown-group">
      <li><a href="">Group A</a></li>
      <li><a href="">Group B</a></li>
      <li><a href="">Group C</a></li>
      <li>
        <a href="">Group D</a>
        <ul class="sub-dropdown-group">
          <li><a href="">Subitem A</a></li>
          <li><a href="">Subitem B</a></li>
          <li><a href="">Subitem C</a></li>
        </ul>
      </li>
    </ul>
  </li>
  <li class="desplegable">
    <a href="#" class="course">2nd Grade</a>
    <ul class="dropdown-group">
      <li><a href="">Group A</a></li>
      <li><a href="">Group B</a></li>
      <li><a href="">Group C</a></li>
      <li>
        <a href="">Group D</a>
        <ul class="sub-dropdown-group">
          <li><a href="">Subitem A</a></li>
          <li><a href="">Subitem B</a></li>
          <li><a href="">Subitem C</a></li>
        </ul>
      </li>
    </ul>
  </li>
  <li class="desplegable">
    <a href="#" class="course">3rd Grade</a>
    <ul class="dropdown-group">
      <li><a href="">Group A</a></li>
      <li><a href="">Group B</a></li>
      <li><a href="">Group C</a></li>
      <li>
        <a href="">Group D</a>
        <ul class="sub-dropdown-group">
          <li><a href="">Subitem A</a></li>
          <li><a href="">Subitem B</a></li>
          <li><a href="">Subitem C</a></li>
        </ul>
      </li>
    </ul>
  </li>
  <li class="desplegable">
    <a href="#" class="course">4th Grade</a>
    <ul class="dropdown-group">
      <li><a href="">Group A</a></li>
      <li><a href="">Group B</a></li>
      <li><a href="">Group C</a></li>
      <li>
        <a href="">Group D</a>
        <ul class="sub-dropdown-group">
          <li><a href="">Subitem A</a></li>
          <li><a href="">Subitem B</a></li>
          <li><a href="">Subitem C</a></li>
        </ul>
      </li>
    </ul>
  </li>
  <li class="desplegable">
    <a href="#" class="course">1st Year of High School</a>
    <ul class="dropdown-group">
      <li><a href="">Group A</a></li>
      <li><a href="">Group B</a></li>
      <li><a href="">Group C</a></li>
      <li><a href="">Group D</a></li>
      <li><a href="">Group E</a></li>
      <li><a href="">Group F</a></li>
      <li>
        <a href="">Group G</a>
        <ul class="sub-dropdown-group">
          <li><a href="">Subitem A</a></li>
          <li><a href="">Subitem B</a></li>
          <li><a href="">Subitem C</a></li>
        </ul>
      </li>
    </ul>
  </li>
  <li class="desplegable">
    <a href="#" class="course">2nd Year of High School</a>
    <ul class="dropdown-group">
      <li><a href="">Group A</a></li>
      <li><a href="">Group B</a></li>
      <li><a href="">Group C</a></li>
      <li><a href="">Group D</a></li>
      <li><a href="">Group E</a></li>
      <li><a href="">Group F</a></li>
      <li>
        <a href="">Group G</a>
        <ul class="sub-dropdown-group">
          <li><a href="">Subitem A</a></li>
          <li><a href="">Subitem B</a></li>
          <li><a href="">Subitem C</a></li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

I've made changes to your code so that all dropdown menus and sub-dropdown menus are now using ul elements.

Simply hover over the last item in each dropdown menu to reveal the sub-dropdown menu.

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

Finding the index of a selected option in node.js can be achieved using express

I am trying to retrieve the index of the selected object using Express and log it in app.js example.html <form id="tableForm" action="getJson"> <select class="example" name="example"> <option name="table1" value="1"& ...

What is the best way to use jQuery to access dynamic HTML elements?

I have created a dynamic table that allows users to add and delete rows. However, I am facing an issue with accessing the information inputted by the users for calculations because it seems to be inaccessible once dynamically added to the page. Below are ...

Tips for preventing window.location from becoming relative to the file

Despite my best efforts, I couldn't figure out why this issue was occurring or how to resolve it. Here is the code in question: window.location.href=("www.google.com"); The intended functionality of this code is to redirect to google.com, but inst ...

Is it possible to make changes to local storage data without impacting the rest of the data set?

I am looking for a way to modify specific data in the local storage without affecting any other stored information. However, I have encountered an issue where editing values works correctly for the first three attempts, but on the fourth try, it seems to ...

SCORM: moving between SCOs by clicking a button in the presentation

Currently, I am developing a website that allows users to create presentations. One of the website's features is the ability to export presentations in SCORM format (either 1.2 or 2004). This is my first time working with SCORM and I am currently impl ...

Close the Bootstrap modal upon clicking the "Ok" button in SweetAlert2

I need help with closing my Modal Bootstrap after clicking the Ok button in SweetAlert. I've attempted various methods, but none of them have worked so far. I'm not sure if I'm implementing it correctly, but I've utilized JQuery for thi ...

Eliminating white space - A CSS and HTML page with no room for gaps

After finally getting my website up and running using style.css, I am faced with the following CSS code: html { height:100%; } { position: relative; z-index: 0; width: 100%; left: 0; top: 0; cursor:default; overflow:visible; } body { ...

Tips for making a div with a single triangular side using Reactjs

Hey there, I'm looking to design a header similar to the one shown in this image. Can someone provide guidance on how I can achieve this UI using React.js? ...

What determines which HTML file is loaded based on the user's browser?

I've been searching online but can't find a definite answer - is it possible to load different HTML based on the type of browser being used? In my specific case, this seems to be the only solution. After trying everything else, it looks like the ...

Error in JQuery Document Ready AJAX function causing image to not load initially without a page refresh

Currently, I have a piece of customized code that extracts data from an XML file and uses this data to populate specific content on my webpage: $(document).ready(function() { $.ajax({ type: 'GET', url: 'config.xml?date=& ...

Pressing a button meant to transfer text from a textarea results in the typed content failing to show up

Having trouble with a custom text area called a math field. I'm currently interning on a project to build a math search engine, where users can input plain text and LaTeX equations into a query bar. The issue I'm facing is that sometimes when th ...

What is the proper way to structure an Xpath search?

Recently, I've been working with Selenium in QA Test automation. When it comes to selecting/writing Xpath for an element, I've noticed that there are different formats that can be used. For example, protected final String LOGIN_BUTTON_LOCATOR = ...

"Crafting a sleek card design using Material UI components in React JS

Exploring the world of material UI and looking to create a card with an image and footer. However, I'm facing challenges as there is no default option for adding a footer in the card. https://i.stack.imgur.com/xlxyb.png I want the image to be center ...

Modifying the color scheme of the table background and text

After finding a code example online to assist with creating a table in my project, I am faced with the challenge of changing the background color of the white rows to match the dark blue used for others. Additionally, I cannot figure out how to change the ...

Is there a method to consistently keep the horizontal scrollbar in view for the MUI Data Grid at all times?

I have a Data table with virtualized columns, totaling more than 25 in number. Users can easily scroll horizontally using a trackpad, but without one, they may struggle to access all the columns due to the delayed appearance of the scrollbar. Is there a w ...

I recently designed a form using a combination of HTML and JavaScript in order to display different dialogues depending on the number of selections made. Unfortunately, the alert function does not seem to be functioning

The concept is to have users select options and then receive employment sector suggestions based on their selections. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

How to send the chosen option value from a select tag in Python Flask

My user registration form features a select tag, and I'm trying to assign the value of the selected option to a variable using the code below: registration.html <form method="POST" action="/register"> <div class="form-group"> ...

How to Align position:relative Divs Horizontally Using CSS

My goal is to showcase content from my website located at . I want to have the ability to place this content into a designated div where I can easily center or style it as needed. The issue arises when attempting to float the content, as it doesn't fu ...

The scrollbar is failing to display the entire content on the right side of the HTML document

I am facing an issue with creating a horizontal scroll bar within a div element that contains all the contents. The problem arises when a user scrolls, as it is not fully scrolled to the end of the right side, making it impossible to see the content at the ...

Is there a way to superimpose multiple PNGs and ensure that each one is clickable within its designated area?

I am looking to implement a interactive image system for dynamically generated content. The image includes a background image and various grey-scale mask images. Background Image: (source: goehler.dk) Masks: (source: goehler.dk) , (source: goe ...