0
votes

Whenever I am hovering my mouse over a menu, I want it to show a drop down menu but instead I get this weird glitchy effect, Is there something wrong with my code?

For example if I hover my mouse over menu, the drop down menu will appear but then the main menus will go down down as well until I stop hovering over the main menu.

body {
  text-align: center;
  margin: 0;
  background-color: white;
}

nav {
  position: fixed;
  width: 100%;
  height: 100px;
  background-color: rgba(0, 0, 0, 0.2);
  z-index: 99;
  color: white;
}

nav li {
  display: inline-block;
  list-style: none;
  margin-left: 40px;
  font-size: 25px;
  line-height: 80px;
}

nav a {
  color: white;
  font-family: arial;
  text-decoration: none;
}

nav a:hover {
  border-bottom-color: #29b26b;
  border-bottom-style: solid;
  border-bottom-width: 100%;
}

nav ul li ul li {
  display: none;
  background-color: black;
}

nav ul li:hover ul li {
  display: block;
}
<nav>
  <ul>
    <li><a href="#">Home</a>
      <ul>
        <li><a href="#">Button 1</a></li>
        <li><a href="#">Button 2</a></li>
      </ul>
    </li>

    <li><a href="#">About Us</a>
      <ul>
        <li><a href="#">Button 1</a></li>
        <li><a href="#">Button 2</a></li>
      </ul>
    </li>

    <li><a href="#">The Team</a>
      <ul>
        <li><a href="#">Button 1</a></li>
        <li><a href="#">Button 2</a></li>
      </ul>
    </li>
  </ul>
</nav>
2

2 Answers

0
votes

/* Add these two classses */
nav ul {
  position:relative;
}

nav ul li ul{
  position:absolute;
}

/* Add these two classses ------------ End */

body {
  text-align: center;
  margin: 0;
  background-color: white;
}

nav {
  position: fixed;
  width: 100%;
  height: 100px;
  background-color: rgba(0, 0, 0, 0.2);
  z-index: 99;
  color: white;
}

nav li {
  display: inline-block;
  list-style: none;
  margin-left: 40px;
  font-size: 25px;
  line-height: 80px;
}

nav a {
  color: white;
  font-family: arial;
  text-decoration: none;
}

nav a:hover {
  border-bottom-color: #29b26b;
  border-bottom-style: solid;
  border-bottom-width: 100%;
}

nav ul li ul li {
  display: none;
  background-color: black;
}

nav ul li:hover ul li {
  display: block;
}
<nav>
  <ul>
    <li><a href="#">Home</a>
      <ul>
        <li><a href="#">Button 1</a></li>
        <li><a href="#">Button 2</a></li>
      </ul>
    </li>

    <li><a href="#">About Us</a>
      <ul>
        <li><a href="#">Button 1</a></li>
        <li><a href="#">Button 2</a></li>
      </ul>
    </li>

    <li><a href="#">The Team</a>
      <ul>
        <li><a href="#">Button 1</a></li>
        <li><a href="#">Button 2</a></li>
      </ul>
    </li>
  </ul>
</nav>

Issue Here is When you make the inside dropdowm ul as display:block on hover. It is pushing the remaining li items down.

You Should set the dropdown ul position to absolute and make the parent ul position as relative

0
votes

Maybe something like this? I have put comments inside the code.

/* CSS */
body
{
    text-align: center;
    margin: 0;
    background-color: grey;
}


nav
{
    position: fixed;
    width: 100%;
    height: 100px;
    background-color: rgba(0, 0, 0, 0.2);
    z-index: 99;
    color: white;
}


nav li
{
    display: inline-block;
    list-style: none;

    margin-left: 40px;
    font-size: 25px;
    line-height: 80px;
}

nav a
{
    color: white;
    font-family: arial;
    text-decoration: none;
}

nav a:hover
{
    border-bottom-color: #29b26b;
    border-bottom-style: solid;
    border-bottom-width: 100%;
}

nav ul li  {
 /* Important to make sub elements be positioned as absolute*/
  position: relative;
}


nav ul li ul
{
  position: absolute;
    display: none;
    background-color: black;
    /* Width is derived from width of parent nav ul, You need to set it here*/
    width: 200px; 

}

/* Show the whole ul part and not individual li elements*/
nav ul li:hover ul
{
    display: block;
}
<nav>
        <ul>
            <li><a href="#">Home</a>
                <ul>
                    <li><a href="#">Button 1</a></li>
                    <li><a href="#">Button 2</a></li>
                </ul>
            </li>

            <li><a href="#">About Us</a>
                <ul>
                    <li><a href="#">Button 1</a></li>
                    <li><a href="#">Button 2</a></li>
                </ul>
            </li>

            <li><a href="#">The Team</a>
                <ul>
                    <li><a href="#">Button 1</a></li>
                    <li><a href="#">Button 2</a></li>
                </ul>
            </li>
        </ul>
    </nav>