盒子水平垂直居中常用的六种方法

方法一:利用定位

父盒子使用相对定位,子盒子使用绝对定位,利用top:50% left:50% 让子盒子相距父盒子上边界,左边界宽高的50% 利用 margin-top margin-left返回子盒子自身宽高的50%

 .parent {
    
    
            width: 500px;
            height: 500px;
            background-color: blanchedalmond;
            /* 父盒子开启相对定位 */
            position: relative;
        }
 
        .child {
    
    
            width: 200px;
            height: 200px;
            background-color: aqua;
            /* 父盒子开启绝对定位 */
            position: absolute;
 
            top: 50%;
            /* 相当于父盒子上边界走父盒子高度的50% */
            left: 50%;
            /*相对于父盒子左边界走父盒子宽度的50%*/
            margin-top: -100px;
            /*向上走回自身高度的一半*/
            margin-left: -100px;
            /*向左走回自身宽度的一半*/
 
        }

方法二:利用margin:auto

/* 父盒子设置相对定位 */
        .parent {
    
    
            width: 500px;
            height: 500px;
            border: 1px solid #000;
            position: relative;
        }
        /* 子盒子设置绝对定位,将top,left,right,bottom设置为0 设置margin为auto  通过计算所得,能实现垂直居中。 */
        .child {
    
    
            width: 100px;
            height: 100px;
            border: 1px solid #999;
            position: absolute;
            margin: auto;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
        }

方法三:利用display:table-cell

.parent {
    
    
            width: 500px;
            height: 500px;
            background-color: blanchedalmond;
            /* 会使元素表现的类似一个表格中的单元格td,利用这个特性可以实现文字的垂直居中效果 */
            display: table-cell;
            vertical-align: middle;
            text-align: center;
        }
 
        .child {
    
    
            width: 200px;
            height: 200px;
            background-color: aqua;
            display: inline-block;
        }

方法四:利用display:flex

 .parent {
    
    
            width: 500px;
            height: 500px;
            background-color: blanchedalmond;
            display: flex;
            /* 主轴居中 */
            justify-content: center;
            /* 从轴居中 */
            align-items: center;
        }
 
        .child {
    
    
            width: 200px;
            height: 200px;
            background-color: aqua;
        }

方法五:计算父盒子与子盒子的空间距离(这跟方法一是一个道理)

.parent {
    
    
            width: 500px;
            height: 500px;
            background-color: blanchedalmond;
            /*父盒子添加边边框,防止外边距塌陷*/
            border: 1px solid transparent;
        }
        .child {
    
    
            width: 200px;
            height: 200px;
            background-color: aqua;
            margin-top: 150px;
            margin-left: 150px;
        }

方法六: 利用transform(变换)实现居中

 /* 父盒子设置相对定位 */
        .parent {
    
    
            width: 500px;
            height: 500px;
            background-color: blanchedalmond;
            position: relative;
        }
 
        .child {
    
    
            width: 200px;
            height: 200px;
            background-color: aqua;
            position: absolute;
            margin: auto;
            top: 50%;
            left: 50%;
            /* translate中的%百分比相对的是自身的 也就是向左向上走自身的%50来实现居中效果 */
            transform: translate(-50%, -50%);
             
        }

猜你喜欢

转载自blog.csdn.net/seimeii/article/details/130767829