【问题标题】:sticky topnav but dropdowns do not show粘性顶部导航,但下拉菜单不显示
【发布时间】:2020-12-01 21:12:07
【问题描述】:

我想制作一个带有下拉菜单的水平导航栏。问题是当我将导航栏设置为粘性时,下拉菜单不再显示。

我使用下面的 CSS3 来实现粘性效果。有没有其他方法可以达到同样的效果,尤其是使用 CSS。

.topnav {
    overflow: hidden;
    background-color: #333;
    position: fixed;         /* This line adds stickyness */
    top: 0;                  /* along with this line here */
    width: 100%;
}

最小问题示例:

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

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

.topnav a {
  float: left;
  display: block;
  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;
}

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

.main {
  padding: 16px;
  margin-top: 30px;
  height: 1500px;
  /* Used in this example to enable scrolling */
}
<div class="topnav">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown 
          <b>V</b>
        </button>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>
</div>
<div class="main">
  <h1>Fixed Top Menu</h1>
  <h2>Scroll this page to see the effect</h2>
  <h2>The navigation bar will stay at the top of the page while scrolling</h2>
  <h3>Dropdown Menu inside a Navigation Bar</h3>
  <p>Hover over the "Dropdown" link to see the dropdown menu.</p>
</div>

编辑:

感谢 Saeed 和 dkobando。我的网络文档之旅才刚刚开始。我一直在慢慢地从各种指南中拼凑我网站的各个部分。我对两个有用的提示答案感到惊喜。您在这里的回复鼓励我继续我的旅程。

【问题讨论】:

标签: html css navigation


【解决方案1】:

您可以在下面尝试此修复,我使用了 display flex 并删除了溢出:隐藏;在您的 .topnav 课程中。我希望这会有所帮助。

<div class="topnav">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown <b>V</b></button>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>
</div>
<div class="main">
  <h1>Fixed Top Menu</h1>
  <h2>Scroll this page to see the effect</h2>
  <h2>The navigation bar will stay at the top of the page while scrolling</h2>
  <h3>Dropdown Menu inside a Navigation Bar</h3>
  <p>Hover over the "Dropdown" link to see the dropdown menu.</p>
</div>

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  background-color: #333;
  position: fixed;
  top: 0;
  width: 100%;
  display:flex;
}

.topnav a,
.dropdown,
.dropdown .dropbtn {
  font-size: 16px;
  color: white;
  text-align: center;
  text-decoration: none;
}

.topnav a,
.dropdown .dropbtn{
    padding: 14px 16px;
}
.dropdown .dropbtn {
  background-color: transparent;
  border: none;
}

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

.main {
  padding: 16px;
  margin-top: 30px;
  height: 1500px;
  /* Used in this example to enable scrolling */
}

【讨论】:

  • 即使我最终选择了另一个答案,这个答案也很有效。你提示我研究这个flex 系统,它看起来很有可能安排主要内容。
  • 很高兴我能以某种方式提供帮助。干杯。
【解决方案2】:

这是因为下拉菜单是绝对定位的,它不会根据固定的导航栏定位,而是根据最近的相对定位元素定位。它可以通过一个固定位置的额外包装器快速固定。

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

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

.fixed {
  position: fixed;
  top: 0;
  width: 100%;
}

.topnav a {
  float: left;
  display: block;
  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;
}

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

.main {
  padding: 16px;
  margin-top: 30px;
  height: 1500px;
  /* Used in this example to enable scrolling */
}
<div class="fixed">
  <div class="topnav">
    <a href="#home">Home</a>
    <a href="#news">News</a>
    <div class="dropdown">
      <button class="dropbtn">Dropdown 
            <b>V</b>
          </button>
      <div class="dropdown-content">
        <a href="#">Link 1</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
      </div>
    </div>
  </div>
</div>
<div class="main">
  <h1>Fixed Top Menu</h1>
  <h2>Scroll this page to see the effect</h2>
  <h2>The navigation bar will stay at the top of the page while scrolling</h2>
  <h3>Dropdown Menu inside a Navigation Bar</h3>
  <p>Hover over the "Dropdown" link to see the dropdown menu.</p>
</div>

【讨论】:

  • 谢谢!我接受了这个答案,因为它是更简单的修复和与我已经在做的事情保持一致,尽管两者都运作良好。如果我可能会问,我认为在我的顶级元素 topnav 上应用粘性格式也会将其应用到子元素上。我猜不是。也许我可以要求进一步说明附加包装器和新的顶级 div 元素如何应用粘性格式,而以前没有?
  • 添加一个具有固定定位的新 div 包装器只是完成了修复导航栏的工作,并有助于保持导航栏包装器的完整性,以便下拉元素可以与其相对定位相关。
  • 您可以在此处阅读有关 CSS 布局和位置的更多信息:w3schools.com/css/css_positioning.asp
猜你喜欢
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多