首页 > Web开发 > 详细

css垂直居中

时间:2018-02-21 23:28:39      阅读:272      评论:0      收藏:0      [点我收藏+]

css水平居中很容易实现,如果是行内元素,直接使用text-align:center,如果是块元素,则可以marin-left:auto;margin-right:auto;来实现。

垂直居中是前端开发中极其常见的需求,理论上很简单,实践起来却有难度。常用的有以下几种方法:

html格式:

 

<div class="out-box">
        <div class="inner-box">
            <p>inner text</p>
            <h5>inner title</h5>
        </div>
    </div>

 

 

 

1.基于绝对定位的方案

.out-box{
            width: 200px;
            height: 200px;
            background: yellow;
            margin: 50px auto;
            position: relative;
        }
        .inner-box{
            width: 80px;
            height: 100px;
            background: blue;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%,-50%);
       color:#fff;
       text-align:center;
}

效果如图:

技术分享图片

2.基于flexbox的解决方案

.out-box{
            width: 200px;
            height: 200px;
            background: yellow;
            margin: 50px auto;
            display: flex;
        }
        .inner-box{
            background: blue;
            margin: auto;
            color: #fff;
            text-align: center;
        }

效果如图所示:

技术分享图片

 

css垂直居中

原文:https://www.cnblogs.com/cherryshuang/p/8457715.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!