利用绝对定位实现垂直居中


<div style="width: 200px;height: 200px;background: gray;position: relative">
        <div style="position: absolute;top: 0;bottom: 0;margin:auto 0;width: 100%;height:50px;background: black"></div>
</div>

 原理就是利用top和bottom将子div块的高度(height+margin)为包含框的height,上下margin对半分

posted @ 2017-05-15 20:07  IsaacYoung  阅读(199)  评论(0)    收藏  举报