Search code examples
htmlcssfrontendnavbar

How to add dropdown menu for every list item


i want to add a dropdown menu for every list item in my navbar when i hover over it I've tried bunch of techniques but every time it ends up ruining the formation or overlaps my list items

<style>
li{
margin-left: -2px;
display: inline-block;
font-size: medium;

}

a{
    display: block;
    text-decoration: none;
    color:rgb(90, 88, 88);
}

a:hover{
    color: black;
}

/* Lowernav */
.lowernav{
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    padding: 0 60px;
    height: 100px;
    width: 100;
    background-color: white;
}

.lowernav-firstpart {
    display: flex;
    justify-content: space-between;
    width: 65%;
    align-items: center;
}

.lowernav ul{

    vertical-align: baseline;

}


.lowernav ul a{
    padding: 0 20px;
}

.lowernav li{
    height: 100px;
    line-height: 100px;
    
    text-align: center;
}

.lowernav li:hover{
    background-color: rgb(246, 247, 249);
}



/* button */
.lowernav button{
    padding: 14px;
    font-size: 14px;
    background-color: rgb(13, 82, 255);
    border: none;
    border-radius: 10px;

}
</style>

My HTML

if you want to run this code make sure to add the logo image. Dimensions (132x30)

<div class="lowernav">
            <div class="lowernav-firstpart">
                <img src="logo.svg" alt="">
                <nav>

here i need to add the dropdown menus for every list item

                    <ul>
                        <li><a href="#">Solutions</a></li>
                        <li><a href="#">Product</a></li>
                        <li><a href="#">Customers</a></li>
                        <li><a href="#">Partners</a></li>
                        <li><a href="#">Resources</a></li>
                        <li><a href="#">Pricing</a></li>
                    </ul>
                </nav>
            </div>
            <button><a><b style="color:white;">CONTACT US</b></a></button>
        </div>

enter image description here


Solution

  • i think you need a nested list, maybe something like this:

    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
      font-family: sans-serif;
    }
    
    .list {
      list-style: none;
      padding: 1rem;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: flex-start;
      gap: 2rem;
      cursor: pointer;
    }
    
    .list__item {
      position: relative;
    }
    
    .sub-list {
      display: none;
      list-style: none;
    }
    
    .list__item:hover {
      color: red;
    }
    
    .list__item:hover .sub-list {
      display: flex;
      flex-direction: column;
      position: absolute;
      top: 1.25rem;
      left: 0;
      color: black;
    }
    <ul class="list">
      <li class="list__item">
        <span class="list__text">One</span>
        <ul class="sub-list">
          <li class="sub-list__item">Apple</li>
          <li class="sub-list__item">Orange</li>
          <li class="sub-list__item">Pear</li>
        </ul>
      </li>
      <li class="list__item">
        <span class="list__text">Two</span>
        <ul class="sub-list">
          <li class="sub-list__item">Pinnaple</li>
          <li class="sub-list__item">Avocado</li>
          <li class="sub-list__item">Salad</li>
          <li class="sub-list__item">Tomatoes</li>
        </ul>
      </li>
      <li class="list__item">
        <span class="list__text">Three</span>
        <ul class="sub-list">
          <li class="sub-list__item">Pumpkin</li>
          <li class="sub-list__item">Kiwi</li>
          <li class="sub-list__item">Banana</li>
        </ul>
      </li>
    </ul>