What changes should I make to my code in order to incorporate an additional level of submenu to my CSS-based dropdown menu?

Hello and thank you for your help,

I currently have some CSS code that is working well for 3 levels of dropdown menus, but I am now in need of it to also support a 4th level. When I try to add the extra level by copying the 3rd level code and making adjustments, it does not work as expected. It's important that this is achieved using only CSS and no jQuery code. The main issue I'm facing is that the 4th tier does not align correctly with its parent, appearing inline instead of beneath.

`

<!DOCTYPE html 
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

<style type="text/css"> 

/* Styles for the Menu */
.menuBackground {
                background-color: #164149; /* Dark Green */
                color: White;
                float: left:         
                text-align: left;    
                white-space: nowrap; 
                margin-left: 1px;    
                margin-top: 12px;    
}

.nav-text {
                display: inline-block;
                vertical-align: -2px;
}

.arrow {
                width: 0; 
                height: 0; 
                border-left: 4px solid transparent;
                border-right: 4px solid transparent;
                border-bottom: 4px solid black;
                display: inline-block;
                padding-bottom: 5px; 
}

.right {
  transform: rotate(90deg);
  -webkit-transform: rotate(90deg);
}

.dropDownMenu,
.dropDownMenu ul {
                list-style: none;    
                margin: 0px;            
                padding: 0px;        
                font-weight: Normal;
                font-family: Calibri;
                font-size: 14px;
}

.dropDownMenu li {
                background-color: #164149; /* Dark Green */
                color: White;                    
                position: relative; 
}

.dropDownMenu a {
                padding: 12px 20px;
                display: block;
                text-decoration: none;
                background-color: #164149; 
                color: White;
                font-family: Calibri;
                font-weight: Normal;
                font-size: 14px;
}

.dropDownMenu a:hover {
                background-color: white; 
                color: black;
}


.dropDownMenu > li {
                display: inline-block;
                vertical-align: top;
                margin-left: -4px; 
                background-color: #164149; /* Dark Green */
                color: White;
}

...

/* Level 4 */
.dropDownMenu  > li > ul > li > ul > li > ul{
                text-align: left;
                display: none;
                background-color: #bddbdb; 
                color: Black;
                position: absolute;
                left: 100%;
                top: 0;
                z-index: 9999999;
                font-weight: normal; 
                font-family: Calibri;
                font-size: 14px;
}

.dropDownMenu  >li > ul > li > ul > li a {
                padding: 10px 20px;
}

.dropDownMenu > li > ul > li > ul > li:hover > ul{
                display: block;
                background-color: #164149; 
                color: White;
}

.dropDownMenu  > li > ul > li > ul > li:hover > a {
    background-color: #164149;  
    color: White;
}

/* Sustain WHITE arrow on 2nd level menu when 4th level menu is hovered */
.dropDownMenu  > li > ul > li > ul > li:hover > a .arrow{
                border-bottom: 4px solid white;
}
</Style>
</head>

<body>

<div class="menuBackground">
    <ul class="dropDownMenu">
        <li><a>Home</a>
            <ul>
                <li><a href="">Home Page 1</a></li>
                <li><a href="">Home Page 2</a></li>
            </ul>
        </li>
        <li><a>Files</a>
            <ul>
                <li><a href="#"><span class="nav-text">&nbsp;&nbsp;2021&nbsp;&nbsp;&nbsp;<i class="arrow right"></i>&nbsp;2023</span></a>
                    ...

`

Answer №1

You have successfully implemented a nested menu structure in HTML and CSS.

Here is the corrected code:

<!DOCTYPE html 
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <style type="text/css"> 

    /* Main Menu Styles */
    .mainMenu {
        background-color: #164149; /* Dark Green */
        color: White;
        float: left;
        text-align: left;
        white-space: nowrap; 
        margin-left: 1px;    
        margin-top: 12px;    
    }

    /* Submenu Text Alignment with Icons */
    .submenu-text {
        display: inline-block;
        vertical-align: -2px;
    }

    /* Create Right Arrow for Year Ranges in Files Menu */
    .arrow {
        width: 0; 
        height: 0; 
        border-left: 4px solid transparent;
        border-right: 4px solid transparent;
        border-bottom: 4px solid black;
        display: inline-block;
        padding-bottom: 5px; 
    }

    /* Transform the arrow to the right orientation */
    .right {
        transform: rotate(90deg);
        -webkit-transform: rotate(90deg);
    }

    .dropdown-menu,
    .dropdown-menu ul {
        list-style: none;    
        margin: 0px;            
        padding: 0px;        
        font-weight: Normal;
        font-family: Calibri;
        font-size: 14px;
    }


    /* Top Level Menu List */
    .dropdown-menu li {
        background-color: #164149; 
        color: White;                    
        position: relative; 
    }

    .dropdown-menu a {
        padding: 12px 20px;
        display: block;
        text-decoration: none;
        background-color: #164149; 
        color: White;
        font-family: Calibri;
        font-weight: Normal;
        font-size: 14px;
    }

    .dropdown-menu a:hover {
        background-color: white; 
        color: black;
    }

    /* Level 1 Dropdown Menu */
    .dropdown-menu > li {
        display: inline-block;
        vertical-align: top;
        margin-left: -4px; 
        background-color: #164149; 
        color: White;
    }

    .dropdown-menu > li:first-child {
        margin-left: 0;
    }

    .dropdown-menu > li > a {
        background-color: #164149;  
        color: White;
    }
    .dropdown-menu > li > a:hover {
        }


    /* Level 2 */
    .dropdown-menu > li > ul {
        text-align: left;
        width: auto; 
        display: none;
        position: absolute;
        top: 100%;
        left: 0;
        z-index: 9999999; 
        font-weight: normal; 
        font-family: Calibri;
        font-size: 14px;
    }

    .dropdown-menu > li:hover > ul {
        display: block;
        background-color: #bddbdb;  
        color: Black;
    }


    .dropdown-menu ul li a {
        background-color: #bddbdb; 
        color: Black;
        padding: 10px 20px;
        font-weight: normal; 
        font-family: Calibri;
        font-size: 14px;
    }

    .dropdown-menu ul li a:hover {
        background-color: #164149; 
        color: White;
    }

    /* Make arrow white on hover of 2nd level menu */
    .dropdown-menu ul li a:hover .arrow{
        border-bottom: 4px solid white;
        }

    .dropdown-menu > li:hover > a {
        background-color: white;  
        color: Black;
    }

    /* Level 3 */
    .dropdown-menu > li > ul > li > ul {
        text-align: left;
        display: none;
        background-color: #bddbdb; 
        color: Black;
        position: absolute;
        left: 100%;
        top: 0;
        z-index: 9999999;
        font-weight: normal; 
        font-family: Calibri;
        font-size: 14px;
    }

    .dropdown-menu > li > ul > li a {
        padding: 10px 20px;
    }

    .dropdown-menu > li > ul > li:hover > ul {
        display: block;
        background-color: #164149; 
        color: White;
    }

    .dropdown-menu > li > ul > li:hover > a {
        background-color: #164149;  
        color: White;
    }

    /* Sustain WHITE arrow when 2nd level menu is hovered over by 3rd level menu */
    .dropdown-menu > li > ul > li:hover > a .arrow{
        border-bottom: 4px solid white;
    }


    /* Level 4 */
    .dropdown-menu  > li > ul > li > ul > li > ul{
        text-align: left;
        display: none;
        background-color: #bddbdb; 
        color: Black;
        position: absolute;
        left: 100%;
        top: 0;
        z-index: 9999999;
        font-weight: normal; 
        font-family: Calibri;
        font-size: 14px;
    }

    .dropdown-menu  >li > ul > li > ul > li a {
        padding: 10px 20px;
    

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

What is the most effective method for establishing a notification system?

My PHP-based CMS includes internal messaging functionality. While currently I can receive notifications for new messages upon page refresh, I am looking to implement real-time notifications similar to those on Facebook. What would be the most efficient ap ...

select specific region within image

I'm currently working on cropping an image and sending the cropped data to the server side. To achieve this, I am utilizing the imgareaselect plugin. While I am able to obtain the coordinates of the selection, I am facing challenges in actually croppi ...

Troubleshooting responsive navigation bar in VueJs: Why are elements not appearing when ToggleButton is clicked?

I'm developing a VueJs single page application and I'm struggling to implement a responsive NavBar. No matter what I try, it just doesn't seem to work as expected. I've experimented with several solutions, and the closest I have come t ...

What is the best method for placing an element above all other elements on a page, regardless of the layout or styles being used?

I want to create a sticky button that remains fixed on the page regardless of its content and styles. The button should always be displayed on top of other elements, without relying on specific z-index values or pre-existing structures. This solution must ...

Fixed and percentage widths in horizontal divs for children, with the percentage width exceeding the desired size

Here's the code snippet I'm working with: http://pastebin.com/W3ggtgZB. The body of this code can be found here: http://pastebin.com/2tkmhnfW. My goal is to create a div containing two child divs. One child div should have a fixed width, while t ...

Rendering data from an API using v-if

Could you help me change the tag that currently displays true or false? I want it to show "FREE" if the event is free and "PAID" if it's not. Check out the Eventbrite API here The response I'm receiving from data.events.is_free is in boolean fo ...

Count of elements in one flexbox row

Having a flexbox container with multiple items in row-wrap format, is there a way to use JavaScript to determine the number of items in each row? Essentially, finding out at what point they wrap. For instance- <div class="container"> <div clas ...

Misalignment of social media buttons is causing display issues

I have arranged my Social Media Div to the right of my Review Div: http://jsfiddle.net/eYQWE/ The issue: the social media buttons in My Social Media div are not staying in a straight line and keep dropping below my Review Div. Do you have any suggestions ...

What is the best way to add "m" to the URL for redirecting mobile devices?

Having already installed a mobile detecting plugin for WordPress, my next step is to create a script that can direct users to the mobile version of the site. My idea is to replicate every desktop page on the mobile subdomain and simply add "m" at the begi ...

Creating a "select all" feature in an HTML multiple select box with jQuery - a step-by-step guide

I'm currently working on an HTML form that includes a multiple select box. I am looking to create a "select all" option within the multiple select box so that when a user clicks on that option, all other options in the select box are automatically sel ...

Rearranging Twitter Bootstrap Grid Columns

Is it possible to rearrange columns in Bootstrap without using col-sm-pull-12/col-sm-push-12? I'm struggling to achieve the desired layout. Are there any alternative methods to accomplish this task? Check out this JSFiddle for reference. <div cla ...

What is the best way to grasp the concepts behind MaterialUI styled components?

Can someone please help me understand how to apply styles to a component using MaterialUI? I've been reading the documentation but it's all very confusing to me. What exactly are classes and how do I connect the const style to the BeerList compon ...

Preserving the HTML tag structure in lxml - What is the best method?

Here is a url link that I have: I am attempting to extract the main body content of this news page using LXML with xpath: //article, however it seems to include content beyond the body tag As LXML modifies the HTML structure upon initialization, I am see ...

Set the background-color of each <td> element to be equal to a value in the array, with each group of three elements having the same

I am trying to color every <td> element in a row of three columns with the same color using the following code: for (var i = 0; itr < $("td").length; i++) { $("td").eq(i).css("background-color", Colors[i]); } However, this code colors each i ...

Is it possible for you to enter "1.00" instead of just 1 when using the input type as number?

I am using Polymer paper-input and I would like to ensure that my input with type "number" always displays 2 decimal points, even when it is a whole number. Instead of just displaying as "1", I want it to be shown as "1.00" at all times. I have tried sett ...

Encountering an issue with retrieving the nth element using Python Selenium

<div class="booking-classes"> <h3 style="text-align: center;"> <p><a href="https://foreupsoftware.com/index.php/booking/20290#/teetimes" style="background-position:0px 0px;"><b> ...

Tips on adding to Jquery html code while maintaining the current CSS styling

My JavaScript function looks like this: function appendAllQna(qnaList, num){ for (var i in qnaList){ var qnaCom = ""; qnaCom += "<div class='scomment scommentLine'>"; if(qnaList[i].sellerYn == "Y"){ ...

json_encode has stopped functioning (now)

Recently, I encountered a strange issue. I've been trying to display chart data, but the JSON_ENCODE function that is supposed to convert my data into JSON format is not returning anything. It was working fine before when I had less data, but now it&a ...

What is the best way to incorporate a new attribute into a particular CSS class?

Just to provide some context, I am relatively new to all of this. Please bear with me. I have a unique CSS class that is specifically assigned to thumbnail images on my Wordpress website. Currently, I am using a script that adds a "Pin it" hover link to a ...

Get a calendar that displays only the month and year

I am looking to incorporate two unique PrimeFaces calendars on a single page. The first calendar will display the date, while the second one will only show the month and year. To achieve this, I have implemented the following JavaScript and CSS specifica ...