让div水平垂直居中的几种方法

结构层

 <div class='container'>
      <div>123</div>
 </div>

样式层

.container{
            width: 500px;
            height: 500px;
            margin:0 auto;
            border:1px solid red;
            /* 一 二 三 五样式 */
            /* position: relative; */
            /* 第四种的样式 */
            /* display: flex;
            justify-content: center;
            align-items: center; */
            /* 第六种样式 */
            /* display: table; */
            /* 第七种样式 */
            display: flex;
        }
        /* 第一种 */
        /* .container div{
            width: 100px;
            height:100px;
            background:rebeccapurple;
            position: absolute;
            left:0;
            top:0;
            right:0;
            bottom:0;
            margin:auto;
        } */
        /* 第二种 */
        /* .container div{
            width: 200px;
            height: 200px;
            background:red;
            position: absolute;
            left:50%;
            top:50%;
            margin-left:-100px;
            margin-top:-100px;
        } */
        /* 第三种 */
        /* .container div{
            width: 200px;
            height: 200px;
            background:red;
            position: absolute;
            top:50%;
            left:50%;
            transform: translate(-50%,-50%);
        } */
        /* 第四种 弹性布局*/
        /* .container div{
            width: 200px;
            height: 200px;
            background-color: salmon;
        } */
        /* 第五种 */
        /* .container div{
            position: absolute;
            background-color: salmon;
            width: 200px;
            height: 200px;
            left:calc((500px - 200px)/2);
            top:calc((500px - 200px)/2);
            left:-webkit-calc((500px - 200px)/2);
            top:-webkit-calc((500px - 200px)/2);
        } */
        /* 第六种 */
        /* .container div{
            width: 200px;
            height: 200px;
            display: table-cell;
            vertical-align: middle;
            text-align: center;
            background:red;
        } */
        /* 第七种 */
        .container div{
            width: 200px;
            height: 200px;
            background-color: slateblue;
            margin:auto;
            }

结果

 

posted @ 2020-07-27 21:31  Running°つ  阅读(421)  评论(0编辑  收藏  举报