子div垂直居中

2018/09/17

<div id="outer">
    <div id="inner">
        居个中
    </div>
</div>

利用绝对布局

#outer {
    width: 300px;
    height: 200px;
    position: relative;
}

#inner {
    width: 50px;
    height: 20px;
    margin: auto;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

利用vertical-align

#outer {
    width: 300px;
    height: 200px;
    display: table-cell;
    vertical-align: middle;
}

#inner {
    width: 50px;
    height: 20px;
    margin: 0 auto;
}

利用display: flex

#outer {
    width: 300px;
    height: 200px;
    display: flex;
    justify-content: center;/*水平居中*/
    align-items: center;/*垂直居中*/
}

#inner {
    width: 50px;
    height: 20px;
}