Html Css Beginner创建下拉导航栏菜单

时间:2016-10-06 10:24:25

标签: html css

我是css& amp;的初学者html和创建在导航栏中创建下拉菜单时遇到了问题。在下拉菜单中"产品"单击时它不会显示下拉菜单列表。如果有人能够指出这个问题,我将非常感激。以下是导航栏的HTML代码:



.Navigation {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #4C4747;
  border: 2px;
  border-radius: 5px;
}
.Navigation li {
  list-style-type: none;
  float: left;
}
.Navigation li a {
  border-right: 2px solid black;
  font-family: sans-serif;
  color: white;
  display: block;
  text-align: center;
  text-decoration: none;
  padding: 10px 15px 10px 15px;
}
.dropdown li {
  position: relative;
  display: inline-block;
}
.dropdown-content {
  display: none;
  position: absolute;
}
.dropdown-content a {
  color: white;
  text-decoration: none;
  display: block;
}
.dropdown:hover .dropbtn {
  display: block;
}

<ul class="Navigation">
  <li><a href="Home.html">HOME</a>
  </li>

  <!-- Dropdown -->
  <li class="dropdown">
    <a href="Product.html" class="dropbtn">PRODUCT</a>
    <div class="dropdown-content">
      <a href="Computer.html">COMPUTER</a>
      <a href="Laptop.html">LAPTOP</a>
      <a href="Smartphone.html">SMARTPHONE</a>
    </div>
  </li>
  <!--  -->
  <li><a href="#news">ABOUT</a>
  </li>
</ul>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您需要将.dropbtn改为.dropdown-content,如下所示:

.dropdown:hover .dropdown-content{
    display: block;
}

以下是代码片段:

.Navigation {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #4C4747;
    border: 2px;
    border-radius: 5px;
}

.Navigation li {
    list-style-type: none;
    float: left;
}

.Navigation li a {
    border-right: 2px solid black;
    font-family: sans-serif;
    color: white;
    display: block;
    text-align: center;
    text-decoration: none;
    padding: 10px 15px 10px 15px;
    }

.dropdown li {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
}

.dropdown-content a {
    color: white;
    text-decoration: none;
    display: block;
    background-color: #4c4747;
}
.dropdown:hover .dropdown-content{
    display: block;
}
<ul class="Navigation">
<li><a href="Home.html">HOME</a></li>

          <!-- Dropdown -->
    <li class="dropdown">
    <a href="Product.html" class="dropbtn">PRODUCT</a>
        <div class="dropdown-content">
           <a href="Computer.html">COMPUTER</a>
           <a href="Laptop.html">LAPTOP</a>
           <a href="Smartphone.html">SMARTPHONE</a>
         </div>
    </li>
            <!--  -->
    <li><a href="#news">ABOUT</a></li>
</ul>

答案 1 :(得分:0)

你可以尝试这个:

.Navigation {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #4C4747;
    border: 2px;
    border-radius: 5px;
}

.Navigation li {
    list-style-type: none;
    float: left;
}

.Navigation li a {
    border-right: 2px solid black;
    font-family: sans-serif;
    color: white;
    display: block;
    text-align: center;
    text-decoration: none;
    padding: 10px 15px 10px 15px;
    }

.dropdown li {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
     background-color: #4C4747;
}

.dropdown-content a {
    color: white;
    text-decoration: none;
    display: block;
}
.dropdown-content a:hover
{
  background-color:red;
}
.dropdown:hover .dropdown-content{
    display: block;
}

DEMO HERE

答案 2 :(得分:0)

您需要将.dropbtn更改为.dropdown-content,因为当鼠标悬停在display: block;上时,即使该按钮仍在显示,该按钮也会显示(带有.dropdown) 。当您将鼠标悬停在.dropdown上时,您希望显示内容。因此,将其更改为此-

.dropdown:hover .dropdown-content {display: block;}