一、常用margin: 0 auto; 水平居中
.center-box{
width: 1200px;
height: 500px;
background-color: green;
margin: 0 auto;
}
二、定位position(兼容ie6/7/8)、水平居中
<div class="father">
<div class="center-box"></div>
</div>
.father{
position: relative;
z-index: 1;
}
.center-box{
width: 1200px;
height: 300px;
z-index: 2;
background: #f60;
position: absolute;
top:0;
left: 50%;
margin-left: -600px;
/*transform: translate(-50%, 0); */ 支持css3浏览器
}
三、定位position+transform 、水平+垂直居中(遮罩层)
<div class="father">
<div class="center-box"></div>
</div>
.father{
position: fixed;
_position: absolute;
top: 0;
left: 0;
z-index: 999999;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.7);
filter: progid:DXImageTransform.Microsoft.gradient(enabled='true',startColorstr='#B2000000',endColorstr='#B2000000');
}
.center-box{
position: fixed;
_position: absolute;
top: 50%;
left: 50%;
width: 500px;
height: 350px;
margin: -175px 0 0 -250px;
padding: 20px 0;
color: #333;
background: #fff;
}
@:亲测、三种方法都好用!