溫馨提示×

溫馨提示×

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

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

CSS中實(shí)現(xiàn)垂直居中的方法有哪些

發(fā)布時(shí)間:2021-08-07 17:03:39 來源:億速云 閱讀:109 作者:Leah 欄目:web開發(fā)

本篇文章給大家分享的是有關(guān)CSS中實(shí)現(xiàn)垂直居中的方法有哪些,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

方法1:table-cell

html結(jié)構(gòu):

XML/HTML Code復(fù)制內(nèi)容到剪貼板

  1. <div class="box box1">  

  2.         <span>垂直居中</span>  

  3. </div>  

css:

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .box1{   

  2.     displaytable-cell;   

  3.     vertical-alignmiddle;   

  4.     text-aligncenter;           

  5. }   

CSS中實(shí)現(xiàn)垂直居中的方法有哪些


方法2:display:flex

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .box2{   

  2.     display: flex;   

  3.     justify-content:center;   

  4.     align-items:Center;   

  5. }   

方法3:絕對定位和負(fù)邊距

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .box3{position:relative;}   

  2. .box3 span{   

  3.             positionabsolute;   

  4.             width:100px;   

  5.             height50px;   

  6.             top:50%;   

  7.             left:50%;   

  8.             margin-left:-50px;   

  9.             margin-top:-25px;   

  10.             text-aligncenter;   

  11.         }   

方法4:絕對定位和0

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .box4 span{   

  2.   width: 50%;    

  3.   height: 50%;    

  4.   background#000;   

  5.   overflowauto;    

  6.   marginauto;    

  7.   positionabsolute;    

  8.   top: 0; left: 0; bottombottom: 0; rightright: 0;    

  9. }   

這種方法跟上面的有些類似,但是這里是通過margin:auto和top,left,right,bottom都設(shè)置為0實(shí)現(xiàn)居中,很神奇吧。不過這里得確定內(nèi)部元素的高度,可以用百分比,比較適合移動(dòng)端。

方法5:translate

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .box6 span{   

  2.             positionabsolute;   

  3.             top:50%;   

  4.             left:50%;   

  5.             width:100%;   

  6.             transform:translate(-50%,-50%);   

  7.             text-aligncenter;   

  8.         }   

這實(shí)際上是方法3的變形,移位是通過translate來實(shí)現(xiàn)的。

方法6:display:inline-block

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .box7{   

  2.   text-align:center;   

  3.   font-size:0;   

  4. }   

  5. .box7 span{   

  6.   vertical-align:middle;   

  7.   display:inline-block;   

  8.   font-size:16px;   

  9. }   

  10. .box7:after{   

  11.   content:'';   

  12.   width:0;   

  13.   height:100%;   

  14.   display:inline-block;   

  15.   vertical-align:middle;   

  16. }   

這種方法確實(shí)巧妙...通過:after來占位。

方法7:display:flex和margin:auto

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .box8{   

  2.     display: flex;   

  3.     text-aligncenter;   

  4. }   

  5. .box8 span{marginauto;}   

方法8:display:-webkit-box

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .box9{   

  2.     display: -webkit-box;   

  3.     -webkit-box-pack:center;   

  4.     -webkit-box-align:center;   

  5.     -webkit-box-orient: vertical;   

  6.     text-aligncenter  

  7. }   

css3博大精深,可以實(shí)現(xiàn)很多創(chuàng)造性的效果,需要好好研究下。

今天又發(fā)現(xiàn)一種方法,現(xiàn)在補(bǔ)上:

方法9:display:-webkit-box

這種方法,在 content 元素外插入一個(gè) div。設(shè)置此 div height:50%; margin-bottom:-contentheight;。

content 清除浮動(dòng),并顯示在中間。

XML/HTML Code復(fù)制內(nèi)容到剪貼板

  1. <div class="floater"></div>     

  2. <div class="content"> Content here </div>       

CSS Code復(fù)制內(nèi)容到剪貼板

  1. .floater {   

  2.     float:left;    

  3.     height:50%;    

  4.     margin-bottom:-120px;   

  5. }   

  6. .content {   

  7.     clear:both;    

  8.     height:240px;    

  9.     position:relative;   

  10. }   

優(yōu)點(diǎn):
適用于所有瀏覽器
沒有足夠空間時(shí)(例如:窗口縮小) content 不會(huì)被截?cái)?,滾動(dòng)條出現(xiàn)

缺點(diǎn):
唯一我能想到的就是需要額外的空元素了

以上就是CSS中實(shí)現(xiàn)垂直居中的方法有哪些,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

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

css
AI