css3实现水平、垂直居中

水平居中的方法:

1、父级text-align:center;

.parent{
    text-align: center;
}
.child{
    display: inline-block;
}

2、table配合margin

.child{
    display:table;
    margin: 0 auto;
}

3、使用定位

.parent{
    position:relative;
}
.child{
    position:absolute;
    left:50%;
    transform: translateX(-50%);
}

垂直居中的方法:

1、table-cell配合vertical-align

.parent{
    display: table-cell;
    vertical-align:middle;
}

2、absolute配合tranform

.parent{
    position:relative;
}
.child{
    position:absolute;
    top: 50%;
    transform: translateY(-50%);
}

3、全能的flex

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