我有两个固定顶部导航栏,第一个导航栏的下拉菜单与第二个导航栏重叠。尝试弄乱z索引,定位和溢出。。还发现了一些有点相关的问题,但似乎无济于事。任何想法可能是什么原因造成的?
链接到 FIDDLE
下图显示了问题的性质(第二导航栏重叠下拉菜单):
示例:
<!DOCTYPE html>
<html>
<head>
<meta>
<title>Navbar overlapping dropdown-menu</title>
<!-- Bootstrap 4 cdn's & jquery dependency -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</head>
<body>
<!-- first navbar with dropdown-menu -->
<nav class="navbar bg-dark fixed-top navbar-expand-sm">
<div class="collapse navbar-collapse">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown"> Dropdown </a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#"> 1 </a>
<a class="dropdown-item" href="#"> 2 </a>
<a class="dropdown-item" href="#"> 3 </a>
<a class="dropdown-item" href="#"> 4 </a>
</div>
</li>
</ul>
</div>
</nav>
<!-- second navbar with top offset -->
<nav class="navbar bg-primary fixed-top navbar-expand-sm" style="top:56px!important;">
<p>hello world</p>
</nav>
</body>
</html>
答案 0 :(得分:2)
您是正确的,这是z-index问题。尝试添加一些样式以使z-index的第二个navbar
降低。弄乱z-index时,请记住要调整引起问题的兄弟姐妹。调整.dropdown-menu
不会执行任何操作,因为问题出在父元素上。
您可以这样做:
.navbar {
z-index: 90;
}
.navbar+.navbar {
z-index: 80;
top:56px;
}
<!DOCTYPE html>
<html>
<head>
<meta>
<title>Navbar overlapping dropdown-menu</title>
<!-- Bootstrap 4 cdn's & jquery dependency -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</head>
<body>
<!-- first navbar with dropdown-menu -->
<nav class="navbar bg-dark fixed-top navbar-expand-sm">
<div class="collapse navbar-collapse">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown"> Dropdown </a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#"> 1 </a>
<a class="dropdown-item" href="#"> 2 </a>
<a class="dropdown-item" href="#"> 3 </a>
<a class="dropdown-item" href="#"> 4 </a>
</div>
</li>
</ul>
</div>
</nav>
<!-- second navbar with top offset -->
<nav class="navbar bg-primary fixed-top navbar-expand-sm">
<p>hello world</p>
</nav>
</body>
</html>
答案 1 :(得分:1)
添加了两个类.front
和.back
,并更新了z-index值。
更新了JSFiddle
CSS:
.front{
z-index: 2000 !important;
}
.back{
z-index: -1 !important;
}
HTML:
<!-- first navbar with dropdown-menu -->
<nav class="navbar bg-dark fixed-top navbar-expand-sm front">
<div class="collapse navbar-collapse">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown"> Dropdown </a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#"> 1 </a>
<a class="dropdown-item" href="#"> 2 </a>
<a class="dropdown-item" href="#"> 3 </a>
<a class="dropdown-item" href="#"> 4 </a>
</div>
</li>
</ul>
</div>
</nav>
<!-- second navbar with top offset -->
<nav class="navbar bg-primary fixed-top navbar-expand-sm back" style="top:56px!important;">
<p>hello world</p>
</nav>
对z-index
-https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/Adding_z-index的引用