亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

  發(fā)布時(shí)間:2016-03-09 14:48:32   作者:彼岸花在開(kāi)   我要評(píng)論
垂直居中是布局中十分常見(jiàn)的效果之一,這篇文章主要介紹了純CSS實(shí)現(xiàn)垂直居中的9種方法,感興趣的小伙伴們可以參考一下

為實(shí)現(xiàn)良好的兼容性,PC端實(shí)現(xiàn)垂直居中的方法一般是通過(guò)絕對(duì)定位,table-cell,負(fù)邊距等方法。有了css3,針對(duì)移動(dòng)端的垂直居中就更加多樣化。

方法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. }   


方法2:display:flex

CSS Code復(fù)制內(nèi)容到剪貼板
  1. .box2{   
  2.     display: flex;   
  3.     justify-content:center;   
  4.     align-items:Center;   
  5. }   

方法3:絕對(duì)定位和負(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:絕對(duì)定位和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. }   

這種方法跟上面的有些類(lèi)似,但是這里是通過(guò)margin:auto和top,left,right,bottom都設(shè)置為0實(shí)現(xiàn)居中,很神奇吧。不過(guò)這里得確定內(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的變形,移位是通過(guò)translate來(lái)實(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í)巧妙...通過(guò):after來(lái)占位。

方法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):
適用于所有瀏覽器
沒(méi)有足夠空間時(shí)(例如:窗口縮小) content 不會(huì)被截?cái)啵瑵L動(dòng)條出現(xiàn)

缺點(diǎn):
唯一我能想到的就是需要額外的空元素了(也沒(méi)那么糟,又是另外一個(gè)話(huà)題)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

原文:http://www.cnblogs.com/hutuzhu/p/4450850.html

相關(guān)文章

最新評(píng)論