我试图有我的页脚(只是一个div与一行文本在它)是在屏幕的底部,如果内容不一直到底部,或在内容的底部,如果内容需要滚动条。如果内容不需要滚动条,它可以完美地工作,但是当内容太长时,页脚仍然在相同的位置,位于内容的正上方。

我的基本div结构是:

<div id="container">
    <div id="body"></div>
    <div id="footer"></div>
</div>

我相应的CSS(剥离了一些):

body {
    margin: 0;
    padding: 0;
    height: 100%;
}

html {
    margin: 0;
    padding: 0;
    height: 100%;
}

#container {
    width: 674px;
    min-height: 100%;
    height: 100%;
    position: relative;
    margin: 0 auto;
}

#body {
    width: 616px;
    padding: 5px 14px 5px 14px;
    margin: 0 auto;
    padding-bottom: 20px;
}

#footer {
    position: absolute;
    bottom: 0;
    width: 644px;
    height: 20px;
    margin: 0 auto;
}

当前回答

我想分享我是如何解决我的使用Javascript函数,在页面加载被调用。当页面内容的高度小于屏幕的高度时,此解决方案将页脚定位在屏幕的底部。

function fix_layout(){ //increase content div length by uncommenting below line //expandContent(); var wraph = document.getElementById('wrapper').offsetHeight; if(wraph<window.innerHeight){ //if content is less than screenheight var headh = document.getElementById('header').offsetHeight; var conth = document.getElementById('content').offsetHeight; var footh = document.getElementById('footer').offsetHeight; //var foottop = window.innerHeight - (headh + conth + footh); var foottop = window.innerHeight - (footh); $("#footer").css({top:foottop+'px'}); } } function expandContent(){ $('#content').append('<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at ante. Mauris eleifend, quam a vulputate dictum, massa quam dapibus leo, eget vulputate orci purus ut lorem. In fringilla mi in ligula. Pellentesque aliquam quam vel dolor. Nunc adipiscing. Sed quam odio, tempus ac, aliquam molestie, varius ac, tellus. Vestibulum ut nulla aliquam risus rutrum interdum. Pellentesque lorem. Curabitur sit amet erat quis risus feugiat viverra. Pellentesque augue justo, sagittis et, lacinia at, venenatis non, arcu. Nunc nec libero. In cursus dictum risus. Etiam tristique nisl a nulla. Ut a orci. Curabitur dolor nunc, egestas at, accumsan at, malesuada nec, magna.</p>'+ '<p>Nulla facilisi. Nunc volutpat. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Ut sit amet orci vel mauris blandit vehicula. Nullam quis enim. Integer dignissim viverra velit. Curabitur in odio. In hac habitasse platea dictumst. Ut consequat, tellus eu volutpat varius, justo orci elementum dolor, sed imperdiet nulla tellus ut diam. Vestibulum ipsum ante, malesuada quis, tempus ac, placerat sit amet, elit.</p>'+ '<p>Sed eget turpis a pede tempor malesuada. Vivamus quis mi at leo pulvinar hendrerit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Pellentesque aliquet lacus vitae pede. Nullam mollis dolor ac nisi. Phasellus sit amet urna. Praesent pellentesque sapien sed lacus. Donec lacinia odio in odio. In sit amet elit. Maecenas gravida interdum urna. Integer pretium, arcu vitae imperdiet facilisis, elit tellus tempor nisi, vel feugiat ante velit sit amet mauris. Vivamus arcu. Integer pharetra magna ac lacus. Aliquam vitae sapien in nibh vehicula auctor. Suspendisse leo mauris, pulvinar sed, tempor et, consequat ac, lacus. Proin velit. Nulla semper lobortis mauris. Duis urna erat, ornare et, imperdiet eu, suscipit sit amet, massa. Nulla nulla nisi, pellentesque at, egestas quis, fringilla eu, diam.</p>'+ '<p>Donec semper, sem nec tristique tempus, justo neque commodo nisl, ut gravida sem tellus suscipit nunc. Aliquam erat volutpat. Ut tincidunt pretium elit. Aliquam pulvinar. Nulla cursus. Suspendisse potenti. Etiam condimentum hendrerit felis. Duis iaculis aliquam enim. Donec dignissim augue vitae orci. Curabitur luctus felis a metus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In varius neque at enim. Suspendisse massa nulla, viverra in, bibendum vitae, tempor quis, lorem.</p>'+ '<p>Donec dapibus orci sit amet elit. Maecenas rutrum ultrices lectus. Aliquam suscipit, lacus a iaculis adipiscing, eros orci pellentesque nisl, non pharetra dolor urna nec dolor. Integer cursus dolor vel magna. Integer ultrices feugiat sem. Proin nec nibh. Duis eu dui quis nunc sagittis lobortis. Fusce pharetra, enim ut sodales luctus, lectus arcu rhoncus purus, in fringilla augue elit vel lacus. In hac habitasse platea dictumst. Aliquam erat volutpat. Fusce iaculis elit id tellus. Ut accumsan malesuada turpis. Suspendisse potenti. Vestibulum lacus augue, lobortis mattis, laoreet in, varius at, nisi. Nunc gravida. Phasellus faucibus. In hac habitasse platea dictumst. Integer tempor lacus eget lectus. Praesent fringilla augue fringilla dui.</p>'); } /*sample CSS*/ body{ background: black; margin: 0; } #header{ background: grey; } #content{background: yellow; } #footer{ background: red; position: absolute; } #header, #content, #footer{ display: inline-block; width: 100vw; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body onload="fix_layout()"> <div id="wrapper"> <div id="header" class="navbar navbar-inverse navbar-fixed-top" role="navigation"> [some header elements here] </div> <div id="content" class="container"> [some content elements here] </div> <div id="footer" class="footer"> [some footer elements here] </div> </div> </body>

希望这能有所帮助。

其他回答

用jQuery把里面的代码<head></head>标签

<script type="text/javascript">
$(document).ready(function() { 
    var docHeight = $(window).height();
    var footerHeight = $('#footer').height();
    var footerTop = $('#footer').position().top + footerHeight;  
    if (footerTop < docHeight) {
        $('#footer').css('margin-top', 10 + (docHeight - footerTop) + 'px');
    }
});
</script>

最简单的解决方案是在<html>标签上使用min-height,并使用position:absolute定位<footer>;

演示:jsfiddle和SO代码片段:

html { position: relative; min-height: 100%; } body { margin: 0 0 100px; /* bottom = footer height */ padding: 25px; } footer { background-color: orange; position: absolute; left: 0; bottom: 0; height: 100px; width: 100%; overflow: hidden; } <article> <!-- or <div class="container">, etc. --> <h1>James Dean CSS Sticky Footer</h1> <p>Blah blah blah blah</p> <p>More blah blah blah</p> </article> <footer> <h1>Footer Content</h1> </footer>

最简单的方法是为你的页面容器设置最小高度为400px,假设你的页脚在最后。你甚至不需要为页脚放置CSS或者只是一个宽度:100%假设你的页脚是<body>的直接子

为什么不使用:{位置:固定;底部:0}?

这里分享的模型与Ryan Fait的StickyFooter非常相似 http://ryanfait.com/sticky-footer

到目前为止,在这个讨论中只少了一个div (Kenneth Palanganas提出的模型在本地Win81设计中正常工作了大约48小时,然后在ie/chrome中由于未知原因崩溃)。Ryan的“push”div将满足一些不情愿的浏览器。注意,px是常用的,但是,对于液体布局一致性,em可能是首选。

* { border: 0; margin: 0; padding: 0; }
html, body { height: 100%; }
.wrapper { height: auto !important; height: 100%; margin: 0 auto -1em; min-height: 100%; }
.footer, .push { height: 1em; }

<div class="wrapper"><p>Your website content here.</p>
<div class="push"></div>
</div>
<div class="footer"><p>This is a footer</p>
</div>