H B*_*amy 241 css positioning css-position footer
请查看该链接,我想要相反:当内容溢出到滚动条时,我希望我的页脚始终位于页面的完整底部,如Stack Overflow.
我有一个div id="footer"
和这个CSS:
#footer {
position: absolute;
bottom: 30px;
width: 100%;
}
Run Code Online (Sandbox Code Playgroud)
但它所做的只是转到视口的底部,即使向下滚动也会停留在那里,因此它不再位于底部.
图片:
对不起,如果不澄清,我不希望它被修复,只是因为它在所有内容的实际底部.
Jos*_*ber 508
这正是position: fixed
为以下目的而设计的:
#footer {
position: fixed;
bottom: 0;
width: 100%;
}
Run Code Online (Sandbox Code Playgroud)
这是小提琴:http://jsfiddle.net/uw8f9/
My *_*rts 181
不幸的是,你不能通过添加一些额外的HTML并让一块CSS依赖另一块来做到这一点.
HTML
首先,你需要包装你的header
,footer
并#body
进入#holder
div:
<div id="holder">
<header>.....</header>
<div id="body">....</div>
<footer>....</footer>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS
然后设置height: 100%
为html
和body
(实际的身体,而不是你的#body
div),以确保您可以将最小高度设置为子元素的百分比.
现在设置min-height: 100%
上#holder
,使其充满屏幕的内容和使用DIV position: absolute
在底部坐在页脚#holder
股利.
不幸的是,你必须申请与高度相同padding-bottom
的#body
div,footer
以确保footer
它不会高于任何内容:
html,body{
height: 100%
}
#holder{
min-height: 100%;
position:relative;
}
#body{
padding-bottom: 100px; /* height of footer */
}
footer{
height: 100px;
width:100%;
position: absolute;
left: 0;
bottom: 0;
}
Run Code Online (Sandbox Code Playgroud)
工作示例,简短的身体:http://jsfiddle.net/ELUGc/
工作实例,长体:http://jsfiddle.net/ELUGc/1/
Ano*_*ous 13
刚刚找到另一个解决方案,如上例所示,我有bug(某处错误).所选答案的变化.
html,body {
height: 100%
}
#nonFooter {
min-height: 100%;
position:relative;
/* Firefox */
min-height: -moz-calc(100% - 30px);
/* WebKit */
min-height: -webkit-calc(100% - 30px);
/* Opera */
min-height: -o-calc(100% - 30px);
/* Standard */
min-height: calc(100% - 30px);
}
#footer {
height:30px;
margin: 0;
clear: both;
width:100%;
position: relative;
}
Run Code Online (Sandbox Code Playgroud)
对于HTML布局
<body>
<div id="nonFooter">header,middle,left,right,etc</div>
<div id="footer"></div>
</body>
Run Code Online (Sandbox Code Playgroud)
那么这种方式不支持旧浏览器,但旧浏览器可以接受滚动下载30px来查看页脚
小智 7
你没有关闭你的 ; 后位置:绝对。否则你的上面的代码会完美地工作!
#footer {
position:absolute;
bottom:30px;
width:100%;
}
Run Code Online (Sandbox Code Playgroud)
我意识到它说不要用它来"回答其他答案",但遗憾的是我没有足够的代表在适当的答案(!)上添加评论但是......
如果您在asp.net中遇到"My Head Hurts"答案时遇到问题 - 您需要在主生成的FORM标签以及HTML和BODY标签上添加"height:100%"以使其正常工作.
如果可以的话,我会发表评论,但我还没有权限,所以我将发布一个提示作为答案,以应对某些 Android 设备上的意外行为:
位置:通过使用以下元标记修复仅适用于 Android 2.1 至 2.3:
<meta name="viewport" content="width=device-width, user-scalable=no">.
Run Code Online (Sandbox Code Playgroud)
请参阅http://caniuse.com/#search=position