我正在实现一个带有粘性页脚的flexbox布局。我在min-height:100vh
和body
上有一个.content-container
来保存内容。我还有header
和footer
没有设置高度。我的问题是页脚设置在页面底部下方,即使没有内容。我已调整min-height:100vh
以将页脚放在底部,但随后移动内容会超出footer
。在所有情况下如何将footer
置于底部?
HTML
<header class="page_header">
<h1>Scoreboard</h1>
</header>
<div class="content_container">
<main class="content">
<h2>Visualized Votes</h2>
<div id="graph_container">
</div>
</main>
<aside class="left_container">
<h2>Recent Votes</h2>
<div class="recent_votes_container"></div>
</aside>
<aside class="right_container">
<h2>Top Votes</h2>
<div class="top_votes_container"></div>
</aside>
</div>
<footer class="page_footer">
Awesomeness developed by Mathieu Jonson
</footer>
CSS
* {
box-sizing: border-box;
}
body {
background-color: #f6f1ed;
display: flex;
min-height: 100vh;
flex-direction: column;
}
header.page_header {
width: 100%;
background-color: #30231d;
}
header h1 {
font-family: 'Lobster Two', cursive;
font-size: 60px;
color: #fff;
padding: 20px;
text-align: center;
}
main, aside {
margin-bottom: 30px;
}
main h2, aside h2 {
font-family: 'Montserrat', sans-serif;
background-color: #b5a797;
padding: 1em;
margin: .5em;
text-transform: capitalize;
text-align: center;
border-top-left-radius: 15px;
border-top-right-radius: 15px;
}
main p, aside p {
padding: 1em;
margin: .5em .5em .3em;
}
footer.page_footer {
background-color: #6a5750;
font-family: 'Lobster Two', cursive;
font-size: 20px;
color: #fff;
padding: 10px;
text-align: center;
}
.content_container {
display: flex;
flex: 1;
min-height: 100vh;
flex-direction: column;
}
main {
flex: 1;
}
.left_container {
flex: 1;
flex: 0 0 15em;
order: -1;
}
.right_container {
flex: 0 0 15em;
}
.recent_votes_container, .top_votes_container {
text-align: center;
}
.recent_votes_container li, .top_votes_container li {
list-style-type: none;
}
@media (min-width: 768px) {
.content_container {
flex-direction: row;
flex: 1;
}
.content_container {
flex: 1;
}
}
答案 0 :(得分:1)
删除以下代码。它使页脚的上一个兄弟太高了,所以页脚被推得太多了。
.content_container {
min-height: 100vh;
}
此外,body
默认有一些余量,但min-height: 100vh
不包括它。删除保证金或将其考虑在内。
* {
box-sizing: border-box;
}
body {
background-color: #f6f1ed;
display: flex;
min-height: 100vh;
flex-direction: column;
margin: 0;
}
header.page_header {
width: 100%;
background-color: #30231d;
}
header h1 {
font-family: 'Lobster Two', cursive;
font-size: 60px;
color: #fff;
padding: 20px;
text-align: center;
}
main,
aside {
margin-bottom: 30px;
}
main h2,
aside h2 {
font-family: 'Montserrat', sans-serif;
background-color: #b5a797;
padding: 1em;
margin: .5em;
text-transform: capitalize;
text-align: center;
border-top-left-radius: 15px;
border-top-right-radius: 15px;
}
main p,
aside p {
padding: 1em;
margin: .5em .5em .3em;
}
footer.page_footer {
background-color: #6a5750;
font-family: 'Lobster Two', cursive;
font-size: 20px;
color: #fff;
padding: 10px;
text-align: center;
}
.content_container {
display: flex;
flex: 1;
flex-direction: column;
}
main {
flex: 1;
}
.left_container {
flex: 1;
flex: 0 0 15em;
order: -1;
}
.right_container {
flex: 0 0 15em;
}
.recent_votes_container,
.top_votes_container {
text-align: center;
}
.recent_votes_container li,
.top_votes_container li {
list-style-type: none;
}
@media (min-width: 768px) {
.content_container {
flex-direction: row;
flex: 1;
}
.content_container {
flex: 1;
}
}
<header class="page_header">
<h1>Scoreboard</h1>
</header>
<div class="content_container">
<main class="content">
<h2>Visualized Votes</h2>
<div id="graph_container">
</div>
</main>
<aside class="left_container">
<h2>Recent Votes</h2>
<div class="recent_votes_container"></div>
</aside>
<aside class="right_container">
<h2>Top Votes</h2>
<div class="top_votes_container"></div>
</aside>
</div>
<footer class="page_footer">
Awesomeness developed by Mathieu Jonson
</footer>