【问题标题】:CSS dropdown menue Navigation barCSS 下拉菜单 导航栏
【发布时间】:2016-05-08 19:10:40
【问题描述】:

我有一个包含 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>

【问题讨论】:

  • 还可以调整下拉菜单的宽度,这里的下拉标题是演示jsfiddle.net/38q2d3aw
  • 我真的很想理解你的文字,但我不能。我不明白你想说什么:“问题是当鼠标悬停在下拉菜单的标题上时我制作的背景,字体颜色应该是绿色背景中的白色。”
  • 抱歉不清楚英语不是我的母语,我将鼠标悬停在“.main_ul”上时显示为绿色背景色,此背景隐藏了“.main_ul”的字体-css部分中有代码ul .nav a::before { content = " "} 我不知道我应该在引号内放什么。
  • 这个 url 可能有帮助 dynamicdrive.com/style/csslibrary/item/slide_up_tabs 我在这里需要相同的导航栏示例,但在 ul 下有下拉菜单

标签: html css


【解决方案1】:

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

您可能还需要更改背景“选项卡”的 z-index

添加到.nav:

z-index: 0;

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

z-index: -1;

https://jsfiddle.net/snfe8pvj/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    相关资源
    最近更新 更多