美文网首页
flex 的垂直居中

flex 的垂直居中

作者: Axiba | 来源:发表于2016-03-14 15:07 被阅读63次

情景如此:使用flex 让子元素垂直居中,此时会发现原本靠左边或者靠右边的属性float:left(right); 已经无法起作用了

.list-item {
    display: flex;
    align-items: center;
}
Paste_Image.png

那么此时或许有两种方式解决这个问题:

(1 使用margin-left: auto;此时伸缩包含块剩余的空间将会分配到flex item的外边距margin上(但是在安卓上兼容性问题)

.list-item {
    img{
        margin-left:auto;
    }
}
Paste_Image.png

(2 使用 justify-content: space-between;进行两端对齐

.list-item {
    display:flex;
    justify-content: space-between;
}

相关文章

网友评论

      本文标题:flex 的垂直居中

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