简体   繁体   English

CSS 顶部栏下拉 hover 没有工作

[英]CSS Topbar dropdown hover not wroking

I'm new to designing a web.我是设计 web 的新手。 Recently, I'am trying to design my web and putting Topbar with dropdown on hover, but it's not working.最近,我正在尝试设计我的 web 并将带有下拉菜单的 Topbar 放在 hover 上,但它不起作用。 I'm using tutorial from W3School.我正在使用 W3School 的教程。 I put my code below, maybe someone can tell me what did I do wrong.我把我的代码放在下面,也许有人可以告诉我我做错了什么。 Thank you谢谢

 .dropdown { float: left; overflow: hidden; }.dropdown.dropbtn { border: none; outline: none; padding: 14px 20px; margin: 0; }.dropdown-content { display: none; position: absolute; min-width: 160px; z-index: 1; }.dropdown:hover.dropdown-content { display: block; }.dropdown-content a { float: none; color: black; padding: 12px 16px; text-decoration: none; display: block; text-align: left; }
 <div class="navbar"> <a class="active" href="/try/front.php">Logo</a> <a class="active" href="rightMain.php">Name</a> <a class="active" href="rightPage1.php">Menu1</a> <div class="dropdown"> <button class="dropbtn">Dropdown</button> <div class="dropdown-content"> <a href="#">Link 1</a> <a href="#">Link 2</a> </div> </div> </div>

You need to add the :hover selector to the links in your dropdown.您需要将:hover选择器添加到下拉列表中的链接。

In your case, simply add the following to your stylesheet.在您的情况下,只需将以下内容添加到您的样式表中。

.dropdown-content a:hover{
  background: blue;
}

Full code snippet:完整的代码片段:

 .dropdown { float: left; overflow: hidden; }.dropdown.dropbtn { border: none; outline: none; padding: 14px 20px; margin: 0; }.dropdown-content { display: none; position: absolute; min-width: 160px; z-index: 1; }.dropdown:hover.dropdown-content { display: block; }.dropdown-content a { float: none; color: black; padding: 12px 16px; text-decoration: none; display: block; text-align: left; }.dropdown-content a:hover{ background: blue; }
 <div class="navbar"> <a class="active" href="/try/front.php">Logo</a> <a class="active" href="rightMain.php">Name</a> <a class="active" href="rightPage1.php">Menu1</a> <div class="dropdown"> <button class="dropbtn">Dropdown</button> <div class="dropdown-content"> <a href="#">Link 1</a> <a href="#">Link 2</a> </div> </div> </div>

It's solved, it was because I put fixed navbar, and when I remove it, the dropdown is working.解决了,这是因为我放置了固定的导航栏,当我删除它时,下拉菜单正在工作。

The code before was:之前的代码是:

.navbar {
  overflow: hidden;
  background-color: #333;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
}

.navbar a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 20px;
  text-decoration: none;
}

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

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

.navbar a:hover, .dropdown:hover .dropbtn {
  background: #ddd;
  color: black;
 }

.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: blue;
}

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

Then it become然后就变成了

.navbar {
  overflow: hidden;
  background-color: #333;
}

.navbar a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 20px;
  text-decoration: none;
}

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

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

.navbar a:hover, .dropdown:hover .dropbtn {
  background: #ddd;
  color: black;
 }

.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: blue;
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM