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

前端 未结 2 1574
无人及你
无人及你 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 16:46

    You can use display:table and display:table-cell:

    html, body {
        width: 100%;
        height: 100%;
    }
    
    body{
        margin: 0;
        display: table
    }
    
    body>div {
        display: table-cell; 
        text-align: center; /* horizontal */
        vertical-align: middle; /* vertical */
    }

    JSFiddle

    More on display property .

提交回复
热议问题