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