我自己的一些方法,仅供借鉴,不接受差评,哼~
①容器有固定高度,父容器设置position:relative,容器设置position:absolute
使用top、left定位
.login-container{
position: relative;
width: 100%;
height: 100%;
}
.login-box{
position: absolute;
width: 642px;
height: 605px;
top: 50%;
left: 50%;
margin-top: -302px;
margin-left: -321px;
/*transform: translate(-50%,-50%);*/
background: url("../img/login_bg.png") no-repeat center;
background-size: cover;
}
因为absolute定位是使用容器的左上角那一点进行定位,所以需要偏移调整使容器中心居中(所以偏移此容器的高宽各一半)
使用margin偏移。
②当容器使用百分比的高宽时,推荐使用使用css3的translate偏移。要考虑浏览器的不兼容