首页 > 其他 > 详细

水平垂直居中的——万能好用方法

时间:2017-06-04 22:10:18      阅读:312      评论:0      收藏:0      [点我收藏+]

一、absolute方法

HTML代码如下:

<body>
        <div id="box">
            <div class="circle left"></div>
            <div class="circle right"></div>
        </div>
</body>

css如下:

#box{
            width: 400px;
            height: 200px;
            background: #ccc;
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
            overflow: hidden;
        }

box是子元素,box相对于body垂直水平居中,只要在box添加以上代码就可以了

 

 

二、display:flex

css如下:

body{
    height: 100vh;
    display: flex;
     justify-content: center;
     align-items: center;   

}
        
#box{
    width: 400px;
    height: 200px;
    background: #ccc;
        position: relative;
}    

在父元素上加以上代码就可以使元素自适应的居中

 

效果

技术分享

水平垂直居中的——万能好用方法

原文:http://www.cnblogs.com/feilu2016/p/6941838.html

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