美文网首页
flex item with margin auto

flex item with margin auto

作者: 老虎爱吃母鸡 | 来源:发表于2017-06-30 11:50 被阅读0次
image.png

Using CSS Flexible Boxes的Flex Item Considerations部分,有这样一段话,表达的意思主要有两个:

  1. flex item的外边距不会合并
  2. flex item使用margin: auto;可以吸收多余的空间

利用第二点,在flex item就可以实现justify-content: center;或者水平垂直居中的效果

<div>
  <span>123</span>
</div>
div {
  width: 100px;
  height: 100px;
  background-color: red;
  display: flex;
}
span {
  margin: auto;
}

最后效果

image.png

其实就相当于在div上使用

div {
  //...
  justify-content: center;
  align-items: center;
}

除了可以用来居中,还可以用来分配多个flex item剩余的空间

<div>
  <span>123</span>
  <span>456</span>
</div>

css代码一样,那么最终的效果就是

image.png

相关文章

网友评论

      本文标题:flex item with margin auto

      本文链接:https://www.haomeiwen.com/subject/rgubcxtx.html