Sub menus are not appearing in the drop-down menu

Below is the code for my dropdown button with an additional level.

Initially, the button works as intended. However, upon attempting to click within the dropdown submenu, I noticed that all four items displayed the same last submenu only.

.icon-cadet-left {
  float: right;
}
.dropbtn {
  width: 300px;
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-family: 'Play';
  font-size: 20px;
  border: none;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 2px;
}
.dropdown {
  position: relative;
  display: inline-block;
}
.dropdown-content {
  display: none;
  position: relative;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
  width: 300px;
  color: black;
  font-family: 'Play';
  letter-spacing: 2px;
  padding: 12px 16px;
  text-decoration: none !important;
  display: block;
  text-align: center;
  border-bottom: 0.1em solid #e1e1e1;
  border-radius: 5%;
  text-transform: uppercase;
  overflow: hidden;
}
#dropdown-submenu {
  display: none;
  background-color: #f9f9f9;
  position: absolute;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  left: 100%;
  top: 0px;
}
#dropdown-submenu a {
  z-index: 1000;
  width: 300px;
  color: black;
  font-family: 'Play';
  letter-spacing: 2px;
  padding: 12px 16px;
  text-decoration: none !important;
  display: block;
  text-align: center;
  border-bottom: 0.1em solid #e1e1e1;
  border-radius: 5%;
  text-transform: uppercase;
}
.dropdown-content a:hover {
  background-color: #f1f1f1
}
#dropdown-submenu a:hover {
  background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
  display: block;
}
.dropdown-content:hover #dropdown-submenu {
  display: block;
}
.dropdown:hover .dropbtn {
  background-color: #3e8e41;
}
<div class="pricing-button">
  <div class="dropdown">
    <button class="dropbtn">Logo Design</button>
    <div class="dropdown-content animated fadeIn">
      <a class="span1" href="#">Logo & Identity<i class="fa fa-caret-right icon-cadet-left"></i></a>
      <div id="dropdown-submenu">
        <a>Logo Design</a>
        <a>Business Cards</a>
        <a>Sationary</a>
        <a>Holiday Doodles</a>
      </div>
      <a class="hover2" href="#">Business & Advertising<i class="fa fa-caret-right icon-cadet-left"></i></a>
      <div id="dropdown-submenu">
        <a>Sample 01</a>
        <a>Sample 02</a>
        <a>Sample 03</a>
        <a>Sample 04</a>
      </div>
      <a class="hover3" href="#">Website & Digitals<i class="fa fa-caret-right icon-cadet-left"></i></a>
      <div id="dropdown-submenu">
        <a>Sample 01</a>
        <a>Sample 02</a>
        <a>Sample 03</a>
        <a>Sample 04</a>
      </div>
      <a class="hover4" href="#">Textile Designing<i class="fa fa-caret-right icon-cadet-left"></i></a>
      <div id="dropdown-submenu">
        <a>Sample 01</a>
        <a>Sample 02</a>
        <a>Sample 03</a>
        <a>Sample 04</a>
      </div>
    </div>
  </div>
</div>

Answer №1

Here are the modifications I made to your code:

  1. I corrected the invalid syntax by replacing id with class.

    Therefore, I updated dropdown-submenu to a class.

  2. Instead of using

    .dropdown-content:hover #dropdown-submenu
    , try this:

    .dropdown-content a:hover + .dropdown-submenu {
        display: block;
    }
    

    The adjacent sibling selector (+) targets the dropdown-submenu following the menu a tag directly.

The adjacent sibling selector (+) selects the immediate sibling element. Visit here and here for detailed examples.

Please provide feedback on these changes. Thank you!

.icon-cadet-left {
  float: right;
}
.dropbtn {
  width: 300px;
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-family: 'Play';
  font-size: 20px;
  border: none;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 2px;
}
.dropdown {
  position: relative;
  display: inline-block;
}
.dropdown-content {
  display: none;
  position: relative;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
  width: 300px;
  color: black;
  font-family: 'Play';
  letter-spacing: 2px;
  padding: 12px 16px;
  text-decoration: none !important;
  display: block;
  text-align: center;
  border-bottom: 0.1em solid #e1e1e1;
  border-radius: 5%;
  text-transform: uppercase;
  overflow: hidden;
}
.dropdown-submenu {
  display: none;
  background-color: #f9f9f9;
  position: absolute;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  left: 100%;
  top: 0px;
}
.dropdown-submenu a {
  z-index: 1000;
  width: 300px;
  color: black;
  font-family: 'Play';
  letter-spacing: 2px;
  padding: 12px 16px;
  text-decoration: none !important;
  display: block;
  text-align: center;
  border-bottom: 0.1em solid #e1e1e1;
  border-radius: 5%;
  text-transform: uppercase;
}
.dropdown-content a:hover {
  background-color: #f1f1f1
}
.dropdown-submenu a:hover {
  background-color: #f1f1f1
}
.dropdown:hover .dropdown-content {
  display: block;
}
.dropdown-content a:hover + .dropdown-submenu {
  display: block;
}
.dropdown:hover .dropbtn {
  background-color: #3e8e41;
}
<div class="pricing-button">
  <div class="dropdown">
    <button class="dropbtn">Logo Design</button>
    <div class="dropdown-content animated fadeIn">
      <a class="span1" href="#">Logo & Identity<i class="fa fa-caret-right icon-cadet-left"></i></a>
      <div class="dropdown-submenu">
        <a>Logo Design</a>
        <a>Business Cards</a>
        <a>Sationary</a>
        <a>Holiday Doodles</a>
      </div>
      <a class="hover2" href="#">Business & Advertising<i class="fa fa-caret-right icon-cadet-left"></i></a>
      <div class="dropdown-submenu">
        <a>Sample 01</a>
        <a>Sample 02</a>
        <a>Sample 03</a>
        <a>Sample 04</a>
      </div>
      <a class="hover3" href="#">Website & Digitals<i class="fa fa-caret-right icon-cadet-left"></i></a>
      <div class="dropdown-submenu">
        <a>Sample 01</a>
        <a>Sample 02</a>
        <a>Sample 03</a>
        <a>Sample 04</a>
      </div>
      <a class="hover4" href="#">Textile Designing<i class="fa fa-caret-right icon-cadet-left"></i></a>
      <div class="dropdown-submenu">
        <a>Sample 01</a>
        <a>Sample 02</a>
        <a>Sample 03</a>
        <a>Sample 04</a>
      </div>
    </div>
  </div>
</div>

Answer №2

I've noticed that your code contains multiple instances of id="dropdown-submenu," which is not recommended. The id attribute should be unique for each HTML element within the document. If you need to use this identifier multiple times, consider using class="dropdown-submenu" instead and reference it with .dropdown-submenu rather than #dropdown-submenu.

Please address the issue with your id attributes before proceeding. Once corrected, everything should work as intended.

Thank you for your attention to this matter!

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

How to insert an SVG into a <span> element using jQuery's find() method

I am encountering challenges when trying to select an svg element (originally written as img and later converted to svg using jQuery). Here is the code snippet: HTML: <div class="tab-content"> <div id="personas" class="active"> <s ...

Utilizing AJAX to upload a file and managing it through a Java servlet on the server side

I've been struggling with this particular issue for the past few days and haven't found any helpful resources elsewhere. I'm attempting to upload a file through an HTML form that includes other input fields: var formData = new FormData() ...

Resetting the width of a CSS-styled div to its default maximum width

I have a set of automatically generated div containers that contain label and data information. For example: <div class="display-label"> @Html.DisplayNameFor(model => model.BusinessPhone) </div> <div class="display-field"> @H ...

Creating a visually appealing label by customizing it according to the child div

Can the label be styled based on whether the input is checked or not using CSS, or do I have to use JavaScript? <label class="filterButton"> <input name="RunandDrive" type="checkbox" value="1"> </label> ...

Embed a Telegram account onto an HTML page using the <iframe> element

Is there a way to display the personal Telegram account in an iframe tag? I experimented with the code below, but unfortunately it did not produce the desired result: <iframe src="https://telegram.me/joinchat/BfNEij9CbDh03kwXacO5OA"></iframe> ...

What is the method used to calculate the heights of flex items when the flex-direction property is set to column?

I'm currently grappling with the concept of flexbox layout and have been exploring the flex-direction: column option. HTML <div class="container"> <div class="item"> Lorem ipsum dolor sit amet consectetur a ...

What is the best way to display a date picker from a different page as a pop-up when a button is clicked on the current

Chapter One <button class="btn btn-outline-primary btn-sm btn-block " >Place order</button> Chapter Two <button class="simplepicker-btn" >Show Date Picker</button> > <script> > let simplepicker = new SimpleP ...

What is the best approach to modifying the color of a menu item in the sidebar upon clicking it using Material-UI in a React application?

I am a beginner with this framework and my goal is to change the color when clicking on a Menu Item in the Sidebar. For example, clicking on the table component should change the table name and icon to white color. Could someone please help me figure out h ...

I desire the div to display based on the conditions set in the PHP file

Hi, I am looking to display the div with ID "msga" when a certain condition is met and show the div with ID "msgb" when another condition is met. Here is my PHP code: if(isset($_POST['aa'])) { echo "a"; } if (isset($_POST['bb']))) { ...

The inclusion of the <div> tag disrupts the functionality of the Material UI Grid

While attempting to enclose my element within a <div> that is nested inside the <Grid>, I realized that this was causing the <Grid> layout to break! Surprisingly, when I tried the same approach with Bootstrap grid system, this issue did n ...

I am running into issues getting Tailwind CSS to work in my project. Despite following the installation instructions in the documentation and trying to learn this new CSS framework, it doesn't seem to

//I followed the instructions in the tailwind documentation to install and set up everything. However, when I try to use tailwind utility classes in my HTML file, they don't seem to work. Can someone please assist me with this issue? // Here is my sr ...

Selenium - pausing for scroll completion

I am currently working on automating downloads from a webpage using the selenium tool. My approach involves creating a firefox driver that opens the page and clicks the download button. However, I have encountered an issue where the button needs to be vis ...

Python code encountering issues within Django framework

I'm having trouble showing and hiding HTML elements based on the data I receive after clicking a button. The output of another Python file will generate this data, but the if statement doesn't seem to be working as expected: {% if data == 'e ...

Display all attribute connections for a product in Prestashop

I have updated the products in my shop to include different combinations. These combinations involve a 'packaging' attribute with two options: a 100 units box and a 200 units box, each with its own unique reference number. On the product page, t ...

Connecting two fields with a line on click in AngularJS

In the top section, there are 10 fields and in the bottom section, there are another 10 fields. I want to be able to connect two fields with a line when they are clicked (one from the top section and one from the bottom section). This connection should app ...

How can I align two inner boxes vertically in the most efficient manner?

.dd{ height:100px; width:100%; border:1px soild red; background:#000; } .dd .d1{ height:20px; width:20px; border:1px solid green; display:inline-block; } .dd .d2{ height:20px; width:20px; border:1px solid green; display:inl ...

Embed a script into an iframe from a separate domain

While experimenting, I attempted to inject a script inside an iframe element using the following method. However, since the child and parent elements do not belong to the same domain (and I am aware that XSS is prevented in modern browsers), I was wonder ...

Switching up icons using React Spring - a step-by-step guide!

Is it possible to change the icon when I click on it using react spring? For example, if I click on " ...

Step-by-step guide on displaying a tag image in HTML using html2canvas

html2canvas($('#header'), { allowTaint: true, onrendered: function (canvas) { var imgData = canvas.toDataURL("image/png"); console.log(imgData); } }); click here for an example ...

Tips for locating the initial <body .....> tag within a text using PHP?

Struggling with the preg_match function, especially when it comes to finding the first body tag. I've attempted to locate the tag in various formats: <body class="blah"> <body style="blah: blahblah;"> <body> Successfully found the ...