【发布时间】:2022-01-21 15:20:34
【问题描述】:
https://i.stack.imgur.com/e2n5o.png
我想通过flexbox实现这样的布局。 body 是弹性容器,只有两个弹性项目,一张卡片和一个页脚。这是simplified codepen link。如何在卡片位置居中时将页脚放在页面底部。
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
.container {
width: 400px;
height: 300px;
background-color: green;
}
.footer {
margin-bottom: 20px;
background-color: yellow;
width: 200px;
height: 50px;
}
<div class="container"></div>
<div class="footer"></div>
如果我添加margin-top:auto,页脚的位置会正确,但容器会到顶部。
如果我将gap 添加到body,容器将不在中心
【问题讨论】: