css实现水平垂直居中

一、基于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;
        } 

二、基于定位flex

      .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%);
        }    

三、基于表格样式spa

    .big{
            width: 300px;
            height: 300px;
            background-color: red;
            display: table-cell;
            vertical-align: middle;
        }
        .small{
            width: 100px;
            height: 100px;
            background-color: green;
            margin: auto;
        }
相关文章
相关标签/搜索