html+css实现横向纵向都居中


横向纵向都居中的三个方法:

最简单的方案当然是flex布局:

.father {
    display: flex;
    justify-content: center;
    align-items: center;
}
.son {
   ...
}

绝对定位配合margin:auto,的实现方案:

.father {
    position: relative;
}
.son {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    margin: auto;
}

绝对定位配合transform实现:

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

原文链接:https://blog.yongit.com/note/1573019.html