Steve.
Steve.

Reputation: 3

My dropdown menu is not showing upon hover

I'm having trouble making a dropdown menu. My dropdown menu just won't show upon hover, but if I change my display from none to block in .submenu ul li, the dropdown is in the right place.

Can anyone please help me?

* {
  padding: 0;
  margin: 0;
  text-decoration: none;
  list-style: none;
  box-sizing: border-box;
}

body {
  font-family: 'montserrat';
}

.container {
  height: 80px;
  width: 100%;
  background: #34495e;
  display: block;
  float: right;
}

h1.logo {
  font-size: 35px;
  font-weight: bold;
  color: white;
  padding: 0 40px;
  line-height: 80px;
  float: left;
  width: auto;
  /* border: 1px red solid; */
}

.logo span {
  color: #3498db;
}

nav ul {
  float: right;
  margin-right: 40px;
  /* border: 1px red solid; */
}

nav li {
  display: inline-block;
  margin: 0 5px;
  line-height: 80px;
  text-align: center;
  /* border: 1px red solid; */
}

nav a {
  color: white;
  font-size: 18px;
  text-transform: uppercase;
  padding: 7px 10px;
  border-radius: 3px;
}

a.active,
a:hover {
  border: 1px solid white;
  transition: .5s ease-in;
}

nav #icon {
  color: white;
  font-size: 30px;
  line-height: 80px;
  float: right;
  margin-right: 40px;
  cursor: pointer;
  background: #34495e;
  border: none;
}

@media (min-width: 980px) {
  h1.logo {
    font-size: 32px;
    padding-left: 60px;
  }
  nav ul {
    margin-right: 20px;
  }
  nav a {
    font-size: 17px;
  }
  nav #icon {
    display: none;
  }
  div button {
    display: none;
  }
}

@media(max-width:980px) {
  nav #icon {
    display: block;
  }
  nav ul {
    position: fixed;
    width: 100%;
    height: 100vh;
    background: #2f3640;
    top: 80px;
    left: -100%;
    text-align: center;
    transition: all .5s ease-in;
  }
  .submenu ul {
    top: 100%;
  }
  nav li {
    display: block;
    margin: 50px 0;
    line-height: 30px;
  }
  nav a {
    font-size: 20px;
    text-align: center;
  }
  a.active,
  a:hover {
    border: none;
    color: #3498db;
  }
  #btn1:hover {
    border: none;
    color: #3498db;
  }
  #btn2:hover {
    border: none;
    color: #3498db;
  }
  #btn3:hover {
    border: none;
    color: #3498db;
  }
  nav ul.show {
    left: 0;
  }
}

body {
  background-image: url('../images/workout.jpg');
  background-size: cover;
  height: calc(100vh-80px);
}

div .connect {
  background: #2f364e;
  display: inline-block;
  width: auto;
  height: auto;
}

#btn1 {
  background: #2f364e;
  padding: .5em .7em;
  border: none;
  font-size: 20px;
  color: white;
  margin: 0 5px;
}

#btn2 {
  background: #2f364e;
  padding: .5em .7em;
  border: none;
  font-size: 20px;
  color: white;
  margin: 0 5px;
}

#btn3 {
  background: #2f364e;
  color: white;
  padding: .5em .7em;
  border: none;
  font-size: 20px;
  margin: 0 5px;
}

.submenu ul li {
  display: none;
  background: #34495e;
  border-radius: 6px;
  padding: 0 2px;
  text-align: center;
  opacity: 0.9;
  transition: .5s ease-in;
}

.submenu ul {
  margin: 0;
  padding: 0;
}

.submenu li a {
  margin: 0;
  padding: .5em;
}

.main-nav li :hover .submenu {
  display: block;
}
<header>
  <nav class="container">
    <h1 class="logo">Fitness Fir<span>st</span></h1>
    <nav class="navbar">
      <div class="main-nav">
        <ul>
          <li><a class="active" href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Services</a>
            <div class="submenu">
              <ul>
                <li><a href="#">Membership</a></li>
                <li><a href="#">Coaching</a></li>
              </ul>
            </div>
          </li>
          <li><a href="#">Timetables</a></li>
          <li><a href="#">Contact</a></li>
          <div class="connect" class="button">
            <button id="btn1"><i class="fa fa-facebook"></i></button>
            <button id="btn2"><i class="fa fa-instagram"></i></button>
            <button id="btn3"><i class="fa fa-twitter"></i></button>
          </div>
        </ul>
        <button id="icon"><i class="fa fa-bars" style="font-size:36px"></i></button>
      </div>
    </nav>
  </nav>
</header>

Upvotes: 0

Views: 713

Answers (1)

Dano507
Dano507

Reputation: 61

Issue

In your CSS file, you have only hidden the li elements in your submenu:

.submenu ul li {
  display: none;
  background: #34495e;
  border-radius: 6px;
  padding: 0 2px;
  text-align: center;
  opacity: 0.9;
  transition: .5s ease-in;
}

But here, you're trying to display the .submenu class, rather than the lis inside. In addition, the :hover selector is not attached to the li

.main-nav li :hover .submenu {
  display: block;
}

Fix

To fix this we can change the above selector to the below, so that it is now un-hiding the submenu's lis, when you hover over the parent li (Services button)

.main-nav li:hover .submenu li {
  display: block;
}

Upvotes: 2

Related Questions