html - 如何让 z-index 工作,它拒绝打球
问题描述
我希望拥有它,以便当您将鼠标悬停在导航栏上时,下拉菜单位于主要内容的上方/顶部,但是在菜单下拉时,它会将主图像向下推而不是坐着正如我所期望的 z-index 属性所做的那样。
我已将导航 div 设置为相对,并将主要部分 div 设置为相对,但仍然没有喜悦!
请问有哪位大侠能帮忙解决下吗?
<div id="top-bar-container">
<img src="img/MSO-logo.jpg" alt="MSO Digital Agency" />
<i id="hamburger-icon" class="fas fa-bars fa-2x"></i>
<nav id="nav-bar">
<ul id="test">
<li><a href="#">Home</a></li>
<li><a href="#">About Us</a></li>
<li>
<a href="#">Services</a>
<ul>
<li><a href="#">Web Design</a></li>
<li><a href="#">Branding</a></li>
<li><a href="#">Consulting</a></li>
<li><a href="#">SEO</a></li>
</ul>
</li>
<li><a href="#">Our Work</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</nav>
</div>
<div id="main-section">
<img id="main-img" src="img/main-image.png" alt="" />
</div>
#top-bar-container {
background-color: #ec671c;
width: 100%;
height: 75px;
}
#nav-bar {
width: 75%;
float: right;
padding-right: 50px;
position: relative;
z-index: 1;
}
ul {
list-style: none;
padding: 0;
float: right;
}
ul li {
float: left;
width: 90px;
list-style: none;
margin-top: 10px;
text-align: center;
padding: 5px;
}
ul li:hover {
background-color: #ec671c;
border-radius: 5%;
}
ul li a {
text-decoration: none;
color: white;
}
ul li a:hover {
color: orange;
}
ul li ul {
line-height: 25px;
}
ul li ul li {
display: none;
font-size: 13px;
}
ul li ul li a {
color: white;
}
ul li:hover ul li {
display: block;
padding: 0px;
}
#hamburger-icon {
display: none;
color: white;
position: absolute;
right: 20px;
top: 20px;
}
#hamburger-icon:hover {
color: orange;
}
#main-section {
width: 100%;
height: 100vh;
position: relative;
}
#main-img {
width: 100%;
height: 100vh;
}
解决方案
#top-bar-container {
background-color: #ec671c;
width: 100%;
height: 75px;
}
#nav-bar {
width: 75%;
float: right;
padding-right: 50px;
}
ul {
list-style: none;
padding: 0;
float: right;
background-color: #ec671c;
}
ul li {
float: left;
width: 90px;
list-style: none;
margin-top: 10px;
text-align: center;
padding: 5px;
position:relative;
}
ul li:hover {
background-color: #ec671c;
border-radius: 5%;
}
ul li a {
text-decoration: none;
color: white;
}
ul li a:hover {
color: orange;
}
ul li ul {
line-height: 25px;
}
ul li ul li {
display: none;
font-size: 13px;
}
ul li ul li a {
color: white;
}
ul li:hover ul li {
display: block;
padding: 0px;
}
#hamburger-icon {
display: none;
color: white;
position: absolute;
right: 20px;
top: 20px;
}
#hamburger-icon:hover {
color: orange;
}
#main-section {
width: 100%;
height: 100vh;
}
#main-img {
width: 100%;
height: 100vh;
}
#top-bar-container >nav >ul > li > ul{
position:absolute;
}
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
</style>
</head>
<body>
<div id="top-bar-container">
<img src="img/MSO-logo.jpg" alt="MSO Digital Agency" />
<i id="hamburger-icon" class="fas fa-bars fa-2x"></i>
<nav id="nav-bar">
<ul id="test">
<li><a href="#">Home</a></li>
<li><a href="#">About Us</a></li>
<li>
<a href="#">Services</a>
<ul>
<li><a href="#">Web Design</a></li>
<li><a href="#">Branding</a></li>
<li><a href="#">Consulting</a></li>
<li><a href="#">SEO</a></li>
</ul>
</li>
<li><a href="#">Our Work</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</nav>
</div>
<div id="main-section">
<img id="main-img" src="img" alt="" />
</div>
</body>
</html>
嗨,您可以在 ul>li{position:relative} 部分中进行操作,另外,放入第二个 UL {position:absolute}
推荐阅读
- c# - 当令牌过期而不是 401 时,ABP 框架在授权过滤器中抛出 500 错误
- python - 将numpy数组的所有元素转换为字符串的最快方法
- javascript - 使用 Leaflet 单击时为每个多边形设置目标 URL
- python - 使用python将字符串数组转换为浮点数
- github - 尝试在 unix:///var/run/docker.sock 连接到 Docker 守护进程套接字时获得权限被拒绝
- node.js - 如何在 GKE 中访问 npm 日志文件?
- ktor - 如何在 Ktor 中列出配置的路由
- javascript - 带有 Cordova 相机插件的 JavaScript - 将图像 FILE_URI 转换为 DATA_URL
- c - C 中的 Strlen 安全性
- reactjs - 运行“npm install npm@update -g”后,我得到的“漏洞”更少。我是否以某种方式修复它们?