css水平垂直居中

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <style>
        #super{
            width: 200px;
            height: 200px;
            background: red;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        #sub{
            width: 100px;
            height: 100px;
            background: blue;
        }
    </style>
    <body>
        <div id="super">
            <div id="sub"></div>
        </div>
    </body>
</html>

只要在父元素放上display:flex(flex布局)

align-items:center 垂直居中

justify-content:center 水平居中

欢迎进群:822162679  

 

posted @ 2018-07-13 13:44  徐伟杰  阅读(190)  评论(0编辑  收藏  举报