vertically center div when body height: 100% without absolute pos

前端 未结 2 1571
无人及你
无人及你 2021-02-10 16:17

I have this to fill the window:

html, body {
height: 100%;}

then a container also set to height: 100%, how do I vertically center a div with an

2条回答
  •  无人共我
    2021-02-10 17:01

    today, display:table and table-cell make it easy, you can have tags to react as a

    would. basicly :

    html, body {
        height: 100%;
        width: 100%;
        margin: 0;
    }
    html {
        display: table;
    }
    body {
        display: table-cell;
        vertical-align: middle;
    }
    

    whatever will be in body will be vertical-align in the middle. for text a small box , use text-align on body or margin : auto. you can transfer display to body and div instead html and body.

    提交回复
    热议问题