垂直居中的幾種方法

1css定位

    <style>
        .wrap {
            position: relative;
            background: #333;
            height: 300px;
            width: 300px;
        }
        .box {
            position: absolute;
            top: 50%;
            left: 50%;
            background: #999;
            width: 100px;
            height: 100px;
            margin: -50px 0 0 -50px;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="box">
        </div>
    </div>
</body>
image.png

適用于父容器和子容器的寬高是確定的。

2.transition

    <style>
        .wrap {
            position: relative;
            background: #333;
            height: 300px;
            width: 300px;
        }
        .box {
            position: absolute;
            top: 50%;
            left: 50%;
            background: #999;
            width: 100px;
            height: 100px;
            transform: translate(-50%, -50%)
        }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="box">
        </div>
    </div>
</body>
image.png

利用c3的動(dòng)畫屬性

3.強(qiáng)大的flex

    <style>
        .wrap {
            position: relative;
            background: #333;
            height: 300px;
            width: 300px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .box {
            background: #999;
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <div class="box">
        </div>
    </div>
</body>
image.png

定義父容器為flex容器,添加設(shè)置主軸和次主軸居中 justify-content: center; align-items: center;代碼簡(jiǎn)潔優(yōu)雅

4.設(shè)置table的屬性,模擬表格定位

    <style>
        .table {
            display: table;
            height: 300px;
            background: #aaa;
        }

        .cell {
            display: table-cell;
            width: 300px;
            background: #999;
            vertical-align: middle;
            text-align: center;
        }
        .img {
            width: 100px;
            height: 100px;
            background: #333;
            display: inline-block;
        }
    </style>
</head>
<body>
    <div class="table">
        <div class="cell">
            <div class="img">
            </div>
        </div>
    </div>
</body>

頁(yè)面看成一個(gè)表格的思想,也很不錯(cuò)


用table完成一個(gè)兩邊固定,中間自適應(yīng)(圖片居中)的經(jīng)典布局:

  <style>
        .table {
            display: table;
            width: 100%;
            background: #aaa;
        }

        .cell {
            display: table-cell;
            /* background: #999; */
            vertical-align: middle;
            text-align: center;
        }
        .table .left,.table .right {
            width: 300px;
            height: 300px;
            background: burlywood;
        }
        .img {
            width: 100px;
            height: 100px;
            background: #333;
            display: inline-block;
        }
    </style>
</head>
<body>
    <div class="table">
        <div class="cell left"></div>
        <div class="cell">
            <div class="img">
            </div>
        </div>
        <div class="cell right"></div>
    </div>
</body>
image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容