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

淺析CSS實現(xiàn)水平垂直同時居中的5種思路

  發(fā)布時間:2016-04-28 14:58:45   作者:佚名   我要評論
下面小編就為大家?guī)硪黄獪\析CSS實現(xiàn)水平垂直同時居中的5種思路。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考,一起跟隨小編過來看看吧

水平居中和垂直居中已經(jīng)單獨介紹過,本文將介紹水平垂直同時居中的5種思路

思路一:  text-align + line-height實現(xiàn)單行文本水平垂直居中

CSS Code復(fù)制內(nèi)容到剪貼板
  1. <style>   
  2. .test{   
  3.     text-aligncenter;   
  4.     line-height100px;   
  5. }   
  6. </style>  
XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <div class="test" style="background-color: lightblue;width: 200px;">測試文字</div>   

思路二: text-align + vertical-align

【1】在父元素設(shè)置text-align和vertical-align,并將父元素設(shè)置為table-cell元素,子元素設(shè)置為inline-block元素

[注意]若兼容IE7-瀏覽器,將結(jié)構(gòu)改為<table>結(jié)構(gòu)來實現(xiàn)table-cell的效果;用display:inline;zoom:1;來實現(xiàn)inline-block的效果

CSS Code復(fù)制內(nèi)容到剪貼板
  1. <style>   
  2. .parent{   
  3.     displaytable-cell;   
  4.     text-aligncenter;   
  5.     vertical-alignmiddle;   
  6. }   
  7. .child{   
  8.     displayinline-block;   
  9. }   
  10. </style>  
XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <div class="parent" style="background-color: gray; width:200px; height:100px;">  
  2.   <div class="child" style="background-color: lightblue;">測試文字</div>  
  3. </div>   

【2】若子元素是圖像,可不使用table-cell,而是其父元素用行高替代高度,且字體大小設(shè)為0。子元素本身設(shè)置vertical-align:middle

CSS Code復(fù)制內(nèi)容到剪貼板
  1. <style>   
  2. .parent{   
  3.     text-aligncenter;   
  4.     line-height100px;   
  5.     font-size: 0;   
  6. }   
  7. .child{   
  8.     vertical-alignmiddle;   
  9. }   
  10. </style>  
XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <div class="parent" style="background-color: gray; width:200px; ">  
  2.   <img class="child" src="http://sandbox.runjs.cn/uploads/rs/26/ddzmgynp/img1.gif" width="50%" alt="test">  
  3. </div>    

思路三: margin + vertical-align  

要想在父元素中設(shè)置vertical-align,須設(shè)置為table-cell元素;要想讓margin:0 auto實現(xiàn)水平居中的塊元素內(nèi)容撐開寬度,須設(shè)置為table元素。而table元素是可以嵌套在tabel-cell元素里面的,就像一個單元格里可以嵌套一個表格

[注意]若兼容IE7-瀏覽器,需將結(jié)構(gòu)改為<table>結(jié)構(gòu)

CSS Code復(fù)制內(nèi)容到剪貼板
  1. <style>   
  2. .parent{   
  3.     display:table-cell;   
  4.     vertical-alignmiddle;   
  5. }   
  6. .child{   
  7.     display: table;   
  8.     margin: 0 auto;   
  9. }   
  10. </style>  

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <div class="parent" style="background-color: lightgray; width:200px; height:100px; ">  
  2.   <div class="child" style="background-color: lightblue;">測試文字</div>  
  3. </div>    

思路四: 使用absolute

【1】利用絕對定位元素的盒模型特性,在偏移屬性為確定值的基礎(chǔ)上,設(shè)置margin:auto

CSS Code復(fù)制內(nèi)容到剪貼板
  1. <style>   
  2. .parent{   
  3.     positionrelative;   
  4. }   
  5. .child{   
  6.     positionabsolute;   
  7.     top: 0;   
  8.     left: 0;   
  9.     rightright: 0;   
  10.     bottombottom: 0;   
  11.     height50px;   
  12.     width80px;   
  13.     marginauto;   
  14. }   
  15. </style>  

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <div class="parent" style="background-color: lightgray; width:200px; height:100px; ">  
  2.   <div class="child" style="background-color: lightblue;">測試文字</div>  
  3. </div>    

【2】利用絕對定位元素的偏移屬性和translate()函數(shù)的自身偏移達到水平垂直居中的效果

[注意]IE9-瀏覽器不支持

CSS Code復(fù)制內(nèi)容到剪貼板
  1. <style>   
  2. .parent{   
  3.     positionrelative;   
  4. }   
  5. .child{   
  6.     positionabsolute;   
  7.     top: 50%;   
  8.     left: 50%;   
  9.     transform: translate(-50%,-50%);   
  10. }   
  11. </style>  

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <div class="parent" style="background-color: lightgray; width:200px; height:100px; ">  
  2.   <div class="child" style="background-color: lightblue;">測試文字</div>  
  3. </div>   

【3】在子元素寬高已知的情況下,可以配合margin負值達到水平垂直居中效果

CSS Code復(fù)制內(nèi)容到剪貼板
  1. <style>   
  2. .parent{   
  3.     positionrelative;   
  4. }   
  5. .child{   
  6.     positionabsolute;   
  7.     top: 50%;   
  8.     left: 50%;   
  9.     width80px;   
  10.     height60px;   
  11.     margin-left: -40px;   
  12.     margin-top: -30px;   
  13. }   
  14. </style>  

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <div class="parent" style="background-color: lightgray; width:200px; height:100px; ">  
  2.   <div class="child" style="background-color: lightblue;">測試文字</div>  
  3. </div>    

思路五: 使用flex  

[注意]IE9-瀏覽器不支持

【1】在伸縮項目上使用margin:auto

CSS Code復(fù)制內(nèi)容到剪貼板
  1. <style>   
  2. .parent{   
  3.     display: flex;   
  4. }   
  5. .child{   
  6.     marginauto;   
  7. }   
  8. </style>  
XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <div class="parent" style="background-color: lightgray; width:200px; height:100px; ">  
  2.   <div class="child" style="background-color: lightblue;">測試文字</div>  
  3. </div>    

【2】在伸縮容器上使用主軸對齊justify-content和側(cè)軸對齊align-items

CSS Code復(fù)制內(nèi)容到剪貼板
  1. <style>   
  2. .parent{   
  3.     display: flex;   
  4.     justify-contentcenter;   
  5.     align-items: center;   
  6. }   
  7. </style>  

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  1. <div class="parent" style="background-color: lightgray; width:200px; height:100px; ">  
  2.   <div class="child" style="background-color: lightblue;">測試文字</div>  
  3. </div>    

以上這篇淺析CSS實現(xiàn)水平垂直同時居中的5種思路就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論