【问题标题】:How to bring navigation div to the front如何将导航 div 置于最前面
【发布时间】:2015-12-20 17:08:29
【问题描述】:

我有一个导航栏,当用户滚动时,它会粘在页面顶部,但是当用户继续向下滚动页面时,导航菜单似乎会消失在某些页面元素后面。 非常感谢您对此事的任何帮助,以下是所有代码。

* {
  margin: 0;
  padding: 0;
  list-style: none;
  font-family: 'Segoe UI';
}
p,
h5 {
  margin-bottom: 10px;
  line-height: 1.5;
}
h5 {
  text-align: center;
  border: 1px solid #ccc;
  border-width: 1px 0;
}
h5.fixed {
  position: fixed;
  top: 80px;
  left: 0;
  background-color: #fff;
  right: 0;
}
.nav a {
  text-decoration: none;
  color: #fff;
  display: block;
  transition: .3s background-color;
}
.nav a:hover {
  background-color: #005f5f;
}
.nav a.active {
  background-color: #fff;
  color: #444;
  cursor: default;
}
/* Option 1 - Display Inline */

.nav li {
  display: inline-block;
  margin-right: -4px;
}
	<h5>
				<div class="nav">
      <ul>
        <li class="home"><a href="#">Home</a></li>
        <li class="tutorials"><a class="active" href="#">Tutorials</a></li>
        <li class="about"><a href="#">About</a></li>
        <li class="news"><a href="#">Newsletter</a></li>
        <li class="contact"><a href="#">Contact</a></li>
      </ul>
    </div>
		</h5>

【问题讨论】:

  • 来吧标题元素只能包含内联元素

标签: html css


【解决方案1】:

您的代码中几乎没有问题。我不明白你为什么在固定 div 中使用 h5。要解决问题,试试这个

h5.fixed {
  position: fixed;
  top: 80px;
  left: 0;
  background-color: #fff;
  right: 0;
z-index:999;
}

但我的建议是使用一些更好的方法来实现固定顶部菜单。请检查这个例子https://getbootstrap.com/examples/navbar-fixed-top/

【讨论】:

  • 这解决了我的问题!效果很好,谢谢你,我会接受你的建议并调查一下。再次感谢您!
【解决方案2】:

我猜没问题,但是字体颜色是白色的所以它不可见,我将它更改为 #000 并且它可以正常工作,如果我错了请纠正

另外我添加了 z-index: 9;出于您网站的目的,如果导航是固定的,那么它会在“z”维度“高于”所有元素中更高。不要忘记使用“位置:相对/绝对/固定”等来使 z-index 工作:)

* {
  margin: 0;
  padding: 0;
  list-style: none;
  font-family: 'Segoe UI';
}
p,
h5 {
  margin-bottom: 10px;
  line-height: 1.5;
}
h5 {
  text-align: center;
  border: 1px solid #ccc;
  border-width: 1px 0;
}
h5.fixed {
  position: fixed;
  top: 80px;
  left: 0;
  background-color: #fff;
  right: 0;
}
.nav{
  position: relative;
  z-index: 9;
}
.nav a {
  text-decoration: none;
  color: #000;
  display: block;
  transition: .3s background-color;
}
.nav a:hover {
  background-color: #005f5f;
}
.nav a.active {
  background-color: #fff;
  color: #444;
  cursor: default;
}
/* Option 1 - Display Inline */

.nav li {
  display: inline-block;
  margin-right: -4px;
}
	<h5>
				<div class="nav">
      <ul>
        <li class="home"><a href="#">Home</a></li>
        <li class="tutorials"><a class="active" href="#">Tutorials</a></li>
        <li class="about"><a href="#">About</a></li>
        <li class="news"><a href="#">Newsletter</a></li>
        <li class="contact"><a href="#">Contact</a></li>
      </ul>
    </div>
		</h5>

【讨论】:

    【解决方案3】:

    尝试使用 z-index css 属性。这将允许您将元素放置在页面上的其他元素之上。

    【讨论】:

      【解决方案4】:

      尝试使用此代码:z-index 属性仅在其元素具有相对或绝对值时才有效。否则它不起作用。

      elementname {
      position: relative;
      z-index: 1;
      }
      

      请看这个链接:https://jsfiddle.net/emumsbgp/

      【讨论】:

      • 嗨,我尝试将其添加到我的 css 文件并将其链接到 .nav 对象,但它仍然无法正常工作。请参阅对原问题的修改
      猜你喜欢
      • 2019-10-03
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 2010-09-14
      • 2015-06-28
      相关资源
      最近更新 更多