我创建一个菜单,在页面顶部有一个子菜单,在菜单下有一个幻灯片图像。
结果:请参考附件中的图像文件: 子菜单显示在幻灯片图像的后面:
如何将子菜单显示在幻灯片图像的前面?
这是我的顶级菜单的CSS: 但是,如果我添加幻灯片图像,则子菜单显示在后面。
{
margin: 0;
padding: 0;
}
#menu {
text-align: center;
padding: 10px;
}
#menu ul {
list-style-type: none;
text-align: center;
}
#menu li {
display: inline-block;
padding: 10px;
position: relative;
}
#menu a {
color: #4C9CF1;
text-decoration: none;
font-weight: bold;
display: block;
font-size: 16px;
}
#menu a:hover {
color: #444;
}
.sub-menu {
display: none;
position: absolute;
}
.sub-menu li {
display: none;
margin-left: 0 !important;
white-space: nowrap;
}
.sub-menu li a {
display: none;
margin-left: 0 !important;
}
#menu li:hover .sub-menu {
display: block;
}
<header>
<div id="menu">
<ul>
<li><a href="home">Home</a></li>
<li><a href="about">Product</a>
<ul class="sub-menu">
<li><a href="#">Product 1</a></li>
<li><a href="#">Product 2</a></li>
<li><a href="#">Product 3</a></li>
</ul>
</li>
<li><a href="login">Login</a></li>
<li><a href="signup">SignUp</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</header>
<!-- display the slider -->
<div>
<div id="amazingslider-1" style="display:block;position:relative;margin:0 auto;">
<ul class="amazingslider-slides" style="">
<li><img src="https://via.placeholder.com/1024x768" alt="1" title="1" />
</li>
</ul>
</div>
</div>
<!-- End -->
答案 0 :(得分:4)
尝试
添加z-index: 100;
,以便其工作
.sub-menu {
z-index: 100;
}
{
margin: 0;
padding: 0;
}
#menu {
text-align: center;
padding: 10px;
}
#menu ul {
list-style-type: none;
text-align: center;
}
#menu li {
display: inline-block;
padding: 10px;
position: relative;
}
#menu a {
color: #4C9CF1;
text-decoration: none;
font-weight: bold;
display: block;
font-size: 16px;
}
#menu a:hover {
color: #444;
}
.sub-menu {
display: none;
position: absolute;
z-index: 100;
}
.sub-menu li {
display: none;
margin-left: 0 !important;
white-space: nowrap;
}
.sub-menu li a {
display: none;
margin-left: 0 !important;
}
#menu li:hover .sub-menu {
display: block;
}
<body>
<header>
<div id="menu">
<ul>
<li><a href="home">Home</a></li>
<li><a href="about">Product</a>
<ul class="sub-menu">
<li><a href="#">Product 1</a></li>
<li><a href="#">Product 2</a></li>
<li><a href="#">Product 3</a></li>
</ul>
</li>
<li><a href="login">Login</a></li>
<li><a href="signup">SignUp</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</header>
<!-- display the slider -->
<div>
<div id="amazingslider-1" style="display:block;position:relative;margin:0 auto;">
<ul class="amazingslider-slides" style="">
<li><img src="https://static.pexels.com/photos/34950/pexels-photo.jpg" alt="1" title="1" />
</li>
</ul>
</div>
</div>
<!-- End -->
</body>
答案 1 :(得分:3)
为要在前面显示的项目设置较高的z索引。
{
margin: 0;
padding: 0;
}
#menu {
text-align: center;
padding: 10px;
}
#menu ul {
list-style-type: none;
text-align: center;
}
#menu li {
display: inline-block;
padding: 10px;
position: relative;
}
#menu a {
color: #4C9CF1;
text-decoration: none;
font-weight: bold;
display: block;
font-size: 16px;
}
#menu a:hover {
color: #444;
}
.sub-menu {
display: none;
position: absolute;
z-index: 100;
}
.sub-menu li {
display: none;
margin-left: 0 !important;
white-space: nowrap;
}
.sub-menu li a {
display: none;
margin-left: 0 !important;
}
#menu li:hover .sub-menu {
display: block;
}
<header>
<div id="menu">
<ul>
<li><a href="home">Home</a></li>
<li><a href="about">Product</a>
<ul class="sub-menu">
<li><a href="#">Product 1</a></li>
<li><a href="#">Product 2</a></li>
<li><a href="#">Product 3</a></li>
</ul>
</li>
<li><a href="login">Login</a></li>
<li><a href="signup">SignUp</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</header>
<body>
<!-- display the slider -->
<div>
<div id="amazingslider-1" style="display:block;position:relative;margin:0 auto;">
<ul class="amazingslider-slides" style="">
<li><img src="https://via.placeholder.com/1024x768" alt="1" title="1" />
</li>
</ul>
</div>
</div>
</body>
<!-- End -->
答案 2 :(得分:1)
#menu {
z-index:9000
text-align: center;
padding: 10px;
}
答案 3 :(得分:1)
#menu {
text-align: center;
padding: 10px;
position:relative
z-index:99;
}
尝试此操作,必须添加位置