我有以下页面(deadlink: http://www.workingstorage.com/Sample.htm),有一个脚注,我不能坐在页面的底部。
我想让页脚
当页面较短且屏幕未被填充时,坚持在窗口底部 当有超过一个屏幕的内容时,保持在文档末尾,并像往常一样向下移动(而不是重叠内容)。
CSS是继承的,让我困惑。我似乎不能正确地改变它,把一个最小高度的内容或使页脚到底部。
我有以下页面(deadlink: http://www.workingstorage.com/Sample.htm),有一个脚注,我不能坐在页面的底部。
我想让页脚
当页面较短且屏幕未被填充时,坚持在窗口底部 当有超过一个屏幕的内容时,保持在文档末尾,并像往常一样向下移动(而不是重叠内容)。
CSS是继承的,让我困惑。我似乎不能正确地改变它,把一个最小高度的内容或使页脚到底部。
当前回答
我实现了使用CSS网格,基本上我定义了3行:
头 内容 页脚
并且使用网格来定义大小,诀窍是在行末对齐页脚,像这样:
CSS
body {
display: grid;
grid-template-rows: auto auto auto;
}
footer {
display: grid;
align-self: end; /* The trick */
}
HTML文件
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<header>
Header content
</header>
<h1>main body</h1>
<p>This is a paragraph.</p>
<footer>
<p>Hello there.</p>
</footer>
</body>
</html>
您可以使用更多的行,但请记住将其添加到CSS中,或将所有内容包装在div中。
这篇指南真的帮我找到了答案。
其他回答
你可以这样做
.footer {
position: absolute;
right: 0;
bottom: 0;
left: 0;
padding: 1rem;
text-align: center;
}
我用这个把我的脚贴在底部,它对我有用:
HTML
<body>
<div class="allButFooter">
<!-- Your page's content goes here, including header, nav, aside, everything -->
</div>
<footer>
<!-- Footer content -->
</footer>
</body>
这是你在HTML中唯一要做的修改,用“allButFooter”类添加div。我对所有的页面都这么做了,那些很短的页面,我知道页脚不会粘在底部,也有足够长的页面,我已经知道我必须滚动。我这样做了,所以我可以看到它在页面内容是动态的情况下工作得很好。
CSS
.allButFooter {
min-height: calc(100vh - 40px);
}
“allButFooter”类有一个min-height值,它取决于视口的高度(100vh意味着视口高度的100%)和页脚的高度,我已经知道是40px。
这就是我所做的,而且对我来说非常有效。我并没有在所有浏览器上都试过,只在Firefox、Chrome和Edge上试过,结果和我想要的一样。页脚贴在底部,不需要更改z-index、位置或任何其他属性。文档中每个元素的位置都是默认位置,我没有将其更改为绝对或固定或其他任何位置。
使用响应式设计
有件事我想澄清一下。当我在使用Twitter-Bootstrap进行响应式设计时,这个解决方案,具有相同的40px高的页脚,并没有像我预期的那样工作。我必须修改函数中我要减去的值
.allButFooter {
min-height: calc(100vh - 95px);
}
这可能是因为Twitter-Bootstrap有自己的边距和填充,所以这就是为什么我必须调整这个值。
一个简单的方法是让页面主体100%,最小高度也为100%。如果你的页脚的高度没有改变,这工作得很好。
给页脚一个负的margin-top:
footer {
clear: both;
position: relative;
height: 200px;
margin-top: -200px;
}
动态一行使用jQuery
我遇到的所有CSS方法都太死板了。此外,如果页脚不是设计的一部分,那么将它设置为fixed也是不可取的。
测试:
铬:60 FF: 54 即:11
假设如下布局:
<html>
<body>
<div id="content"></div>
<div id="footer"></div>
</body>
</html>
使用下面的jQuery函数:
$('#content').css("min-height", $(window).height() - $("#footer").height() + "px");
它所做的是将#content的min-height设置为窗口高度-页脚的高度,无论当时可能是什么。
由于我们使用min-height,如果#content height超过窗口高度,该函数将优雅地降级,并且不会产生任何影响,因为不需要它。
看看它的实际应用:
$("#fix").click(function() { $('#content').css("min-height", $(window).height() - $("#footer").height() + "px"); }); * { margin: 0; padding: 0; box-sizing: border-box; } html { background: #111; } body { text-align: center; background: #444 } #content { background: #999; } #footer { background: #777; width: 100%; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html> <body> <div id="content"> <p>Very short content</p> <button id="fix">Fix it!</button> </div> <div id="footer">Mr. Footer</div> </body> </html>
JsFiddle上的相同片段
奖金:
我们可以更进一步,让这个函数适应动态的查看器高度调整,如下所示:
$(window).resize(function() { $('#content').css("min-height", $(window).height() - $("#footer").height() + "px"); }).resize(); * { margin: 0; padding: 0; box-sizing: border-box; } html { background: #111; } body { text-align: center; background: #444 } #content { background: #999; } #footer { background: #777; width: 100%; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html> <body> <div id="content"> <p>Very short content</p> </div> <div id="footer">Mr. Footer</div> </body> </html>
我的jQuery方法,这一个把页脚放在页面的底部,如果页面内容小于窗口高度,或者只是把页脚放在内容之后,否则:
此外,在其他代码之前将代码保存在自己的附件中,将减少重新定位页脚所需的时间。
(function() {
$('.footer').css('position', $(document).height() > $(window).height() ? "inherit" : "fixed");
})();