【问题标题】:Footer position bottom issue页脚位置底部问题
【发布时间】:2014-05-06 05:03:58
【问题描述】:

我希望我的页脚在页面底部是绝对位置,即使内容小于窗口高度。我已经尝试了所有可能的教程以及所有能想到但做不到的东西。

我的 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 和边距 0 自动;

谁能给个解决办法?

【问题讨论】:

  • 您希望它始终位于窗口底部,或者如果页面长于窗口则位于页面底部,如果页面较短则位于窗口底部?
  • 是的,如果页面较短,则在底部。我不想要一个总是在顶部的粘性页脚。
  • 好的,但是如果页面较长,您还希望它在屏幕底部还是在页面底部?
  • 页面底部不是屏幕。
  • 我相信你不能单独使用 css 来切换它,但是使用 javascript 相当容易,你会接受 javascript/jQuery 解决方案吗?

标签: css footer


【解决方案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()
        });
    }
}

【讨论】:

  • 内容已经结束了,我讨厌这个问题,就像它没有解决方案一样哈哈......
  • 大声笑你是对的,我没有注意它,但我现在修复了它:答案中的新小提琴:)
【解决方案2】:

如果我正确理解您的问题,您实际上希望将页脚定位为固定元素并将底部设置为 0px。无论如何,这都会将其保留在页面底部。

position:fixed;
bottom:0px;

【讨论】:

  • 它修复了它不放在底部。
【解决方案3】:

您可以使用 CSS 中的“fixed”属性并添加“bottom:0px;”所以它会永远在底部;)

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

【讨论】:

  • 但是页脚是固定的并且总是显示?我不希望页脚始终位于屏幕底部,而是位于页面底部。因此,如果页面比内容短,它仍应保持在底部。我认为绝对位置是在我的情况下如何使用它的解决方案不知道,大声笑。我已经尝试了一切,因为我真的很了解 CSS。
  • 所以是的 - 其他“相对”元素中的“绝对”位置将解决它。
  • 是的,但如何?大声笑我真的尝试了一切,在正文或内容上设置最小高度或使用相对于正文的位置并给出页脚位置绝对但它不起作用我无法弄清楚。
  • 好吧...让我们更进一步:jsfiddle.net/nLHwE 它只是使用了一点 jQuery。一旦用户更改了窗口大小,您也可以添加一个函数来刷新它。
  • 是的,但是如果您有很多内容怎么办?检查jsfiddle.net/nLHwE/1,然后页脚在内容的顶部。
猜你喜欢
  • 2020-01-02
  • 2016-06-13
  • 2012-04-24
  • 2016-12-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-02
  • 2017-10-03
  • 2011-08-01
相关资源
最近更新 更多