当您尝试选择选项时,css下拉菜单会消失

时间:2017-06-23 13:46:40

标签: html css drop-down-menu html-select dropdown

一直试图做一个下拉菜单; 问题是,当试图将鼠标悬停在菜单选项上时,下拉选项会消失 - 但有时只会! (其他时候,罚款!)

代码:



body {
  margin: 0;
}

.container {
  overflow: hidden;
  background-color: rgb(0, 0, 0);
  font-family: Arial;
  height: 60px;
  vertical-align: middle;
}

.container a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 10px 10px;
  text-decoration: none;
  height: 20px;
  vertical-align: middle;
}

.dropdown {
  float: left;
  overflow: hidden;
}

span {
  vertical-align: middle;
}

.dropdown .dropbtn {
  font-size: 20px;
  border: none;
  outline: none;
  color: white;
  padding: 20px 20px;
  background-color: inherit;
}

.redhover a:hover,
.dropdown:hover .dropbtn {
  background-color: red;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

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

<div class="container">
  <a href="index.html"><img src="homeicon.jpg" width="50" alt="Home"></a>
  <div class="redhover">
    <div class="dropdown">
      <button class="dropbtn">GCSEs</button>
      <div class="dropdown-content">
        <a href="chemistry.html">Chemistry</a>
        <a href="biology.html">Biology</a>
      </div>
    </div>
    <div class="dropdown">
      <button class="dropbtn">A-Levels</button>
      <div class="dropdown-content">
        <a href="chemistry.html">Chemistry</a>
        <a href="biology.html">Biology</a>
      </div>
    </div>
    <div class="dropdown">
      <button class="dropbtn">University</button>
      <div class="dropdown-content">
        <a href="chemistry.html">Telecommunications</a>
        <a href="biology.html">Electronic Engineering</a>
      </div>
    </div>
    <div class="dropdown">
      <button class="dropbtn">More</button>
      <div class="dropdown-content">
        <a href="biology.html">About me</a>
        <a href="https://www.youtube.com/channel/UC5GPvJSUO4599z_ugFuoJwg">Youtube</a>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

有什么想法吗?

非常感谢, 瑞恩

3 个答案:

答案 0 :(得分:1)

将.dropdown .dropbtn更改为填充:18px 20px 19px 20px; 由于填充大于您的下拉列表,因此您将退出悬停区域并关闭下拉列表。请参阅带有较小填充的下拉列表示例,因此您不会退出悬停区域

body {
  margin: 0;
}

.container {
  overflow: hidden;
  background-color: rgb(0, 0, 0);
  font-family: Arial;
  height: 60px;
  vertical-align: middle;
}

.container a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 10px 10px;
  text-decoration: none;
  height: 20px;
  vertical-align: middle;
}

.dropdown {
  float: left;
  overflow: hidden;
}

span {
  vertical-align: middle;
}

.dropdown .dropbtn {
  font-size: 20px;
  border: none;
  outline: none;
  color: white;
  padding: 18px 20px 19px 20px;
  background-color: inherit;
}

.redhover a:hover,
.dropdown:hover .dropbtn {
  background-color: red;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.dropdown:hover .dropdown-content {
  display: block;
}
<div class="container">
  <a href="index.html"><img src="homeicon.jpg" width="50" alt="Home"></a>
  <div class="redhover">
    <div class="dropdown">
      <button class="dropbtn">GCSEs</button>
      <div class="dropdown-content">
        <a href="chemistry.html">Chemistry</a>
        <a href="biology.html">Biology</a>
      </div>
    </div>
    <div class="dropdown">
      <button class="dropbtn">A-Levels</button>
      <div class="dropdown-content">
        <a href="chemistry.html">Chemistry</a>
        <a href="biology.html">Biology</a>
      </div>
    </div>
    <div class="dropdown">
      <button class="dropbtn">University</button>
      <div class="dropdown-content">
        <a href="chemistry.html">Telecommunications</a>
        <a href="biology.html">Electronic Engineering</a>
      </div>
    </div>
    <div class="dropdown">
      <button class="dropbtn">More</button>
      <div class="dropdown-content">
        <a href="biology.html">About me</a>
        <a href="https://www.youtube.com/channel/UC5GPvJSUO4599z_ugFuoJwg">Youtube</a>
      </div>
    </div>
  </div>
</div>

答案 1 :(得分:1)

在你的css中删除overflow:hidden;

.container {
  //delete overflow: hidden;
  background-color: rgb(0, 0, 0);
  font-family: Arial;
  height: 60px;
  vertical-align: middle;
}

.dropdown {
  float: left;
  //delete overflow: hidden;
}

因为它已溢出,您的下拉菜单会隐藏文本。删除css中的溢出,或为下拉菜单创建更大的框。

答案 2 :(得分:1)

黑色工具栏高60px,但按钮略高。因此,按钮和下拉列表之间存在小的差距。

添加

.dropdown .dropbtn {
  height: 60px;
  box-sizing: border-box;
}

调整按钮的高度并修复问题:

body {
  margin: 0;
}

.container {
  overflow: hidden;
  background-color: rgb(0, 0, 0);
  font-family: Arial;
  vertical-align: middle;
  height: 60px;
}

.container a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 10px 10px;
  text-decoration: none;
  height: 20px;
  vertical-align: middle;
}

.dropdown {
  float: left;
  overflow: hidden;
}

span {
  vertical-align: middle;
}

.dropdown .dropbtn {
  font-size: 20px;
  border: none;
  outline: none;
  color: white;
  padding: 20px 20px;
  height: 60px;
  box-sizing: border-box;
  background-color: inherit;
}

.redhover a:hover,
.dropdown:hover .dropbtn {
  background-color: red;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.dropdown:hover .dropdown-content {
  display: block;
}
<div class="container">
  <a href="index.html"><img src="homeicon.jpg" width="50" alt="Home"></a>
  <div class="redhover">
    <div class="dropdown">
      <button class="dropbtn">GCSEs</button>
      <div class="dropdown-content">
        <a href="chemistry.html">Chemistry</a>
        <a href="biology.html">Biology</a>
      </div>
    </div>
    <div class="dropdown">
      <button class="dropbtn">A-Levels</button>
      <div class="dropdown-content">
        <a href="chemistry.html">Chemistry</a>
        <a href="biology.html">Biology</a>
      </div>
    </div>
    <div class="dropdown">
      <button class="dropbtn">University</button>
      <div class="dropdown-content">
        <a href="chemistry.html">Telecommunications</a>
        <a href="biology.html">Electronic Engineering</a>
      </div>
    </div>
    <div class="dropdown">
      <button class="dropbtn">More</button>
      <div class="dropdown-content">
        <a href="biology.html">About me</a>
        <a href="https://www.youtube.com/channel/UC5GPvJSUO4599z_ugFuoJwg">Youtube</a>
      </div>
    </div>
  </div>
</div>