CSS box vertical centering method

1. The method of centering the box vertically
  1. Let the upper and lower edges of the box overlap with the horizontal center line of the parent box, and then let the child box move back half of its distance.

  <div class="father">
        <div class="son"></div>
    </div>
  /* 通过 transform 属性来移动*/
    .father {
    
    
        width: 500px;
        height: 500px;
        background-color: skyblue;
        border: 1px solid #000;
        margin: 0 auto;
    }

    .son {
    
    
        width: 200px;
        height: 200px;
        background-color: pink;
        border: 1px solid #000;
        margin-top: 50%; // 向下移动父盒子的一半
        transform: translateY(-50%); // 向上移动自身盒子的一半
    }  
     /* 通过 定位来移动*/
    .father {
    
    
        width: 500px;
        height: 500px;
        background-color: skyblue;
        border: 1px solid #000;
        margin: 0 auto;
        position: relative;
    }

    .son {
    
    
        width: 200px;
        height: 200px;
        background-color: pink;
        border: 1px solid #000;
        position: absolute;
        top: 50%; // 先向下移动父盒子的一半
        margin-top: -100px; // 再向上移动自身盒子的一半

    }

Insert picture description here
3. Knowing the height of the parent box, you can use margin to calculate the top and bottom margins of the box to center the box.

.father {
    
    
            width: 500px;
            height: 500px;
            background-color: skyblue;
            border: 1px solid #000;
            margin: 50px auto;

        }
        .son {
    
    
            width: 200px;
            height: 200px;
            background-color: pink;
            border: 1px solid #000;
            margin-top: 149px;         // 根据父盒子的高度指定 margin-top 即可
        }

Second, the method of horizontally centering the box
  1. Use margin: 0 auto;

.father {
    
    
            width: 500px;
            height: 500px;
            background-color: skyblue;
            border: 1px solid #000;
            margin: 50px auto;

        }
        .son {
    
    
            width: 200px;
            height: 200px;
            background-color: pink;
            border: 1px solid #000;
            margin: 0 auto;           // 让盒子左右自动适应,想当于 left:auto; right:auto
        }

2. Achieve centering by calculating the left and right margins of the margin

.father {
    
    
            width: 500px;
            height: 500px;
            background-color: skyblue;
            border: 1px solid #000;
            margin: 50px auto;

        }
        .son {
    
    
            width: 200px;
            height: 200px;
            background-color: pink;
            border: 1px solid #000;
            margin-left: 149px;          // 父盒子的定宽的,指定 margin-left 即可
        }

3. First make the left and right edges of the box perpendicular to the vertical center line of the parent box, and then move the child box back by half of its own width

/* 通过 transform 实现*/
    .father {
    
    
            width: 500px;
            height: 500px;
            background-color: skyblue;
            border: 1px solid #000;
            margin: 50px auto;

        }
        .son {
    
    
            width: 200px;
            height: 200px;
            background-color: pink;
            border: 1px solid #000;
            margin-left: 50%;                // 先移动父盒子的一半
            transform: translateX(-50%);     // 再移动自身盒子一半

        }
    /*通过 定位实现*/
    .father {
    
    
            width: 500px;
            height: 500px;
            background-color: skyblue;
            border: 1px solid #000;
            margin: 50px auto;
            position: relative;

        }
        .son {
    
    
            width: 200px;
            height: 200px;
            background-color: pink;
            border: 1px solid #000;
            position: absolute;
            left: 50%;                       // 向右移动父盒子一半
            margin-left: -100px;             // 向左移动自身盒子一半
            /* transform: translateX(-50%); */    //向左移动自身盒子一半 38 }

4. Turn the box into an inline block, and then use the text-align attribute to center the box horizontally

.father {
    
    
            width: 500px;
            height: 500px;
            background-color: skyblue;
            border: 1px solid #000;
            margin: 50px auto;
            text-align: center;               // 让父盒子设置水平居中

        }
        .son {
    
    
            width: 200px;
            height: 200px;
            background-color: pink;
            border: 1px solid #000;
            display: inline-block;            // 让子盒子显示为行内块模式
        }

Guess you like

Origin blog.csdn.net/weixin_44433499/article/details/107412988