【发布时间】:2018-04-19 10:03:23
【问题描述】:
我有一个在我的网络应用程序中使用的页脚,如果内容小于屏幕尺寸,我需要将其显示在屏幕底部,但如果内容更大,则不要粘贴(即我需要只有在内容大于屏幕尺寸时向下滚动才能看到它)
我发现多个问题和主题分别讨论,但没有一起讨论。
【问题讨论】:
标签: css
我有一个在我的网络应用程序中使用的页脚,如果内容小于屏幕尺寸,我需要将其显示在屏幕底部,但如果内容更大,则不要粘贴(即我需要只有在内容大于屏幕尺寸时向下滚动才能看到它)
我发现多个问题和主题分别讨论,但没有一起讨论。
【问题讨论】:
标签: css
你在这里:
head, body {
height: 100%;
padding-bottom: 40px;
position: relative;
}
.footer {
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: gray;
padding: 10px;
text-align: center;
}
<h1>awesome page</h1>
<p>awesome content</p>
<div class="footer">
awesome footer
</div>
【讨论】:
将主体或包含元素的最小高度设置为 100vh,然后将页脚绝对定位在元素的底部:
.container {
min-height: 100vh;
background: firebrick;
position: relative;
}
footer {
background: lime;
box-sizing: border-box;
padding: 20px;
bottom: 0;
left: 0;
position: absolute;
width: 100%;
}
<div class="container">
<footer></footer>
</div>
【讨论】:
你可以用 flexbox 做到这一点:
<!DOCTYPE html>
<html>
<head>
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
min-height: 100%;
}
.all {
display: flex;
flex-direction: column;
justify-content: space-between;
min-height: 100%;
background-color: yellow;
}
.content {
background-color: red;
}
.footer {
background-color: green;
}
</style>
</head>
<body>
<div class="all">
<div class="content">
Content...
</div>
<div class="footer">
Footer...
</div>
</div>
</body>
</html>
【讨论】: