DIV水平垂直居中

 

<div class="father">
        <div class="son"></div>
</div>

方案一:經過父相子絕 自走父的50%再回走自身的一半佈局

   .father {
            margin: 0 auto;
            width: 400px;
            height: 400px;
            background-color: black;
            position: relative;
        }
        
    .son {
            width: 100px;
            height: 100px;
            background-color: hotpink;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -50px;
            margin-top: -50px;
        }

方案二:經過父相子絕  子 margin: auto; top: 0;left: 0; right: 0;bottom: 0;flex

.father {
            margin: 0 auto;
            width: 400px;
            height: 400px;
            background-color: black;
            position: relative;
        }
        
.son {
            width: 100px;
            height: 100px;
            background-color: hotpink;
            position: absolute;
            margin: auto;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
        }

方案三:父相子絕 利用父的text-align實現子的居中 子須要轉行內塊 並回走自身一半spa

        .father {
            margin: 0 auto;
            width: 400px;
            height: 400px;
            background-color: black;
            position: relative;
            text-align: center;
           
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: hotpink;
            display: inline-block;
            position: absolute;
            top: 50%;
            margin-left: -50px;
            margin-top: -50px;
        }

方案四:父相子絕 子走父50% 再過渡回本身的一半code

        .father {
            margin: 0 auto;
            width: 400px;
            height: 400px;
            background-color: black;
            position: relative;
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: hotpink;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

方案五:flex伸縮佈局 主軸 副軸 都居中orm

        .father {
            margin: 0 auto;
            width: 400px;
            height: 400px;
            background-color: black;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: hotpink;
        }
相關文章
相關標籤/搜索