垂直居中

sankigan2019-3-21前端CSS

1、行高

<div class="box">垂直居中</div>
.box {
    width: 100px;
    height: 100px;
    background: #FEE;

    line-height: 100px;
}

2、内边距

<div class="box">垂直居中</div>
.box {
    width: 100px;
    background: #FEE;

    padding: 50px 0;
}

3、table

<div class="container">
    <div class="box">垂直居中</div>
</div>
.container {
    width: 100px;
    height: 100px;
    background: #FEE;

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

4、定位

<div class="container">
    <div class="box">垂直居中</div>
</div>
.container {
	width: 100px;
    height: 100px;
    background: #FEE;

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

5、Flex

<div class="box">垂直居中</div>
.box {
    width: 100px;
    height: 100px;
    background: #FEE;

    display: flex;
    align-items: center;
}