在我们网页开结构中,经常遇到div米素的垂直居中或者水平居中。这篇文章总结一下使用的方式。
.div{
margin:auto;
width:60%;
}
.boxdiv{
text-align:center;
}
.boxdiv .div{
display:inline-block;
}
.div{
position: absolute;
width: 100px;
left: 50%;
margin-left: -50px;/*宽度值/2*/
}
.boxdiv{
width:400px;
height:400px;
display:table-cell;
vertical-align:middle;
text-align: center;
}
.boxdiv .div{
width:200px;
height:200px;
display:inline-block;
}
.boxdiv{
width:400px;
height:400px;
position:relative;
}
.boxdiv .div{
width:200px;
height:200px;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
.boxdiv{
width:400px;
height:400px;
position:relative;
}
.boxdiv .div{
width:300px;
height:200px;
margin:auto;
position:absolute;
left:50%;
top:50%;
margin-left: -150px;
margin-top:-100px;
}
.divbox{
width:400px;
height:400px;
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
.divbox .div{
width:200px;
height:200px;
background-color: red;
}
<div class="parent">
<div class="child">Demo</div>
</div>
<style>
.parent {
position: relative;
}
.child {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
</style>
.parent{
position: relative;
top: 0; left: 0; right: 0; bottom: 0;
display: -webkit-box;
-webkit-box-align: center;
-webkit-box-pack: center;
}
.child{
-webkit-box-flex: 0;
}
div的水平和垂直居中方式总结基本就这些了。若是你有更好的,或者更巧妙的解决方案,迎接留言~~~~
1.阿里云: 本站现在使用的是阿里云主机,平安/可靠/稳固。点击领取2000米代金券、领会最新阿里云产物的种种优惠流动点击进入
2.腾讯云: 提供云服务器、云数据库、云存储、视频与CDN、域名等服务。腾讯云各种产物的最新流动,优惠券领取点击进入
3.广告同盟: 整理了现在主流的广告同盟平台,若是你有流量,可以作为参考选择适合你的平台点击进入
链接: http://www.fly63.com/article/detial/23