带有 HTML 和 CSS 的多级悬停导航栏

Posted

技术标签:

【中文标题】带有 HTML 和 CSS 的多级悬停导航栏【英文标题】:Multi-level Hover Navigation Bar with HTML and CSS 【发布时间】:2020-11-26 22:09:32 【问题描述】:

所以我正在查看 w3schools.com 上的代码,其中有一些很好的示例,我正在尝试对这一切做出正面或反面。但他们没有提供关于自定义第三级菜单代码的说明。有人可以帮我分解这段代码,以便我可以随意添加多级菜单吗?

<!doctype html>
<html>
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<title>Title</title>
<style>
body 
  font-family: Arial, Helvetica, sans-serif;


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


.navbar a 
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;


.dropdown 
  float: left;
  overflow: hidden;


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


.navbar 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;

</style>
</head>
<body>

<div class="navbar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown 
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div> 
</div>

<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>



</body>
</html>

也有人可以在这里向我解释这段代码在哪里以及如何告诉浏览器导航栏的整体宽度。我在这里尝试了其他人的代码,导航栏仅停留在 150 像素。当我更改宽度值时,它全是错误的。所以我知道我只是不明白这是在代码中确定的。

更新:感谢其中一个答案,我已经弄清楚如何进入第三级菜单,并通过让菜单弹出到右侧来摸索。现在我的问题是它扩展了“Link 3”下的下拉菜单,就好像它为它下面的链接腾出了空间。我将如何删除它?这是我当前的代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<title>Title</title>
<style>
body 
  font-family: Arial, Helvetica, sans-serif;


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


.navbar a 
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;


.dropdown 
  float: left;
  overflow: hidden;


.dropdown .dropbtn 
  font-size: 16px;  
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
  width: 100%; /*red background color width*/
  text-align: start; /*text inside in red background color*/


.navbar a:hover, .dropdown:hover .dropbtn 
  background-color: red;


.dropdown-content-sub 
  display: none;
  position: relative;
  top: -46px;
  right: -300px;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
  width: 300px; /*block width*/


.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;
  width: 300px; /*block width*/


.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;


.dropdown-sub:hover .dropdown-content-sub 
  display: block;


</style>
</head>
<body>
        
<div class="navbar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown 
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <div class="dropdown-sub">
    <button class="dropbtn">Link 3 
      <i class="fa fa-caret-right"></i>
    </button>
    <div class="dropdown-content-sub">
      <a href="#">Link 4</a>
      <a href="#">Link 5</a>
      <a href="#">Link 6</a>
    </div>
  </div> 
    </div>
  </div> 
</div>

<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>



</body>
</html>

Update 2 Final:所以看来我将第三层的位置从绝对更改为相对导致了不希望的效果。只需将其切换回来即可解决,现在一切正常。 :) 之后我不得不更改按钮的“顶部”位置,但这很容易弄清楚。现在我应该能够将我的链接放在每个菜单项中,我很高兴! :) 感谢您的帮助!

【问题讨论】:

假设 this 是您遵循的教程,对我来说似乎解释得很好。你能更准确地说你在哪个部分苦苦挣扎吗? 是的,这就是我正在使用的。我假设我需要在样式中添加另一块 CSS 并以某种方式在正文中使用它。我只是不确定这一切是如何分解的以便对其进行定制。我在 css 上确实有点绿,但如果有人向我解释,我相信我可以跟进。 我已经更新了 OP。你能帮我找出导致我在更新中提到的问题的错误吗? 使用position: absolute,而不是position:relative。 this article 解释区别 @JannesCarpentier 你太棒了!非常感谢你的帮忙!另一个删除了他们的答案的人。我不确定他们为什么这样做。但他们也完全帮助了我。我应该可以从这里拿走它。我并不是说我都明白了,但这解决了我的问题。只是希望我知道在代码中导航栏和按钮的大小是在哪里确定的。 :\ 【参考方案1】:

查看 OP 中的更新。感谢@JannesCarpentier 和另一位匿名用户的帮助!这是从 w3schools.com 调整的代码以获得 3 层悬停导航栏。

<!doctype html>
<html>
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<title>Title</title>
<style>
body 
  font-family: Arial, Helvetica, sans-serif;


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


.navbar a 
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;


.dropdown 
  float: left;
  overflow: hidden;


.dropdown .dropbtn 
  font-size: 16px;  
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
  width: 100%; /*red background color width*/
  text-align: start; /*text inside in red background color*/


.navbar a:hover, .dropdown:hover .dropbtn 
  background-color: red;


.dropdown-content-sub 
  display: none;
  position: absolute;
  top: 85px;
  right: -300px;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
  width: 300px; /*block width*/


.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;
  width: 300px; /*block width*/


.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;


.dropdown-sub:hover .dropdown-content-sub 
  display: block;


</style>
</head>
<body>
        
<div class="navbar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown 
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <div class="dropdown-sub">
    <button class="dropbtn">Link 3 
      <i class="fa fa-caret-right"></i>
    </button>
    <div class="dropdown-content-sub">
      <a href="#">Link 4</a>
      <a href="#">Link 5</a>
      <a href="#">Link 6</a>
    </div>
  </div> 
    </div>
  </div> 
</div>

<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>



</body>
</html>

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


.navbar a 
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;


.dropdown 
  float: left;
  overflow: hidden;


.dropdown .dropbtn 
  font-size: 16px;
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
  width: 100%; /*red background color width*/
  text-align: start; /*text inside in red background color*/


.navbar a:hover, .dropdown:hover .dropbtn 
  background-color: red;


.dropdown-content-sub 
  display: none;
  position: absolute;
  top: 85px;
  right: -300px;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
  width: 300px; /*block width*/


.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;
  width: 300px; /*block width*/


.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;


.dropdown-sub:hover .dropdown-content-sub 
  display: block;
<div class="navbar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <div class="dropdown-sub">
    <button class="dropbtn">Link 3
      <i class="fa fa-caret-right"></i>
    </button>
    <div class="dropdown-content-sub">
      <a href="#">Link 4</a>
      <a href="#">Link 5</a>
      <a href="#">Link 6</a>
    </div>
  </div>
    </div>
  </div>
</div>

【讨论】:

以上是关于带有 HTML 和 CSS 的多级悬停导航栏的主要内容,如果未能解决你的问题,请参考以下文章

CSS导航栏悬停未激活

HTML/CSS 导航栏不工作

HTML导航栏自适应填充[重复]

使用 Css 的导航栏样式

使用奇怪的导航栏的 CSS 悬停问题

在 CSS/HTML 中为导航栏上的按钮添加图像