在Flex容器中水平居中两个div

时间:2016-02-15 16:06:42

标签: html css css3 flexbox

我有两个div(一个在另一个下面),我希望这个列集中对齐。但是使用justify-content: center div将沿y轴(垂直)居中对齐,但不沿x轴(水平)对齐。

我想知道是否唯一的方法(使用flexbox)也沿着x轴进行居中对齐是添加margin: auto

<div id="sharesocial">
  <div id="sharecondividi">
    Condividi
  </div>
  <div id="shareicons">
    <a target="blank" href=""><span class="fa fa-twitter-square" id="twitterbt"></span></a>
    <a target="blank" href=""><span class="fa fa-google-plus-square" id="googleplusbt"></span></a>
    <a target="blank" href=""><span class="fa fa-facebook-square" id="facebookbt"></span></a>
  </div>
</div>

CSS

#sharesocial {
 background: #646464 none repeat scroll 0 0;
 border-radius: 10px;
 display: flex;
 flex-direction: column;
 height: 100px;
 justify-content: center;
 width: 174px;
}

#sharecondividi {
 align-self: center;
 color: #fff;
 display: flex;
 text-align: center;
}

#shareicons {
 align-items: center;
 background: #f0f0f0 none repeat scroll 0 0;
 border-radius: 5px;
 display: flex;
 height: 50%;
 justify-content: space-around;
 width: 95%;
}

这里是jsfiddle:

https://jsfiddle.net/uLah0gqa/

1 个答案:

答案 0 :(得分:5)

flex-directioncolumn时,您可以使用justify-content进行垂直对齐align-items进行水平对齐

试试这个:

#sharesocial {
    align-items: center;
}

Revised Demo

了解详情:

相关问题