CSS布局之如何實(shí)現(xiàn)居中布局
1. 父級(jí)容器設(shè)置成表格,子級(jí)設(shè)為行內(nèi)元素。
適合子級(jí)內(nèi)容為文本展示。

<!-- css -->
<style>
#parent {
height: 200px;
width: 200px;
border: 1px solid red;
display: table-cell; /* 轉(zhuǎn)變成表格 */
text-align: center; /* 水平 */
vertical-align: middle; /* 垂直 */
}
#child {
background-color: blue;
color: white;
display: inline; /* 子元素設(shè)置為行內(nèi)或行內(nèi)塊 */
}
</style>
<!-- html -->
<div id="parent">
<div id="child">內(nèi)容</div>
</div>
2. 父級(jí)容器設(shè)置相對(duì)定位,子級(jí)設(shè)置絕對(duì)定位后通過外邊距居中。

<!-- css -->
<style>
#parent {
height: 200px;
width: 200px;
border: 1px solid red;
position: relative; /* 設(shè)置相對(duì)定位 */
}
#child {
height: 50px;
width: 50px;
color: white;
background-color: blue;
/* 絕對(duì)定位,4 個(gè)方向設(shè)置為 0 后,margin 設(shè)為 auto */
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
<!-- html -->
<div id="parent">
<div id="child"></div>
</div>
3. 父級(jí)容器設(shè)置為彈性盒,子級(jí)設(shè)置外邊距。

<!-- css -->
<style>
#parent {
height: 200px;
width: 200px;
border: 1px solid red;
display: flex; /* 父元素轉(zhuǎn)換為彈性盒 */
display: -webkit-flex; /* Safari */
}
#child {
height: 50px;
width: 50px;
background-color: blue;
margin: auto;
}
</style>
<!-- html -->
<div id="parent">
<div id="child"></div>
</div>
4. 父級(jí)容器設(shè)置相對(duì)定位,子級(jí)設(shè)置絕對(duì)定位,左邊距和上邊距設(shè)置負(fù)一半寬度。
適合子級(jí)的寬高固定的情況。

<!-- css -->
<style>
#parent {
height: 200px;
width: 200px;
border: 1px solid red;
position: relative; /* 設(shè)置相對(duì)定位 */
}
#child { /* 子元素已知自身寬高的情況下 */
background-color: blue;
width: 50px;
height: 50px;
margin-top: -25px;
margin-left: -25px;
position: absolute;
left: 50%;
top: 50%;
}
</style>
<!-- html -->
<div id="parent">
<div id="child"></div>
</div>
5. 父級(jí)容器設(shè)置相對(duì)定位,子級(jí)設(shè)置絕對(duì)定位,通過變形屬性設(shè)置水平和垂直方向負(fù)一半。
適合子級(jí)的寬高不固定的情況。

<!-- css -->
<style>
#parent {
height: 200px;
width: 200px;
border: 1px solid red;
position: relative; /* 設(shè)置相對(duì)定位 */
}
#child { /* 子元素未知自己的寬高,使用 transform 的 translate */
border: 1px solid blue;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%,-50%);
-ms-transform: translate(-50%,-50%);
-o-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
}
</style>
<!-- html -->
<div id="parent">
<div id="child">
<div id="content">
內(nèi)容1
<br/>
內(nèi)容2
</div>
</div>
</div>
6. 父級(jí)設(shè)置為彈性盒,設(shè)置對(duì)齊屬性。

<!-- css -->
<style>
#parent {
height: 200px;
width: 200px;
border: 1px solid red;
display: flex; /* 父元素轉(zhuǎn)換為彈性盒 */
display: -webkit-flex; /* Safari */
justify-content: center;/* 水平 */
align-items: center; /* 垂直 */
}
#child {
height: 50px;
width: 50px;
background-color: blue;
}
</style>
<!-- html -->
<div id="parent">
<div id="child"></div>
</div>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
- 在前端開發(fā)中,我們經(jīng)常會(huì)遇到各種上不同場(chǎng)景的關(guān)于居中的布局,這篇文章主要介紹了CSS之居中布局的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起2019-04-08
- 居中是我們使用css來(lái)布局時(shí)常遇到的情況。使用css來(lái)進(jìn)行居中時(shí),有時(shí)一個(gè)屬性就能搞定,本篇文章主要介紹了CSS布局奇淫技巧之--各種居中,有興趣的可以了解一下。2016-12-09
- 這篇文章主要針對(duì)CSS各種居中布局方法為大家進(jìn)行詳細(xì)匯總,感興趣的小伙伴們可以參考一下2016-01-22
- 這篇文章主要介紹了CSS元素居中布局的簡(jiǎn)單方法,文中介紹了內(nèi)嵌元素和塊元素以及行內(nèi)塊三種情況,需要的朋友可以參考下2015-07-18
CSS網(wǎng)頁(yè)布局:div垂直居中的各種方法-CSS教程-網(wǎng)頁(yè)制作-網(wǎng)頁(yè)教學(xué)網(wǎng)
在前面的文章中我簡(jiǎn)單總結(jié)了一個(gè)“CSS在頁(yè)面布局中實(shí)現(xiàn)div水平居中的方法”,其實(shí)水平居中實(shí)現(xiàn)還是比較簡(jiǎn)單的,反而垂直居中有點(diǎn)麻煩,因?yàn)槲覀冊(cè)O(shè)計(jì)頁(yè)面的時(shí)候往2008-10-17CSS網(wǎng)頁(yè)布局:div水平居中的各種方法-CSS教程-網(wǎng)頁(yè)制作-網(wǎng)頁(yè)教學(xué)網(wǎng)
在Web標(biāo)準(zhǔn)中的頁(yè)面布局是使用Div配合CSS來(lái)實(shí)現(xiàn)的。這其中最常用到的就是使整個(gè)頁(yè)面水平居中的效果,這是在頁(yè)面布局中基本,也是最應(yīng)該首先掌握的知識(shí)。不過,還是經(jīng)常會(huì)有2008-10-17CSS高級(jí)技巧:網(wǎng)頁(yè)布局-CSS教程-網(wǎng)頁(yè)制作-網(wǎng)頁(yè)教學(xué)網(wǎng)
上一篇CSS教程 文章:CSS高級(jí)技巧:文字環(huán)繞圖片 布局 CSS至關(guān)重要的作用, CSS的設(shè)計(jì)初衷. CSS布局和幾年前table橫行時(shí)的布局又不太一樣, 在結(jié)構(gòu)化語(yǔ)義化的HTML文檔2008-10-17

