最佳答案
在 flexbox 中,将 margin
设置为 auto
将自动为水平轴和垂直轴设置边距。
HTML
<div class="parent">
<div class="child">
</div>
</div>
CSS
html, body {width:100%;height:100%;}
.parent {
display: flex;
width: 100%;
height: 100%;
border: 5px solid red;
}
.child {
width: 100px;
height: 100px;
margin: auto; /* Auto margin for both horizontal and vertical axis */
border: 5px solid blue;
}
关于html - 如何使用 flexbox 在 div 中水平和垂直居中 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24709645/