html - 侧边栏 z-index 始终高于下拉菜单
问题描述
我制作了一个侧边栏,其中包含 2 个绝对彼此相邻的导航元素。在我所谓的“子侧边栏”的底部,我有一个充当下拉菜单的图像。当我激活下拉菜单时,菜单项本身总是位于所谓的“主侧边栏”后面。为了说明这一点,我有一个插图。
虽然它应该看起来像这样
在我的 CSS(Sass) 中,我从未设置过任何 z-index。是什么导致了这个问题
代码
.dashboard-sub-sidebar {
width: 64px;
background-color: #f9328e;
position: fixed;
display: block;
top: 0;
left: 0;
height: 100vh;
padding: 24px 0 24px 0;
}
.dashboard-sidebar {
transition: width 300ms cubic-bezier(0.2, 0, 0, 1) 0s;
width: 250px;
height: 100vh;
background-color: #f9328e;
position: fixed;
top: 0;
left: 64px;
display: block;
padding: 15px;
color: $text-white;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" 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://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" 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>
<nav class="dashboard-sub-sidebar">
<div class="dashboard-sub-sidebar-container">
<div class="dashboard-sub-top-content text-center text-white">
<div class="dashboard-sub-top-logo">
LOGO
</div>
<div class="sidebar-icon-box slideout mt-4" data-slideout-block="search">
<i id="dashboard-sub-search" class="fa fa-search dashboard-sub-search"></i>
</div>
</div>
<div class="dashboard-sub-user-content text-center">
<div class="sidebar-icon-box slideout mb-3" data-slideout-block="notification">
<i id="dashboard-sub-notification" class="fa fa-bell dashboard-sub-notification"></i>
</div>
<div class="sidebar-icon-box mb-3">
<i id="dashboard-sub-help" class="fa fa-question-circle dashboard-sub-help"></i>
</div>
<div class="btn-group dropright">
<img src="http://s3.amazonaws.com/images.seroundtable.com/google-dog-badge-1526987203.jpg" class="dashboard-sub-sidebar-user-image dropdown-toggle" alt="" width="40" height="40" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<div class="dropdown-menu">
<ul class="list-unstyled">
<li><a href="">Profiel</a></li>
<li><a href="">Account instellingen</a></li>
<li><a href="">Uitloggen</a></li>
</ul>
</div>
</div>
</div>
</div>
</nav>
<nav id="dashboard-sidebar" class="dashboard-sidebar shadow">
<div class="dashboard-sidebar-brand">
<h5><a href="../../index.html" class="text-white">Logo bedrijf</a></h5>
</div>
<ul class="dashboard-sub-content list-unstyled">
<a href="" class="dashboard-sidebar-link">
<li class="dashboard-sidebar-listitem active"><i class="fal fa-window-maximize"></i>Dashboard</li>
</a>
<a href="" class="dashboard-sidebar-link">
<li class="dashboard-sidebar-listitem"><i class="fal fa-folder"></i>Projects</li>
</a>
<a href="#" class="dashboard-sidebar-link slideout slideout-item" data-slideout-item="clients">
<li class="dashboard-sidebar-listitem"><i class="fal fa-user-tie"></i>Clients</li>
</a>
<a href="#" class="dashboard-sidebar-link">
<li class="dashboard-sidebar-listitem"><i class="fal fa-user-friends"></i>Employees</li>
</a>
<a href="your_company.html" class="dashboard-sidebar-link">
<li class="dashboard-sidebar-listitem"><i class="fal fa-building"></i>My company</li>
</a>
</ul>
<button id="dashboard-sidebar-toggle" class="btn btn-light btn-sm shadow dashboard-sidebar-toggle"><i class="fal fa-arrow-left"></i></button>
</nav>
解决方案
同一堆叠上下文中的元素将按出现顺序显示,后面的元素位于前面的元素之上。
.dashboard-sub-sidebar
在您的示例中,由于之前声明了工具提示的父级,因此由于出现顺序,dashboard-sidebar
最后一个自动位于其子级之上。.dashboard-sub-sidebar
添加z-index
到.dashboard-sub-sidebar
将解决问题。
推荐阅读
- java - 如何在不从本地 xml、csv 文件访问 API 的情况下将变量发布到 php 站点?
- javascript - 将鼠标悬停在上下文菜单上时如何更改上下文菜单的 li 项的颜色
- javascript - 与索引 0 的 Levenshtein 距离
- php - 将“Image”标签替换为“a”标签 PHP DOMDocument
- azure-storage - 如何检查Azure容器中是否存在多个文件
- nginx - Nginx 服务于一个子域的根而不是另一个子域的根
- console - 如果qpad关闭但端口打开,qpad查询可以恢复吗?
- css - 媒体查询纵横比
- .htaccess - 覆盖特定页面的 301 重定向
- julia - 在 Julia 的函数中使用 Optim 时如何解决此方法错误?