CSS下拉菜单导航栏

古达

我有一个包含5个元素的导航栏。其中两个有一个附加的下拉菜单,应在鼠标悬停时显示。问题是当鼠标悬停覆盖下拉菜单的标题和字体颜色(在绿色背景内应该是白色)时我制作的背景。

这是我已经实现的html和CSS:

.nav {
  list-style: none;
  margin: 0;
  padding: 0;
  position: relative;
  text-align: left; /* change to "center" or "right" to align differently */
  border-bottom: 10px solid green; /* bottom border */
  background: #ffffff;
  background: -moz-linear-gradient(top,  #ffffff 0%, #d8d8d8 100%); 
  background: -webkit-gradient(linear, left top, left bottom, color-     stop(0%,#ffffff), color-stop(100%,#d8d8d8));
  background: -webkit-linear-gradient(top,  #ffffff 0%,#d8d8d8 100%);
  background: -o-linear-gradient(top,  #ffffff 0%,#d8d8d8 100%);
  background: -ms-linear-gradient(top,  #ffffff 0%,#d8d8d8 100%);
  background: linear-gradient(to bottom,  #ffffff 0%,#d8d8d8 100%);
 }
.nav ul li{
  display: inline-block;vertical-align:top;
  }
.nav ul a{
  position: relative;
  display: inline-block;
  overflow: hidden;
  color: black; /* font color */
  text-decoration: none;
  padding: 8px 20px;
  font-size: 14px; /* font size */
  font-weight: bold;
  vertical-align: bottom;
 -webkit-transition: color 0.5s; /* transition property and duration */
 -moz-transition: color 0.5s;
 transition: color 0.5s;
 }

ul .nav a::before{
 content: '';
 color: white;
 display: block;
 position: absolute;
 width: 100%;
 height: 100%;
 background: green; /* tab background */
 left: 0;
 top: 110%; /* extra 10% is to account for shadow dimension */
 box-shadow: -2px 2px 10px rgba(255,255,255,.5) inset;
 border-radius: 15px 15px 0 0 / 12px 12px;
 -webkit-transition: top 0.5s; /* transition property and duration */
 -moz-transition: top 0.5s;
 transition: top 0.5s;
}

ul .nav a:hover{
 color: white; 
}
ul .nav a:hover::before{
 top: 0; /* slide tab up */
}

.nav li ul {
  position: absolute;
  display: none;;
  width: inherit;
  margin:0px;
  text-align:center;
  }
  ul li ul li a {
  text-align:center;
 }
 .nav li:hover ul {
   display: block;
  }

  .nav li ul li {
   display: block;  
  }
<div class="nav">
  <ul calss="main_ul">
    <li><a href="#">Home</a></li>
    <li><a href="#">HTML</a>
      <ul>
        <li><a href="#">choice 1</a></li>
        <li><a href="#">choice 2</a></li>
        <li><a href="#">choice 3</a></li>
      </ul>
    </li>
    <li><a class="active" href="#">CSS</a></li>
    <li><a href="#">Javascript</a>
      <ul>
        <li><a href="#">choice 1</a></li>
        <li><a href="#">choice 2</a></li>
        <li><a href="#">choice 3</a></li>
      </ul>
    </li>
    <li><a href="#">jQuery</a></li>
  </ul>
</div>

亚历克斯·杜宾

您的许多组合器都被翻转了。将“ ul .nav”更改为“ .nav ul”,即可解决很多问题。

您可能还需要更改背景“标签”的z-index

添加到.nav:

z-index: 0;

添加到.nav ul a:hover :: before:

z-index: -1;

https://jsfiddle.net/snfe8pvj/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章