css - 为什么下拉菜单不显示在内容之上?

标签 css menu z-index

我有一个下拉菜单,它不会出现在其余内容之上。我已将位置设置为绝对位置,将 z-index 设置为 99,但没有成功。有什么想法可以让它位于网站其余部分之上吗?

<body>
    <header>
        <div class="container">
            <h1 class="logo">Relaxr</h1>

            <nav>
                <a href='#'><span class="hamburger"></span></a>
                <ul>
                    <li><a href="index.html">Home</a></li>
                    <li><a href="#">About</a></li>
                    <li><a href="#">Contact</a></li>
                    <li><a href="blog.html">Blog</a></li>
                </ul>
            </nav>
        </div>
    </header>
    <section id="main">
        <div class="container">

nav {
    position: relative;
}
nav ul {
    display: none;
}
header {
    margin-left: -20px;
    margin-right: -20px;
}
body {
    margin-left: 20px;
    margin-right: 20px;
}
.container {
    width: 100%;
    margin: 0;
}
#main {
    margin-top: 10px;
}
header nav ul li {
    display: block;
    margin-right: 20px;
    margin: 0 20px 0 0;
}
.hamburger {
    margin-top: 5px;
    margin-right: 80px;
    margin-bottom: 20px;
    float:right;
    position: relative;
    display: inline-block;
    width: 1.5em;
    height: 0.74em;
    border-top: 0.2em solid #fff;
    border-bottom: 0.2em solid #fff;
}
.hamburger:before {
    content: "";
    position: absolute;
    top: 0.9em;
    left: 0px;
    width: 100%;
    border-top: 0.2em solid #fff;
}
nav ul {    
    position: absolute;
    left: 0; top: 100%;  
    width: 100%; 
    background-color: black;
    z-index: 99;

代码笔: http://codepen.io/kiddigit/pen/wWvPJm

最佳答案

在您的@media 查询中更改

nav {
    position: relative;
}

nav {
    position: absolute;
}

这将使您的菜单保持在顶部。

关于css - 为什么下拉菜单不显示在内容之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37508672/

相关文章:

css - 关于 Canvas 的 CSS z-index 值

css - 为什么在相同的堆叠上下文中具有较低不透明度的 div(但后来在 html 中)堆叠在具有较高不透明度的 div 之上

css - Z-index 不适用于定位

html - 防止输入组跨越 Bootstrap 导航栏的整个宽度

javascript - 快速加载 HTML 中的图像

CSS:菜单上的动态宽度

javascript - 进入子菜单网络链接后,保持父菜单项突出显示

CSS根据宽度/固定像素值旋转

javascript - 通过输入进行搜索

css - 在没有切换的情况下折叠 Bootstrap3 中的菜单