绝对定位居中问题

关键,如果要垂直居中,设置top: 0;bottom: 0;marign:auto;

//垂直居中
.box{
    text-align: center;
    background-color: blue;
    width: 40%;
    text-align: center;
    height: 300px;
    position: absolute;
    top: 0;
    bottom: 0;
    margin: 0 auto;
}

关键,如果要水平居中,设置left: 0;right: 0; margin: auto;

//垂直居中
.box{
    text-align: center;
    background-color: blue;
    width: 40%;
    text-align: center;
    height: 300px;
    position: absolute;
    left: 0;
    right: 0;
    margin: 0 auto;
}