Home > Enterprise >  Dropdown menu not displaying correctly when hovering over
Dropdown menu not displaying correctly when hovering over

Time:03-26

The dropdown menu appeared just fine. But the when hovering over the "Mobile" and "Email" link the submenu got shifted to the right. Here is a CodePen link and below the code snippet :

 div > ul > li {
      list-style: none;
      display: inline-block;
      margin: 0 20px;
      font-weight: bolder;
      font-size: large;
      font-variant: small-caps;
      position: relative;
    }
    ul > li > a {
      text-decoration: none;
      color: white;
    }
    .sub-menu {
      position: absolute;
      display: none;
    }
    ul > li:hover > .sub-menu {
      display: block;
      background-color: white;
      margin: 0px;
      padding-left: 10px;
      width: 400px;
      height: 200px;
      box-shadow: 0 0 10px;
      border-radius: 10px;
      list-style-type: none;
      text-align: left;
    }
    .sub-menu > li > a {
      color: orangered;
      padding: 0 10px;
    
    }
    .sub-menu > li:hover > a:hover {
      color: white;
      background-color: orangered;
      padding: 0 200px;
      margin: 0 200px;
      border-radius: 10px;
      float: left;
    }
    ul > li > a:hover {
      color: yellow;
    }
    div {
      background-color: orangered;
      height: 100px;
      text-align: center;
      line-height: 100px;
    }
    
 
<div>
  <ul>
    <li><a href="#">Home </a></li>
    <li><a href="#">Intro </a></li>
    <li>
      <a href="#">Contact </a>
      <ul >
        <li><a href="#">Mobile</a></li>
        <li><a href="#">Email</a></li>
      </ul>
    </li>
    <li><a href="#">Help </a></li>
    <li><a href="#">Q&A </a></li>
  </ul>
</div>

    

   

I did try to put the submenu in another <div\> but the result was still the same (or maybe I'm just stupid).

CodePudding user response:

the issue come from the selector .sub-menu > li:hover > a:hover where you put padding and margin on hover

i also delete the float but i let you give item your expected design

.sub-menu > li:hover > a:hover {
  color: white;
  background-color: orangered;
  border-radius: 10px;
}

div > ul > li {
  list-style: none;
  display: inline-block;
  margin: 0 20px;
  font-weight: bolder;
  font-size: large;
  font-variant: small-caps;
  position: relative;
}
ul > li > a {
  text-decoration: none;
  color: white;
}
.sub-menu {
  position: absolute;
  display: none;
}
ul > li:hover > .sub-menu:hover {
   background: orange;
}
ul > li:hover > .sub-menu {
  display: block;
  background-color: white;
  margin: 0px;
  padding-left: 10px;
  width: 400px;
  height: 200px;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  list-style-type: none;
  text-align: left;
}
.sub-menu > li > a {
  color: orangered;
  padding: 0 10px;

}
.sub-menu > li:hover > a:hover {
  color: white;
  background-color: orangered;
  border-radius: 10px;
}
ul > li > a:hover {
  color: yellow;
}

div {
  background-color: orangered;
  height: 100px;
  text-align: center;
  line-height: 100px;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="CSS/Bai2.css" />
  </head>
  <body>
    <div>
      <ul>
        <li><a href="#">Home </a></li>
        <li><a href="#">Intro </a></li>
        <li>
          <a href="#">Contact </a>
          <ul >
            <li><a href="#">Mobile</a></li>
            <li><a href="#">Email</a></li>
          </ul>
        </li>
        <li><a href="#">Help </a></li>
        <li><a href="#">Q&A </a></li>
      </ul>
    </div>
  </body>
</html>

If you want to have background color on one sub-menu line when you hover <a> one way can be to use selector .sub-menu>li:hover

div>ul>li {
  list-style: none;
  display: inline-block;
  margin: 0 20px;
  font-weight: bolder;
  font-size: large;
  font-variant: small-caps;
  position: relative;
}

ul>li>a {
  text-decoration: none;
  color: white;
}

.sub-menu {
  position: absolute;
  display: none;
}

ul>li:hover>.sub-menu:hover {
  background: orange;
}

ul>li:hover>.sub-menu {
  display: block;
  background-color: white;
  margin: 0px;
  padding-left: 0;
  z-index: 9;
  width: 400px;
  height: 200px;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  list-style-type: none;
  text-align: left;
}

.sub-menu>li>a {
  color: orangered;
  padding: 0 10px;
}

.sub-menu>li:hover {
  background-color: orangered;
  color: yellow;
  border-radius: 10px;
  border: solid 1px;
}

.sub-menu>li:hover>a {
  color: white;
}

ul>li>a:hover {
  color: yellow;
}

div {
  background-color: orangered;
  height: 100px;
  text-align: center;
  line-height: 100px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <link rel="stylesheet" href="CSS/Bai2.css" />
</head>

<body>
  <div>
    <ul>
      <li><a href="#">Home </a></li>
      <li><a href="#">Intro </a></li>
      <li>
        <a href="#">Contact </a>
        <ul >
          <li><a href="#">Mobile</a></li>
          <li><a href="#">Email</a></li>
        </ul>
      </li>
      <li><a href="#">Help </a></li>
      <li><a href="#">Q&A </a></li>
    </ul>
  </div>
</body>

</html>

  • Related