3

请参考我的网站 [已删除]

我的页脚按预期显示在视口的末尾。

当屏幕垂直调整大小时,页脚将覆盖其上方的所有内容。

如何避免这种情况?

4

4 回答 4

4

看看这个粘性页脚教程。以下代码应该是您所需要的。

* {
    margin: 0;
}
html, body {
    height: 100%;
}
.wrapper {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0 auto -142px; /* the bottom margin is the negative value of the footer's height */
}
.footer, .push {
    height: 142px; /* .push must be the same height as .footer */
}

/*

Sticky Footer by Ryan Fait
http://ryanfait.com/

*/

编辑:

包装器中的负值与页脚的高度不匹配。这很可能是您问题的一部分。

#wrapper {
    min-height: 100%;
    min-width: 450px;
    height: auto !important;
    height: 100%;
    /* your old code: margin: 0 auto -4em;*/
    margin: 0 auto -83px;
}

footer {    
    background: url('images/foot_bg.jpg') center no-repeat,
    url('images/foot_liq_bg.jpg') repeat;
    position:absolute;
    bottom:0;
    width:100%;
    height:83px;
    min-width: 450px;
}

编辑:

您没有将 html 和正文的高度设置为 100%。因此,body 将仅设置为其父 (html) 的 100%,而不是浏览器的 100%。您还必须将 html 的高度设置为 100% 才能正常工作。

于 2012-04-25T16:07:22.240 回答
1

它重叠是因为你给它绝对定位。您需要将其相对于您的其他内容进行定位。将您的主要内容设置为页面的 100% 高度,并将页脚放在下面。然后对页脚应用负边距,其像素数量与其高度相同。

例如,如果height:100px;使用了页脚margin-top:-100px;

来源:CSS 粘滞页脚

于 2012-04-25T16:12:28.453 回答
0

别担心,肖恩。你离让它工作不远了。

您需要在元素上更改padding-bottom为,并从元素上取下绝对定位。此外,添加 'push' div 是让这种粘页脚方法起作用的必要条件。margin-bottombodyfooter

这个 jsFiddle 应该有帮助,它对我有用:http: //jsfiddle.net/4vunq/

HTML:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Vault-X</title>
<!--JS enabling IE <=8 to recognise HTML5 elements-->
    <script type="text/javascript">
    document.createElement ('header')
    document.createElement ('footer')
    </script>
    <script type="text/javascript" src="javascript.js"></script>
    <link href="style_01.css" title="one" rel="stylesheet" type="text/css">
</head>
<body>
    <div id="switchoff">
        <div id="wrapper">
            <header></header>
            <div id="switch">
                <a href="#" onClick="lightSwitch();">
                    <img src="http://www.vault-x.com/images/switch.jpg">
                </a>
            </div>
            <div id="content"><p class="switch">Hello World</p></div>
            <div class='push'></div>
        </div>
        <footer></footer>
    </div>
</body>
</html>

​ 相关CSS:

* {
    margin: 0;
}
html, body {
    height: 100%;
}
footer, .push {
    height: 83px; /* .push must be the same height as "footer" */
}

在这里你有padding-bottom而不是margin-bottom

body {
    background:url('http://www.vault-x.com/images/body_bg.jpg') repeat-x;
    background-color: #000;
    margin-bottom:83px;
}

下边距在这里也很重要:

#wrapper {
    min-height: 100%;
    min-width: 450px;
    height: auto !important;
    height: 100%;
    margin: 0 auto -83px; /* bottom margin is negative value of footer's height */
}

你的页脚代码:

footer {
background: url('http://www.vault-x.com/images/foot_bg.jpg') center no-repeat,
url('http://www.vault-x.com/images/foot_liq_bg.jpg') repeat;;
        width:100%;
        height:83px;
        min-width: 450px;
}

那应该差不多了。漂亮的网站;还有祝你好运!:)

于 2012-04-25T17:43:10.243 回答
0

我补充说:两者都解决了我的重叠问题。

#footer {
        position:relative;
        width:100%;
        height: 200px;
        background-color:#7B7168;
        margin-top: -200px;
        padding-bottom:50px;
        clear:both;
        }
于 2015-10-07T14:44:29.497 回答