css 实现水平垂直居中

 第一种:

.parent {
    position: relative;
    background-color: bisque;
    width: 45em;
    height: 45em;
    display: flex;
    justify-content: center;
    align-items: center;
}

第二种:

.parent {
    position: relative;
    background-color: bisque;
    width: 45em;
    height: 45em; 
}

.child {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

第三种:

.parent {
    position: relative;
    background-color: bisque;
    width: 45em;
    height: 45em; 
}

.child {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto;
}

猜你喜欢

转载自blog.csdn.net/chenh297/article/details/79903628