Syed Muhammad Iftikhar Syed Muhammad Iftikhar - 2 months ago 6
CSS Question

Drop down menu not showing sub menus

Here is the code for my dropdown button with 1 additional level.

When I click on button it works fine but when I try to click inside the dropdown for submenu so I realized something is wrong a all my four items showing the 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

So this is what I changed in your code:

  1. You have invalid syntax- you can't have multiple ids- use classes for that.

    So I changed dropdown-submenu to a class.

  2. And instead of .dropdown-content:hover #dropdown-submenu use this:

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

    The adjacent sibling selector(+) selects dropdown-submenu that immediately follows the menu a tag.

Adjacent sibling selector (+) that selects the sibling element that immediately follows. See here and here for examples.

Let me know your feedback on this. Thanks!

.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>

Comments