块居中 总结

块居中 总结_第1张图片

margin:

块居中 总结_第2张图片



    

定位

方法1:left,right,top,bottom:0;

.outer{
        width: 400px;
        height: 300px;
        background-color: pink;
        position:relative;
    }
    .inner{
        width: 200px;
        height: 100px;
        background-color: aquamarine;
        position: absolute;
        left:0;
        right: 0;
        top: 0;
        bottom: 0;
        margin:auto;
    }

块居中 总结_第3张图片

方法2-1:left:50%;top:50%;  margin-left  margin-right

.outer{
        width: 400px;
        height: 300px;
        background-color: pink;
        position:relative;
    }
    .inner{
        width: 200px;
        height: 100px;
        background-color: aquamarine;
        position: absolute;
        left: 50%;
        top: 50%;
        margin-left:-100px;
        margin-top: -50px;
    }

方法2-2:left:50%;top:50%;  transform:translate()

    .outer{
        width: 400px;
        height: 300px;
        background-color: pink;
        position:relative;
    }
    .inner{
        width: 200px;
        height: 100px;
        background-color: aquamarine;
        position: absolute;
        left: 50%;
        top: 50%;
        /* margin-left:-100px;
        margin-top: -50px; */
        transform: translate(-50%,-50%);
    }

flex

 .outer{
        width: 400px;
        height: 300px;
        background-color: pink;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .inner{
        width: 200px;
        height: 100px;
        background-color: aquamarine;
    }

你可能感兴趣的:(css,html,css,html)