元素居中几种写法

    /**
         * 兼容所有浏览器,不兼容移动端
         * 元素必须有固定宽度高度
         */
        .box{
             320px;
            height: 320px;
            background: red;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-top: -160px;
            margin-left: -160px;
        }


/**
         * 绝对居中法
         * 兼容ie8以上浏览器
         * 元素必须有固定宽度高度,可以百分比
         */
        .box{
             320px;
            height: 320px;
            background: red;
            margin: auto;
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
        }


/**
         * transform css3特性 平移
         * 兼容ie9以上版本浏览器,适合移动端元素居中
         * 元素无需固定宽高,宽高可以设置百分比
         */
        .box{
             320px;
            height: 320px;
            background: red;
            position: absolute;
            left: 50%;
            top:50%;
            transform: translate(-50%, -50%);
        }


/**
         * flex弹性盒模型居中法
         * 兼容ie10以上浏览器
         * 元素无需固定宽高
         * 灵活
         */
        .wrap{
             880px;
            height: 550px;
            border: 1px solid  red;
            margin: 10px auto;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .box{
             400px;
            height: 200px;
            background: green;
        }
原文地址:https://www.cnblogs.com/lzy007/p/8146331.html