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

rendering google charts using jade template

I am facing an issue while trying to display a google annotated chart on my jade project. I managed to successfully load a pie chart, but I am having trouble rendering a chart that requires the container element size to be explicitly defined. You can find ...

There seems to be an issue with the CSS header alignment as the logo is not properly positioned using a top margin of -20px

My goal is to create a header bar that includes an image, a title, and a menu car. I want the bar to be displayed after scrolling 50px down, and I also want the image to be positioned slightly above the wrapping div, showing a bit of it in the top 50px. Ri ...

Troubleshooting: Issues with CSS fixed positioning

Whenever I try to scroll down, my navbar moves along with the page. Even when I resize the window, it keeps shifting despite using position:fixed. What mistake am I making? nav li:nth-child(1) { position: fixed; border: 1px solid #15317E; font-si ...

Using Jquery and css to toggle and display active menu when clicked

I am trying to create a jQuery dropdown menu similar to the Facebook notification menu. However, I am encountering an issue with the JavaScript code. Here is my JSFiddle example. The problem arises when I click on the menu; it opens with an icon, similar ...

Using Angular/Typescript to interact with HTML5 Input type Date on Firefox (FF)

Are there any Angular/Typescript projects that are completely built without relying on third-party libraries? I am encountering problems with Firefox and IE11. It works fine on Chrome where the value can be read, but the calendar does not display when us ...

Instructions for activating a button in the absence of any inputs

I need help enabling a button in angularjs based on whether any of the inputs are filled out. I was successful in disabling a button when only one input is checked, but how can I do this for all inputs affecting one button? This is what I've attempted ...

Issues with jQuery horizontal sliding gallery functionality

My attempt at creating a jQuery sliding video gallery is not working as I hoped. Instead of scrolling through the images when clicking arrow buttons, the entire div moves left or right depending on the direction. HTML: <div id="videocontainer"> & ...

Utilize grids to ensure consistency in the width of every <li> element across the browser

Is there a way to make the ul element span the entire width of the webpage regardless of window size? http://jsfiddle.net/32hp1w5p/ ul { grid-column: 1/13; list-style-type: none; display: table; margin: 0 auto; } li { float: left; border: ...

Should Chrome be allowed to have TABLE tags within P tags?

When testing the code below on Google Chrome: <p>Some words <table> ... </table> I noticed that the nesting under Chrome appears as html > body > p > table. It seems like there is a missing </p>, but according to HTML4, ...

Choose just one column within an HTML table

Is there a way to easily select the text of a single column from an HTML table using just the mouse pointer? Imagine you have a table that looks something like this: https://i.sstatic.net/n3lZR.png When trying to select only the text in the Lastname col ...

Using CSS :hover for elements with dual classes only

Is there a way to make the CSS :hover selector work only when two different classes are attached to a div? I have tried a couple of methods, but they eliminate the hover effect. For example: .accordionButton .cyan:hover{ color: cyan; } I cannot s ...

Is it possible to implement the SCSS CSS preprocessor in Angular 1.6.6, even though it is already utilizing LESS as the current CSS preprocessor?

Is it necessary to compile SCSS and Stylus files into CSS before importing them into my Angular version 1 app? Are there any other alternatives available? Context: I have two applications, one using Angular 4 with SCSS and the other using Angular 1 with L ...

The clipPath feature in webkit fails to display correctly

After reading this insightful article, I decided to experiment with applying a gradient clip-path to a simple shape (an O letter converted to curves). To my frustration, the technique worked perfectly in Firefox but failed to render anything in webkit bro ...

When a DIV is clicked, trigger the fadein effect on another DIV; when the same DIV is clicked again, fade

I have a sliding panel on my webpage that reveals the navigation (www.helloarchie.blue). I want the content inside the panel to fade in slowly when it slides out, and then fade out when the user clicks the icon to close the panel. How can I achieve this ef ...

The Unexpected Mishaps of CSS Grid Layout

I attempted to create a grid using CSS, but I seem to have made an error somewhere in my code. Can someone please take a look at my jsFiddle link and provide some guidance? jsFiddle /*Grid*/ .container { width: 100%; max-width: 1200px; } .row:before, ...

Position elements flush to the left and right

Is it possible to align the text in divs so that it perfectly lines up along the edges? .name { margin: 0 0 5px 10px; font-weight: bold; font-size: 14px; } .row { margin: 0 0 5px 10px; width: 500px; justify-content: space-between; } < ...

Transforming a Bootstrap 4 HTML project into Angular 9

After stumbling upon a beautiful HTML template that caught my eye, I realized it was built using Bootstrap. Luckily, I came across tutorials on how to convert such templates into Angular 6 projects, making the process seem quite straightforward (like this ...

Hover over this area to reveal the hidden text as it slides into view

My goal is to hover over truncated text and reveal the full length of the dynamic text. However, I am struggling with removing extra space at the end of shorter text when hovered and displaying all the text for longer ones. I have a solution that almost w ...

Having trouble with JQuery Draggable in FireFox only working in an iFrame? Learn how to set the ViewPort to fix

Having trouble with jQuery UI Draggable in FireFox 33.0.2: I followed the example code, but it doesn't seem to be working. The scripts are running, CSS classes are added, event bindings are in place, but dragging won't work. I noticed that when ...

Issue with login form in IONIC: Form only functions after page is refreshed

Encountering an issue with my Ionic login form where the submit button gets disabled due to invalid form even when it's not, or sometimes displays a console error stating form is invalid along with null inputs. This problem seems to have surfaced afte ...