CSS水平居中+垂直居中+水平/垂直居中的方法總結(jié)

原文鏈接:https://blog.csdn.net/weixin_37580235/article/details/82317240

水平居中

行內(nèi)元素

首先看它的父元素是不是塊級(jí)元素熟呛,如果是益咬,則直接給父元素設(shè)置 text-align: center;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        text-align: center;
       }
</style>
 
<div id="father">
    <span id="son">我是行內(nèi)元素</span>
</div>

如果不是签财,則先將其父元素設(shè)置為塊級(jí)元素未蝌,再給父元素設(shè)置 text-align: center;

<style>
    #father {
        display: block;
        width: 500px;
        height: 300px;
        background-color: skyblue;
        text-align: center;
       }
</style>
 
<span id="father">
    <span id="son">我是行內(nèi)元素</span>
</span>

效果:

image.png

塊級(jí)元素

方案一:(分寬度定不定兩種情況)

定寬度:需要誰(shuí)居中,給其設(shè)置 margin: 0 auto; (作用:使盒子自己居中)

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
    }
 
    #son {
        width: 100px;
        height: 100px;
        background-color: green;
        margin: 0 auto;
    }
</style>
 
<div id="father">
    <div id="son">我是塊級(jí)元素</div>
</div>

效果:


image.png

不定寬度:默認(rèn)子元素的寬度和父元素一樣,這時(shí)需要設(shè)置子元素為display: inline-block; 或 display: inline;即將其轉(zhuǎn)換成行內(nèi)塊級(jí)/行內(nèi)元素,給父元素設(shè)置 text-align: center;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        text-align: center;
    }
 
    #son {
        background-color: green;
        display: inline;
    }
</style>
 
<div id="father">
    <div id="son">我是塊級(jí)元素</div>
</div>

效果:(將#son轉(zhuǎn)換成行內(nèi)元素要尔,內(nèi)容的高度撐起了#son的高度,設(shè)置高度無(wú)用)

image.png

方案二:使用定位屬性

首先設(shè)置父元素為相對(duì)定位新娜,再設(shè)置子元素為絕對(duì)定位赵辕,設(shè)置子元素的left:50%,即讓子元素的左上角水平居中概龄;

定寬度:設(shè)置絕對(duì)子元素的 margin-left: -元素寬度的一半px; 或者設(shè)置transform: translateX(-50%);

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        position: relative;
}
 
    #son {
        width: 100px;
        height: 100px;
        background-color: green;
        position: absolute;
        left: 50%;
        margin-left: -50px;
}
</style>
 
<div id="father">
    <div id="son">我是塊級(jí)元素</div>
</div>

不定寬度:利用css3新增屬性transform: translateX(-50%);

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        position: relative;
}
 
    #son {
        height: 100px;
        background-color: green;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
}
</style>
 
<div id="father">
    <div id="son">我是塊級(jí)元素</div>
</div>

效果:


image.png

方案三:使用flexbox布局實(shí)現(xiàn)(寬度定不定都可以)

使用flexbox布局还惠,只需要給待處理的塊狀元素的父元素添加屬性 display: flex; justify-content: center;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        display: flex;
        justify-content: center;
    }
 
    #son {
        width: 100px;
        height: 100px;
        background-color: green;
    }
</style>
 
<div id="father">
    <div id="son">我是塊級(jí)元素</div>
</div>

效果:


image.png

垂直居中

單行的行內(nèi)元素

只需要設(shè)置單行行內(nèi)元素的"行高等于盒子的高"即可;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
    }
 
    #son {
        background-color: green;
        height: 300px;
    }
</style>
 
<div id="father">
    <span id="son">我是單行的行內(nèi)元素</span>
</div>

效果:

image.png

多行的行內(nèi)元素

使用給父元素設(shè)置display:table-cell;和vertical-align: middle;屬即可旁钧;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        display: table-cell;
        vertical-align:middle;
    }
 
    #son {
        background-color: green;
    }
</style>
 
<div id="father">
    <span id="son">我是多行的行內(nèi)元素我是多行的行內(nèi)元素我是多行的行內(nèi)元素我是多行的行內(nèi)元素我是多行的行內(nèi)元素我是多行的行內(nèi)元素我是多行的行內(nèi)元素我是多行的行內(nèi)元素</span>
</div>

效果:

image.png

塊級(jí)元素

方案一:使用定位

首先設(shè)置父元素為相對(duì)定位,再設(shè)置子元素為絕對(duì)定位互拾,設(shè)置子元素的top: 50%歪今,即讓子元素的左上角垂直居中;

定高度:設(shè)置絕對(duì)子元素的 margin-top: -元素高度的一半px; 或者設(shè)置transform: translateY(-50%);

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        position: relative;
}
 
    #son {
        height: 100px;
        background-color: green;
        position: absolute;
        top: 50%;
        margin-top: -50px;
}
</style>
 
<div id="father">
    <div id="son">我是塊級(jí)元素</div>
</div>

不定高度:利用css3新增屬性transform: translateY(-50%);

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        position: relative;
}
 
    #son {
        width: 100px;
        background-color: green;
        position: absolute;
        left: 50%;
        transform: translateY(-50%);
}
</style>
 
<div id="father">
    <div id="son">我是塊級(jí)元素</div>
</div>

效果:

image.png

方案二:使用flexbox布局實(shí)現(xiàn)(高度定不定都可以)

使用flexbox布局颜矿,只需要給待處理的塊狀元素的父元素添加屬性 display: flex; align-items: center;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        display: flex;
        align-items: center;
    }
 
    #son {
        width: 100px;
        height: 100px;
        background-color: green;
    }
</style>
 
<div id="father">
    <div id="son">我是塊級(jí)元素</div>
</div>

效果:

image.png

水平垂直居中

已知高度和寬度的元素

方案一:設(shè)置父元素為相對(duì)定位寄猩,給子元素設(shè)置絕對(duì)定位,top: 0; right: 0; bottom: 0; left: 0; margin: auto;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        position: relative;
}
 
    #son {
        width: 100px;
        height: 100px;
        background-color: green;
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        margin: auto;
}
</style>
 
<div id="father">
    <div id="son">我是塊級(jí)元素</div>
</div>

效果:

image.png

方案二:設(shè)置父元素為相對(duì)定位骑疆,給子元素設(shè)置絕對(duì)定位田篇,left: 50%; top: 50%; margin-left: --元素寬度的一半px; margin-top: --元素高度的一半px;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        position: relative;
}
 
    #son {
        width: 100px;
        height: 100px;
        background-color: green;
        position: absolute;
        left: 50%;
        top: 50%;
        margin-left: -50px;
        margin-top: -50px;
}
</style>
 
<div id="father">
    <div id="son">我是塊級(jí)元素</div>
</div>

效果:

image.png

未知高度和寬度的元素

方案一:使用定位屬性

設(shè)置父元素為相對(duì)定位,給子元素設(shè)置絕對(duì)定位箍铭,left: 50%; top: 50%; transform: translateX(-50%) translateY(-50%);

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        position: relative;
}
 
    #son {
        background-color: green;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translateX(-50%) translateY(-50%);
}
</style>
 
<div id="father">
    <div id="son">我是塊級(jí)元素</div>
</div>

效果:

image.png

方案二:使用flex布局實(shí)現(xiàn)

設(shè)置父元素為flex定位泊柬,justify-content: center; align-items: center;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        display: flex;
        justify-content: center;
        align-items: center;
}
 
    #son {
        background-color: green;
}
</style>
 
<div id="father">
    <div id="son">我是塊級(jí)元素</div>
</div>

效果:


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市诈火,隨后出現(xiàn)的幾起案子兽赁,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件刀崖,死亡現(xiàn)場(chǎng)離奇詭異惊科,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)亮钦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門馆截,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人蜂莉,你說(shuō)我怎么就攤上這事蜡娶。” “怎么了巡语?”我有些...
    開(kāi)封第一講書人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵妓柜,是天一觀的道長(zhǎng)楷扬。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么管行? 我笑而不...
    開(kāi)封第一講書人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮倦始,結(jié)果婚禮上括授,老公的妹妹穿的比我還像新娘。我一直安慰自己踏拜,他們只是感情好碎赢,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著速梗,像睡著了一般肮塞。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上姻锁,一...
    開(kāi)封第一講書人閱讀 51,631評(píng)論 1 305
  • 那天枕赵,我揣著相機(jī)與錄音,去河邊找鬼位隶。 笑死拷窜,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的涧黄。 我是一名探鬼主播篮昧,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼笋妥!你這毒婦竟也來(lái)了懊昨?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤春宣,失蹤者是張志新(化名)和其女友劉穎疚颊,沒(méi)想到半個(gè)月后狈孔,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡材义,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年均抽,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片其掂。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡油挥,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出款熬,到底是詐尸還是另有隱情深寥,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布贤牛,位于F島的核電站惋鹅,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏殉簸。R本人自食惡果不足惜闰集,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望般卑。 院中可真熱鬧武鲁,春花似錦、人聲如沸蝠检。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)叹谁。三九已至饲梭,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間焰檩,已是汗流浹背憔涉。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留锅尘,地道東北人监氢。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓布蔗,卻偏偏與公主長(zhǎng)得像藤违,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子纵揍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容