【问题标题】:how to code a stickyfooter in css?如何在css中编写一个粘性页脚?
【发布时间】:2013-05-22 18:27:32
【问题描述】:

我想要一个页眉、正文和页脚。我已经对其进行了编码。当我更改代码时,当我这样给出页脚时,页脚要么位于正文下方

HTML 代码:

<div id="body" style="background-image:url(img/bg.png);" class="body">
<div id="title" class="title">
         <h1><strong></strong></h1>
    </div>

    <div id="desc" class="desc">
    <p style="desc p"></p>
    </div>
 </div> 

<div id="footer" style="background-image:url(img/bottom_bar.png);" class="footer">
<div><h6 class="footer h6">2011-FOOOTER</h6><a href=""><img src="img/info.png" alt="" /></a></div>
</div>

CSS 代码:

.body
 {
float:left; float:left; width:100%; height:100%; min-height: 100%; overflow-y: scroll;
 }

.title
{
width:85%; font-weight:bold; float:left; font-size:20px; margin-top:3%; margin-bottom:2%; margin-left:5%; color:#FFFFFF;
}

.desc
{
  width:90%; font-size:15px; margin-left:5%; margin-right:5%; float:left; color: #FFFFFF; overflow:auto; text-align:justify; line-height:18px;
}

.desc p
{
margin-top:0; 
}

页脚的 CSS 代码:

 .footer
    {
         float:left; width:100%; line-font-size:15px; padding:0px; bottom:0px; height:25px;  font-size:18px;
    }

当我将其编码如下时,页脚位于正文上,当您向下时,您可以看到页脚下方的文本

.footer
{
     float:left; width:100%; position:absolute; line-font-size:15px; padding:0px; bottom:0px; height:25px;  font-size:18px;
}

我希望页脚固定在屏幕底部,并且希望文本在没有滚动条的情况下滚动。

有人可以建议我犯了什么错误以及在哪里做错了吗?

【问题讨论】:

标签: html css


【解决方案1】:

试试这种样式,只需要删除溢出:隐藏在正文中即可查看滚动条

html,
body {
    margin:0;
    padding:0;
    height:100%;
    overflow:hidden;
}
#wrapper {
    min-height:100%;
    position:relative;

}
#header {
    background:#ededed;
    padding:10px;
}
#content {
    padding-bottom:100px; /* Height of the footer element */

}
#footer {
    background:#ffab62;
    width:100%;
    height:100px;
    position:fixed;
    bottom:0;
    left:0;
}

【讨论】:

    【解决方案2】:

    当然你会在身体高度为 100% 的情况下将页脚推到身体下方,没有空间让页脚留在你的视野中,你需要解决它,反正这个问题很常见,你的话可能对你没有帮助,只是你需要搜索关于“粘滞页脚”的很多问题在这里得到了回答,或者只是用谷歌的魔法你可以看到: http://ryanfait.com/sticky-footer/http://www.cssstickyfooter.com/

    并了解它。 祝你好运。

    【讨论】:

    • Al-Mothafar,谢谢,我已经解决了部分固定身体高度的问题它通过以%固定高度。当我将它用于移动设备时,当设备发生变化时,px 高度也会发生变化。 请建议我一种以 % 为单位设置高度的方法
    • 我想为另一个 css 问题找到解决方案CHECK THIS!
    • @user 我给你的链接将height 设置为100% 和min-height 设置为100%,带像素的固定值用于页脚height,并且必须为@987654327 @ 和负值相同的值 margin-top ,所以我真的看不出问题出在哪里,因为链接已经使用 % 而不是 px,你是否希望 height 也用于带有 % 的页脚??
    • Al-Mothafar,使用 float:left; width:100%; min-height: 100%; height: auto !important; height: 100%; overflow:auto; 没有解决问题,我用于正文和页脚 width:100%; position:absolute; padding:0px; margin:0px; bottom:0px; height:100%; line-height:45px; font-size:12px; color:#FFFFFF; text-align:center; float:left; height:25px; line-height:25px;
    • 为什么你使用float:leftwidth:100% 在这里浮动有什么意义?关于position:absolute; 它会破坏一切。尝试从页脚和正文中删除浮动和定位,并修复您的填充和边距以适应链接中的 css。
    【解决方案3】:

    在页脚类中

    代替

    position:absolute;
    

    使用

    position:fixed;
    

    这应该可以工作

    【讨论】:

      猜你喜欢
      • 2022-07-15
      • 2011-09-08
      • 2011-08-14
      • 2012-05-14
      • 2015-02-11
      • 2018-02-19
      • 1970-01-01
      • 2014-03-15
      相关资源
      最近更新 更多