页脚位置底部问题

时间:2014-05-05 19:00:57

标签: css footer

我希望我的页脚位于页面底部的绝对位置,即使内容小于窗口高度。我已经尝试了所有可能的教程和一切可以想到但无法做到的事情。

我的HTML语法是:

<html>

<head></head>

<body>

<header>
     <div class="wrap"></div>
</header>

<div class="content wrap">
    <div class="left"></div>
    <div class="right"></div>
</div>

<footer>
    <div class="inner"></div>
</footer>

</body>

</html>

网站是全宽我使用.wrap和.inner类的宽度:1000px和margin 0 auto;

如果有人能提出解决方案吗?

3 个答案:

答案 0 :(得分:1)

这是一个小提琴做你用jQuery做的事:FIDDLE

基本上你只是比较身体和窗口的高度,如果身体更长的页脚到绝对, 如果更短,则将其设置为固定:

$(function () {
    //change this to 'display:none'
    $('.right').css({'display':'static'});
    var footer = $('footer');
    var theDocument = $('body');
    var theWindow = $(window);
    if (theDocument.height() < theWindow.height()) {
        footer.css({
            'position': 'fixed'
        });
    } else {
        theWindow.height();
        footer.css({
            'position': 'absolute'
        });

    }
});

更新:

这是一个修复页脚在内容上方的版本,您只需要将页脚向下移动其高度大小

FIDDLE

//######  code inside $(function () {}) will run as soon as DOM loads
$(function () {
    //change this to 'display:static' to add more content
    $('.right').css({'display':'none'});
    //sets a custom event handler to window resize + zoom, which triggers the 
    //footer fix function
    $(window).resize(function(){
        adjust_footer();
    });
    //also call this function as soon as document finishes loading
    adjust_footer();
});
//#####


function adjust_footer(){
    var footer = $('footer');
    var theDocument = $('body');
    var theWindow = $(window);
    //the  +footer.height() checks if there is enough space for footer
    //to stick it as fixed without having it cover content
    if (theDocument.height() +footer.height() < theWindow.height()) {
        footer.css({
            'position': 'fixed',
            //important, or footer will remain misplaced
            'bottom':0
        });
    } else {
        theWindow.height();
        footer.css({
            'position': 'absolute',
            //push footer down and align it to the end of content
            //meaning if footer's height is 50px, it will be pushed 50px
            //from the bottom of the content
            //* remember, bottom attribute aligns the element by its bottom
            'bottom':- footer.height()
        });
    }
}

答案 1 :(得分:0)

如果我正确理解你的问题,你实际上想要将页脚定位为固定元素并将底部设置为0px。无论如何,这都将保持在页面底部。

position:fixed;
bottom:0px;

答案 2 :(得分:0)

您可以使用&#34;固定&#34;来自CSS的属性并添加&#34;底部:0px;&#34;所以它永远处于最底层;)

<header>
     <div class="wrap">Header stuff</div>
</header>

<div class="content wrap">
    <div class="left">Left Stuff</div>
    <div class="right">Right stuff</div>
</div>

<footer>
    <div class="inner">Footer stuff</div>
</footer>

和CSS:

header
{
    width:100%;
    height:50px;
    background:green;
}

.content
{
    width:100%;
    background:blue;
}

.content .left
{
    width:30%;
    float:left;
    background:blue;
}

.content .right
{
    width:70%;
    float:right;
    background:yellow;
}

.inner
{
    padding:10px;
}

footer
{
    position:fixed;
    bottom:0px;
    height:40px;
    width:100%;
    background:red;
}

结果在这里:http://jsfiddle.net/67zR6/1/