不定寬高的盒子水平垂直居中的方法?

方法1:

div絕對(duì)定位水平垂直居中【margin 負(fù)間距】? ? 這或許是當(dāng)前最流行的使用方法。

div{

? ? ? ? ? ? width:200px;

? ? ? ? ? ? height: 200px;

? ? ? ? ? ? background:green;

? ? ? ? ? ? position: absolute;

? ? ? ? ? ? left:50%;

? ? ? ? ? ? top:50%;

? ? ? ? ? ? margin-left:-100px;

? ? ? ? ? ? margin-top:-100px;

? ? ? ? }


方法2:

div絕對(duì)定位水平垂直居中【margin:auto實(shí)現(xiàn)絕對(duì)定位元素的居中】

div{

? ? ? ? ? ? width: 200px;

? ? ? ? ? ? height: 200px;

? ? ? ? ? ? background: green;

? ? ? ? ? ? position:absolute;

? ? ? ? ? ? left:0;

? ? ? ? ? ? top: 0;

? ? ? ? ? ? bottom: 0;

? ? ? ? ? ? right: 0;

? ? ? ? ? ? margin: auto;

? ? ? ? }

方案3:

div絕對(duì)定位水平垂直居中【Transforms 位移】

div{

? ? ? ? ? ? width: 200px;

? ? ? ? ? ? height: 200px;

? ? ? ? ? ? background: green;

? ? ? ? ? ? position:absolute;

? ? ? ? ? ? left:50%;/* 定位父級(jí)的50% */

? ? ? ? ? ? top:50%;

? ? ? ? ? ? transform: translate(-50%,-50%); /*自己的50% */

? ? ? ? }

方案4:

flex彈性布局居中

.box{

? ? ? ? ? ?? height:600px;

? ? ? ? ? ?? display: -webkit-flex;

? ? ? ? ? ? ?display: flex;

? ? ? ? ? ?? justify-content:center;

? ? ? ? ? ?? align-items:center;

? ? ? ? }

?著作權(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 方案一 div絕對(duì)定位水平垂直居中【margin:auto實(shí)現(xiàn)絕對(duì)定位元素的居中】 div{ width...
    愛前端的喵喵閱讀 4,264評(píng)論 0 1
  • div水平居中的N種方法 一、單行垂直居中 如果一個(gè)容器中只有一行文字,對(duì)它實(shí)現(xiàn)居中相對(duì)比較簡單,我們只需要設(shè)置它...
    fredah閱讀 6,048評(píng)論 0 1
  • 1.div絕對(duì)定位水平垂直居中【margin:auto實(shí)現(xiàn)絕對(duì)定位元素的居中】 兼容性:,IE7及之前版本不支持 ...
    凱凱_閱讀 481評(píng)論 0 0
  • 發(fā)現(xiàn)了一個(gè)公眾號(hào),他們文章全是標(biāo)題黨,而且……標(biāo)題和內(nèi)容還真有關(guān)系,比如: 《注意!千萬不要喝蜂蜜啦,出大事兒了》...
    卡薩布蘭史塔克的卡閱讀 441評(píng)論 0 0
  • 在00:00的時(shí)候,心情賊不美麗的我可煩躁了。 沒到十二點(diǎn)之前,有作業(yè)陪著我,有游戲陪著我,有朋友陪著我,距離十...
    神賜的公主閱讀 179評(píng)論 3 1

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