【问题标题】:CSS Sticky headerCSS 粘性标题
【发布时间】:2013-09-16 11:16:16
【问题描述】:

我在主页上添加了一个置顶页眉,但置顶页眉似乎位于页面上其余内容的后面,所以当我向下滚动页面时,图像和文本位于页眉顶部,是否存在有办法阻止这种情况吗?

这是我的代码:

<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 -->

【问题讨论】:

标签: html css header sticky


【解决方案1】:

添加此代码.. 在 #header_container 和 z-index:1001 中添加 z-index:1000 #header 样式

 #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; }
  • 为什么不加z-index: 2001;?它好 1000 倍。 ; )
  • 您可以*做的另一件事...实际上是将页眉标记移动到页面底部。当您在标记中向下移动时,事情自然会堆积起来。
【解决方案2】:

只需使用 z-index 参数。 例如 z-index: 2 (表示层的顺序) 它仅适用于使用位置的元素:绝对、相对或固定

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

从 iPhone 发送

【讨论】:

    【解决方案3】:

    <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>

    Click here to Check this best link for Sticky header using css

    【讨论】:

      【解决方案4】:

      在您的标题中添加:

      position:absolute;
      z-index:9999;
      

      这将把它提升到其他一切之上

      【讨论】:

      • OP 正在使用“粘性”标头,position: absolute; 会破坏该功能。他们已经有了position: fixed;,所以z-index 是唯一需要的。
      猜你喜欢
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 2019-08-17
      • 2015-06-14
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      相关资源
      最近更新 更多