居中方式

方案一:
div绝对定位水平垂

直居中【margin:auto实现绝对定位元素的居中】(需要有width和height)
兼容性:,IE7及之前版本不支持

div{
width: 200px;
height: 200px;
background: green;
position:absolute;
left:0;
top: 0;
bottom: 0;
right: 0;
margin: auto;
}
方案二:
div绝对定位水平垂直居中【margin 负间距】 这或许是当前最流行的使用方法。

div{
width:200px;
height: 200px;
background:green;
position: absolute;
left:50%;
top:50%;
margin-left:-100px;
margin-top:-100px;
}
方案三:
div绝对定位水平垂直居中【Transforms 变形】(不需要有width和height)
兼容性:IE8不支持;

div{
width: 200px;
height: 200px;
background: green;
position:absolute;
left:50%; /* 定位父级的50% */
top:50%;
transform: translate(-50%,-50%); /*自己的50% */
}
方案四:
flex布局实现(不需要有width和height)

.box{
height:600px;
display:flex;
justify-content:center;//水平方向居中
align-items:center;//垂直方向居中
/* aa只要三句话就可以实现不定宽高水平垂直居中。 */
}
.box>div{
background: green;
width: 200px;
height: 200px;
}

作者:qq_安之虚静于幻_0
链接:http://www.imooc.com/article/283633?block_id=tuijian_wz
来源:慕课网

发布了8 篇原创文章 · 获赞 0 · 访问量 131

猜你喜欢

转载自blog.csdn.net/weixin_45549435/article/details/104992993