Home > database >  How to make this dropdown work using flexbox?
How to make this dropdown work using flexbox?

Time:01-21

I am trying to make this dropdown button using flexbox but it's not working as I want. It's not displaying when I hover around the button using display properties. Can someone edit this code so that the dropdown menu works? HTML

enter image description here

CodePudding user response:

Is this what you want

function Enable(){
    x = document.getElementById("Ulist");
    if(x.className == "display"){
   x.className  = " active";
   }else{
   x.className == "display";
   }
}
li{
  list-style: none;
  
}

.Maincover{
   display: flex;
   flex-direction: column;

   align-items: center;
   height: 300px;
}


button{
 font-family: montserrat;
 font-weight: 500;
 width: 100px;
 border-radius: 5px;
 color: white;
 border: 0px;
 padding: 5px 10px;
 text-align: center;
 background-color: tomato;

}

.list{
  font-family: rubik;
  text-align: center;
  margin-top: 10px;

}
.display{
  padding:0px;
  display: none;
}

.display.active{
  display: block;
}
.hoverbutton:hover ~ .display{
 display: block;
}
<div >
<button  onclick="Enable()">Hover Here</button>
<ul  id="Ulist">
<li  id="lists">List item 1</li>
<li  id="lists">List item 2</li>
<li  id="lists">List item 3</li>
</ul>
</div>

CodePudding user response:

Here I have attached the html and css code for hoverable dropdown menu using flex.

.menu-area{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  width:150px;
  display:flex;
  flex-direction:column;
  justify-content:center;
}
.menu-area a{
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  background: darkslategrey;
  height: 60px;
  font-family:Poppins;
  font-weight: bold;
  text-decoration: none;
  

}
.menu-area a:hover{
  background: tomato;
  color: #fff;
}
.menu-area ul{
  list-style: none;
  display: flex;
}
.menu-area li{
  position: relative;
  width: 100%;
  text-align: center;
}
.menu-area li:hover .dropdown-1 > li{
  display: block;
  top: 0;
}
.dropdown-1 li{
  display: none;
  position: relative;
}
.dropdown-1{
  position: absolute;
  display: flex;
  flex-direction: column;
  width: 100%;
}
.dropdown-1 li:hover .dropdown-2 li{
  display: block;
}
.dropdown-2 li{
  display: none;
}
.dropdown-2{
  display: flex;
  flex-direction: column;
  position: absolute;
  top: 0;
  left: 100%;
  width: 100%;
}
.banner-area{
  background-image: linear-gradient(rgba(0,0,0,0.5),rgba(0,0,0,0.5)),url(https://i.postimg.cc/Y0YM36yL/aa.jpg);
  -webkit-background-size:cover;
  background-size:cover;
  background-position: center center;
  display: flex;
  align-items: center;
  justify-content: center;
  height: calc(100vh - 60px);
}
.banner-area h1 {
    text-transform: uppercase;
    color: #fff;
    font-size: 100px;
    font-family: poppins;
    letter-spacing: 2px;
}
 <div >
  <ul>
    <li><a href="#">Dropdown</a>
      <ul >
        <li><a href="#">service one</a></li>
        <li><a href="#">service two</a></li>
        <li><a href="#">service three</a>
        </li>
      </ul>
    </li>
  </ul>
</div>

  •  Tags:  
  • Related