CSS粘性头部

7
我已经在我的主页上添加了一个固定头部,但是这个固定头部似乎在页面的其他内容后面,所以当我向下滚动页面时,图片和文本会覆盖在头部之上,有没有办法阻止这种情况发生?
以下是我的代码:
<style>
/* Reset body padding and margins */
body
{
    margin: 0;
    padding: 0;
}

/* Make Header Sticky */
#header_container
{
    background: #827878;
    border: 1px solid #666;
    height: 60px;
    left: 0;
    position: fixed;
    width: 100%;
    top: 0;
}

#header
{
    line-height: 60px;
    margin: 10 auto;
    width: 940px;
    text-align: left;
    font-size: 26px;
    color: #f5f5f5;
    line-height: 28px;
    margin-bottom: 14px;
    font-family: 'Source Sans Pro',sans-serif;
}

/* CSS for the content of page. I am giving top and bottom 
   padding of 80px to make sure the header and footer 
   do not overlap the content. */
#container
{
    margin: 0 auto;
    overflow: auto;
    padding: 80px 0;
    width: 940px;
}

#content
{
}

/* Make Footer Sticky */
#footer_container
{
    background: #eee;
    border: 1px solid #666;
    bottom: 0;
    height: 60px;
    left: 0;
    position: fixed;
    width: 100%;
}

#footer
{
    line-height: 60px;
    margin: 0 auto;
    width: 940px;
    text-align: center;
}
</style>


<!-- BEGIN: Sticky Header -->
<div id="header_container">
    <div id="header">
        Header Content
    </div>
</div>
<!-- END: Sticky Header -->

1
这样做怎么样?在头部容器中添加了 z-index:9999。http://jsfiddle.net/hGYkt/ - Vikas Ghodke
5个回答

3

将以下代码添加入页面中。在 #header_container 样式和 #header 样式中分别加入 z-index:1000 和 z-index:1001

 #header_container 
      { 
        position:fixed;
        top:0px;
        left:0px;
        z-index:1000;
       }

 #header{
      z-index:1001;
      }

谢谢,这个方法可行,但是我的菜单是唯一保持在顶部的。这是我的菜单CSS代码:menu_sticky { float: left; width: 100%; top: 200px; position: absolute; z-index: 1001; background: #f9f9f9 url(../images/menu-shadow.png) repeat-x left bottom; } - Shane
1
为什么不加上 z-index: 2001; 呢?这样会好很多哦。 ; ) - sheriffderek
你可以做的另一件事是...将标题标记实际上移动到页面底部。当您通过标记向下移动时,事物自然地堆叠在一起。 - sheriffderek

1

只需使用 z-index 参数。 例如:z-index: 2(表示图层的顺序) 仅适用于使用 position: absolute、relative 或 fixed 属性的元素。

W3schools 的示例: http://www.w3schools.com/cssref/pr_pos_z-index.asp

来自 iPhone 发送


0

<style type="text/css">
body{
    margin:0px;
    background:#000;
}
.header-cont {
    width:100%;
    position:fixed;
    top:0px;
}
.header {
    height:50px;
    background:#F0F0F0;
    border:1px solid #CCC;
    width:960px;
    margin:0px auto;
}
.content {
    width:960px;
    background: #F0F0F0;
    border: 1px solid #CCC;
    height: 2000px;
    margin: 70px auto;
}
</style>
</head>
<body>

<div class="header-cont">
    <div></div>
</div>

<div></div>

点击此处查看使用CSS实现粘性标题的最佳链接


0
我正在寻找一个解决方案,这样我就可以为我的客户使用它,幸运的是我找到了一个解决方案,如果你正在使用一个名为Elementor的WordPress插件,那么也可以使用它,在该容器中,你可以放置与下面代码类似的代码。
.sticky-cssSelector-here {
  position: sticky;
  top: 0;
  /* for alignment below */
  align-self: flex-start;
  height: calc(100vh - 45px); /* this is to set a fixed height in different screen sizes and 100 view height for a maximum height of the element */
  padding-top: 100px; // here you can adjust this one as well to make your element viewable

注意:CSS选择器是您应该放置在要插入的元素内部的内容。
如果这不起作用,您可以尝试下面的代码: position: absolute; z-index:9999;

-3

在你的头部添加:

position:absolute;
z-index:9999;

这将使它高于其他所有内容


1
OP正在使用“sticky”标题,position: absolute; 将会破坏该功能。他们已经使用了 position: fixed;,所以唯一需要的是 z-index - Novocaine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接