内容div越过固定的导航栏

Cur*_*age 14 css

我有一个样式表,其目的是有一个固定的导航栏,无论你向下滚动多远,都会保持在屏幕的顶部.为此我刚刚使用position:fixed;- 但是当我实际向下滚动时,#contentdiv会覆盖它并直接在顶部(因此导航栏位于页面顶部但位于内容div下方.)

多年来我没有做过任何严肃的CSS编码,所以我有点生疏 - 这可能是一个非常简单的解决方案,所以道歉是如此微不足道!

style.css文件

body {
    margin:0;
    background:#eeeeee;
}

#navbar {
    background-color:#990000;
    position:fixed;
    width:100%;
    height:50px;
    text-align:center;
    vertical-align:middle;
    line-height:50px;
    top:0px;
}

#navbar a {
    color:#fff;
}

#content {
    background:#eeeeee;
    margin-top:50px;
    width:100%;
}

#feed {
    background: #fff;
    position:absolute;
    left:22%;
    width:776px;
}
Run Code Online (Sandbox Code Playgroud)

页面结构如下:

<body>
    <div id="navbar"><?php include core/navbar.php; ?></div>
    <div id="content">
        <div id="feed">
            CONTENT
        </div>
    </div>
</body>  
Run Code Online (Sandbox Code Playgroud)

Dan*_*niP 22

为了解决这个问题,您需要z-indexW3定义的属性来指定元素的级别.试试这个:

#navbar {
  background-color:#990000;
  position:fixed;  
  z-index:1; /*Add this*/
  width:100%;
  height:50px;
  text-align:center;
  vertical-align:middle;
  line-height:50px;
  top:0px;
}
Run Code Online (Sandbox Code Playgroud)

  • 2020 年,它起作用了。我猜我们都缺少 Z-Index (2认同)