我希望实现以下问题的相反行为:CSS推Div到页面底部。也就是说,当内容溢出到滚动条时,我希望页脚位于页面底部,就像Stack Overflow一样。

我有一个div id="footer"和下面的CSS:

#footer {
    position: absolute;
    bottom: 30px;
    width: 100%;
}

这将把div移动到视口的底部——但即使你向下滚动页面,元素也会留在那里,所以它不再在底部。

我怎么能确保div保持在页面的内容底部,即使当内容溢出?我不寻找固定的位置,只寻找元素在所有内容的底部。

图片:


当前回答

这是一个直观的解决方案,使用viewport命令将最小高度设置为视口高度减去页脚高度。

html,body{
height: 100%
}
#nonFooter{
min-height: calc(100vh - 30px)
}

#footer {
height:30px;
margin: 0;
clear: both;
width:100%;
}

其他回答

position: fixed;
bottom: 0;
(if needs element in whole display and left align)
left:0;
width: 100%;

这是一个直观的解决方案,使用viewport命令将最小高度设置为视口高度减去页脚高度。

html,body{
height: 100%
}
#nonFooter{
min-height: calc(100vh - 30px)
}

#footer {
height:30px;
margin: 0;
clear: both;
width:100%;
}

如果你有一个固定高度的页脚(例如712px),你可以用js这样做:

var bgTop = 0;
window.addEventListener("resize",theResize);
function theResize(){
    bgTop = winHeight - 712;
    document.getElementById("bg").style.marginTop = bgTop+"px";
}

你没有合上你的;后位:绝对。 否则,您上面的代码将完美地工作!

#footer {
   position:absolute;
   bottom:30px;
   width:100%;
}

I've solved a similar issue by putting all of my main content within an extra div tag (id="outer"). I've then moved the div tag with id="footer" outside of this last "outer" div tag. I've used CSS to specify the height of "outer" and specified the width and height of "footer". I've also used CSS to specify the margin-left and margin-right of "footer" as auto. The result is that the footer sits firmly at the bottom of my page and scrolls with the page too (although, it's still appears inside the "outer" div, but happily outside of the main "content" div. which seems strange, but it's where I want it).