溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

css中有幾種方法可以實現(xiàn)水平垂直居中

發(fā)布時間:2020-06-05 16:01:00 來源:億速云 閱讀:204 作者:Leah 欄目:web開發(fā)

css中有幾種方法可以實現(xiàn)水平垂直居中?針對這個問題,今天小編總結(jié)這篇有關(guān)水平垂直居中的文章,可供感興趣的小伙伴們參考借鑒,希望對大家有所幫助。

水平居中
(1)使用inline-block+text-align

<div class="parent">
<div class="child">demo</div>
</div>

   .child {

            display:inline-block;        }        .parent {             text-align:center        }    原理:先將子框由塊級元素改變?yōu)樾袃?nèi)塊元素,再通過設(shè)置行內(nèi)塊元素居中以達到水平居中。    優(yōu)點:兼容性好,甚至可以兼容ie6、ie7    (2)使用table+margin        .child {            display:table            margin:0 auto;        } 原理:先將子框設(shè)置為塊級表格來顯示,再設(shè)置子框居中以達到水平居中。 缺點:不支持ie6、ie7,將div換成table (3)使用absolute+transform    .child {                 position:absolute;                 left:50%;                 transform:translateX(-50%)        }        .parent {                  position:relative        } 缺點:transform屬于css3內(nèi)容,兼容性存在一定問題,高版本瀏覽器需要添加一些前綴 (4)使用flex+margin        .child {          margin:0 auto        }        .parent {                display:flex        } 缺點:低版本瀏覽器(ie6 ie7 ie8)不支持 (5)使用flex+justify-content    .parent {                display:flex;                justify-content:center        }    缺點:低版本瀏覽器(ie6 ie7 ie8)不支持    垂直居中    (1)使用table-cell+vertical-align        .parent {                display:table-cell;                vertical-align:middle        }    (2)使用absolute+transform        .child {          position:absolute;            top:50%;            transform:translateY(-50%)        }        .parent {                position:relative        } 缺點:transform屬于css3內(nèi)容,兼容性存在一定問題,高版本瀏覽器需要添加一些前綴    (3)使用flex+align-items        .parent {            display:flex;            align-items:center;        }    水平垂直居中    (1)使用absolute+transform(未知高度)    .parent {            position:relative;        }    .child {            position:absolute;            left:-50%;            top:-50%            transform:translate(-50%,-50%)        }    (1.1)使用absolute+transform(已知高度)    .parent {            position:relative;        }    .child {            position:absolute;            width:100px;            height:100px;            left:-50%;            top:-50%          margin: -50px 0 0 -50px;        }        (2)使用inline-block+text-align+table-cell+vertical-align        .parent {            text-align:center;            display:table-cell;            vertical-align:middle;        }        .child {                display:inline-block;        }        優(yōu)點:兼容性較好        (3)使用flex+justify-content+align-items        .parent {            display:flex;            justify-content:center;            align-items:center;        }        缺點:兼容性存在一定問題

關(guān)于css中實現(xiàn)水平垂直居中的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果喜歡這篇文章,不如把它分享出去讓更多的人看到。


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI