如何使用flexbox在div中水平和垂直居中div?

时间:2014-07-12 04:26:26

标签: html css layout flexbox

如何使用flexbox将div置于另一个div中心(水平)和中间(垂直)?

Other问/答exists,但过于具体

1 个答案:

答案 0 :(得分:0)

jsFiddle

在弹性箱中,将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;
}
相关问题