Search code examples
htmlcsshoverpseudocode

I cannot make my hover class works. How can I make it work?


This is my html code, and the lower one is css code. I really need help :( :hover class does not work at all. Did I code something wrong?

html {
  box-sizing: border-box;
}

a {
  text-decoration: none;
  color: black;
}

.nav_bar>li {
  padding: 20px;
  flex-basis: 0;
  flex-grow: 1;
  background-color: #ffffa8;
  height: 1rem;
  text-align: center;
  flex-direction: column;
}

#Homesub {
  display: none;
}

#Home:hover #Homesub {
  display: list-item;
}
<nav>
  <ul class="nav_bar">
    <li>
      <a href="Hme.html" id="Home">Home</a>
      <ul class="sub">
        <li><a href="Members.html" id="Homesub">Members</a></li>
      </ul>
    </li>
  </ul>
</nav>


Solution

  • You are targeting the anchor, not the li

    Seems like you should be targeting the ul.

    html {
      box-sizing: border-box;
    }
    
    a {
      text-decoration: none;
      color: black;
    }
    
    .nav_bar>li {
      padding: 20px;
      flex-basis: 0;
      flex-grow: 1;
      background-color: #ffffa8;
      height: 1rem;
      text-align: center;
      flex-direction: column;
    }
    
    li > ul {
      display: none;
    }
    
    li:hover > ul {
      display: block;
    }
    <nav>
      <ul class="nav_bar">
        <li>
          <a href="Hme.html" id="Home">Home</a>
          <ul class="sub">
            <li><a href="Members.html" id="Homesub">Members</a></li>
          </ul>
        </li>
      </ul>
    </nav>