transform 方法

.father {
        width: 500px;
        height: 500px;
        background-color: blueviolet;
    }
    
    .son {
        position:relative;
        width: 200px;
        height: 200px;
        background-color: pink;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }


子绝父相

  .father {
        position: relative;
        width: 500px;
        height: 500px;
        background-color: blueviolet;
    }
    
    .son {
        position: absolute;
        width: 200px;
        height: 200px;
        margin: auto;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        background-color: pink;
    }


flex布局 display flex

        .father {
        display: flex;
        width: 500px;
        height: 500px;
        background-color: blueviolet;
        justify-content: center;
        align-items: center;
    }
    
    .son {
        width: 200px;
        height: 200px;
        background-color: pink;
    }


margin-left 自身一半的宽度
margin-top 自身一半的高度

 .father {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 200px;
        height: 100px;
        margin-left: -100px;
        margin-top: -50px;
        background-color: pink;
    }

发表评论