CSS實(shí)現(xiàn)垂直居中的7種方法

1、設(shè)定行高(line-height)

HTML:

<div class="parent">
    <span class="child">你好</span>
</div>

CSS:

.parent{
    width:200px;
    height:200px;
    border:1px solid red;
}
.child{
    width:200px;
    line-height: 200px;   
    text-align: center; //水平居中
    display: inline-block;
}
效果實(shí)現(xiàn)

重點(diǎn):父容器高度和子元素line-height一樣的數(shù)值,內(nèi)容中的行內(nèi)元素就會(huì)垂直居中。

2、設(shè)置偽元素::before

HTML:

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

CSS:

.parent{
    width:200px;
    height: 200px;
    border:1px solid red;
    
}
.parent::before{
    content:'';
    display: inline-block;
    height:100%;
    vertical-align: middle;  
  
}
.child{
    width:80px;
    height: 100px;
    background:red;
    vertical-align: middle;
    display: inline-block;
}
效果圖

重點(diǎn):給父元素添加一個(gè)偽元素::before,讓這個(gè)偽元素的div高度為100%,這樣其他div就可垂直居中了,但div 本身就是塊級(jí)元素,而vertical-align是行內(nèi)元素屬性,則需要修改為inline-block。

3、absolute + transform

HTML:

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

CSS:

.parent{
    width: 200px;
    height: 200px;
    background:#ddd;
    position: relative;
    
}
.child{
    width: 100px;
    height: 50px;
    background:green;
    position: absolute;
    top: 50%;
    left:50%;
    transform: translate(-50% , -50%);
}
效果圖

重點(diǎn):在父元素中設(shè)置相對定位position: relative,子元素設(shè)置絕對定位 position: absolute;top和left相對父元素的50%,與其搭配的 transform: translate(-50% , -50%)表示X軸和Y軸方向水平居中。

4. 設(shè)置絕對定位

HTML:

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

CSS:

.parent{
    width:200px;
    height: 200px;
    position: relative;
    border:1px solid red;   
}
.child{
  width:50px;
    height: 50px;
    position: absolute;
    top:0;
    left:0;
    right:0;
    bottom:0;
    margin:auto;
    background:red;
}

重點(diǎn):子元素絕對定位position:absolute,父元素相對定位position: relative,將上下左右的數(shù)值都設(shè)置為0,同時(shí)margin:auto。絕對定位是會(huì)脫離文檔流的,這點(diǎn)要注意一下。


效果圖
5、設(shè)置display:flex

HTML:

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

CSS:

 .parent{
    width: 200px;
    height: 200px;
    background: grey;
    display: flex;
    justify-content: center;
    align-items: center;
}
 .child{
    width: 100px;
    height: 50px;
    background:green;
}

重點(diǎn):給父元素設(shè)置display: flex布局,水平居中 justify-content: center,垂直居中align-items: center。

6、absolute + calc

HTML:

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

CSS:

.parent{
  width: 200px;
  height: 200px;
  border:1px solid black;
  position: relative;
}
.child{
  width: 100px;
  height: 50px;
  position: absolute;
  background:red;
  top: calc(50% - 25px); /*垂直居中 */
  left:calc(50% - 50px); /*水平居中 */
}
效果圖

重點(diǎn):父元素position定位為relative,子元素position定位為absolute。水平居中同理。calc居中要減多少要結(jié)合到自己的寬高設(shè)置多少再進(jìn)行計(jì)算。

7. display:table-cell實(shí)現(xiàn)CSS垂直居中。

HTML:

<div class="parent">
    <span class="child">你好</span>
</div>

CSS:

 .parent{
    width:200px;
    height:200px;
    border:1px solid red;
    display: table;
}
 .child{
    background:red;
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}
效果圖

重點(diǎn):將父元素設(shè)置display:table,子元素table-cell會(huì)自動(dòng)撐滿父元素。組合 display: table-cell、vertical-align: middle、text-align: center完成水平垂直居中。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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