css实现水平垂直居中

1、基于flex布局

     .big{
            width: 300px;
            height: 300px;
            background-color: red;
            display: flex;
            /*
            justify-content: center;
            align-items: center;
            */
        }
        .small{
            width: 100px;
            height: 100px;
            background-color: green;
            margin: auto;
        } 

2、基于定位

      .big{
            width: 300px;
            height: 300px;
            background-color: red;
            position: relative;
        }
        .small{
            width: 100px;
            height: 100px;
            background-color: green;
            position: absolute;
            left: 50%;
            top: 50%;
            /*
            margin-left: -50px;
            margin-top: -50px;
            */
            transform: translate(-50%,-50%);
        }    

3、基于表格样式

    .big{
            width: 300px;
            height: 300px;
            background-color: red;
            display: table-cell;
            vertical-align: middle;
        }
        .small{
            width: 100px;
            height: 100px;
            background-color: green;
            margin: auto;
        }

 

posted @ 2019-03-31 09:08  wenzizaigaoge  阅读(86)  评论(0编辑  收藏  举报