顶部的红色和蓝色条之间有一个神秘的空间。
我做错了什么?当我删除 main
时,栏消失了。但是 footer
到了顶部?
我做了一个 fiddle :https://jsfiddle.net/v9yrmafw/1/#
.strip {
height: 20px;
background: red;
}
.bar {
text-align: center;
background: blue;
color: white;
height: 100px;
}
.container {
display: flex;
flex-direction: column;
height: 100vh;
}
.footer {
margin-top: auto;
}
.footer__body {
display: flex;
align-items: center;
justify-content: center;
height: 70px;
background: yellow;
color: white;
}
main {
flex-grow: 1;
flex-shrink: 0;
flex-basis: auto;
}
<div class="container">
<main>
<div class="strip"></div>
<div class="bar">
<h1>Home</h1>
</div>
</main>
<footer>
<div class="footer">
<div class="footer__body">
<p>© {{ copyright }} {{ now }}</p>
</div>
</div>
</footer>
</div>
最佳答案
这是由于 margin collapsing
而发生的h1
到 bar
div - 所以你可以:
将
的 margin 重置为零h1
向
添加填充/边框bar
请参阅下面的演示 - 我在 bar
中添加了一些填充:
.strip {
height: 20px;
background: red;
}
.bar {
text-align: center;
background: blue;
color: white;
height: 100px;
padding: 10px;
}
.container {
display: flex;
flex-direction: column;
height: 100vh;
}
.footer {
margin-top: auto;
}
.footer__body {
display: flex;
align-items: center;
justify-content: center;
height: 70px;
background: yellow;
color: white;
}
main {
flex-grow: 1;
flex-shrink: 0;
flex-basis: auto;
}
<div class="container">
<main>
<div class="strip"></div>
<div class="bar">
<h1>Home</h1>
</div>
</main>
<footer>
<div class="footer">
<div class="footer__body">
<p>© {{ copyright }} {{ now }}</p>
</div>
</div>
</footer>
</div>
关于html - Flexbox 中元素之间的未知间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41019889/