CSS怎么實現(xiàn)水平垂直同時居中

這篇文章主要講解了“CSS怎么實現(xiàn)水平垂直同時居中”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“CSS怎么實現(xiàn)水平垂直同時居中”吧!

10年積累的成都網(wǎng)站制作、網(wǎng)站設計經驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡服務。我雖然不認識你,你也不認識我。但先做網(wǎng)站后付款的網(wǎng)站建設流程,更有云陽免費網(wǎng)站建設讓你可以放心的選擇與我們合作。

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

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

CSS Code復制內容到剪貼板

  1. <style>   

  2. .test{   

  3.     text-align: center;   

  4.     line-height: 100px;   

  5. }   

  6. </style>  

XML/HTML Code復制內容到剪貼板

  1. <div class="test" style="background-color: lightblue;width: 200px;">測試文字</div>   

思路二:text-align + vertical-align

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

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

CSS Code復制內容到剪貼板

  1. <style>   

  2. .parent{   

  3.     display: table-cell;   

  4.     text-align: center;   

  5.     vertical-align: middle;   

  6. }   

  7. .child{   

  8.     display: inline-block;   

  9. }   

  10. </style>  

XML/HTML Code復制內容到剪貼板

  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,而是其父元素用行高替代高度,且字體大小設為0。子元素本身設置vertical-align:middle

CSS Code復制內容到剪貼板

  1. <style>   

  2. .parent{   

  3.     text-align: center;   

  4.     line-height: 100px;   

  5.     font-size: 0;   

  6. }   

  7. .child{   

  8.     vertical-align: middle;   

  9. }   

  10. </style>  

XML/HTML Code復制內容到剪貼板

  1. <div class="parent" style="background-color: gray; width:200px; ">  

  2.   <img class="child" src="/upload/otherpica42/10685.gif" width="50%" alt="test">  

  3. </div>    

思路三:margin + vertical-align

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

[注意]若兼容IE7-瀏覽器,需將結構改為<table>結構

CSS Code復制內容到剪貼板

  1. <style>   

  2. .parent{   

  3.     display:table-cell;   

  4.     vertical-align: middle;   

  5. }   

  6. .child{   

  7.     display: table;   

  8.     margin: 0 auto;   

  9. }   

  10. </style>  

XML/HTML Code復制內容到剪貼板

  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】利用絕對定位元素的盒模型特性,在偏移屬性為確定值的基礎上,設置margin:auto

CSS Code復制內容到剪貼板

  1. <style>   

  2. .parent{   

  3.     position: relative;   

  4. }   

  5. .child{   

  6.     position: absolute;   

  7.     top: 0;   

  8.     left: 0;   

  9.     rightright: 0;   

  10.     bottombottom: 0;   

  11.     height: 50px;   

  12.     width: 80px;   

  13.     margin: auto;   

  14. }   

  15. </style>  

XML/HTML Code復制內容到剪貼板

  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復制內容到剪貼板

  1. <style>   

  2. .parent{   

  3.     position: relative;   

  4. }   

  5. .child{   

  6.     position: absolute;   

  7.     top: 50%;   

  8.     left: 50%;   

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

  10. }   

  11. </style>  

XML/HTML Code復制內容到剪貼板

  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復制內容到剪貼板

  1. <style>   

  2. .parent{   

  3.     position: relative;   

  4. }   

  5. .child{   

  6.     position: absolute;   

  7.     top: 50%;   

  8.     left: 50%;   

  9.     width: 80px;   

  10.     height: 60px;   

  11.     margin-left: -40px;   

  12.     margin-top: -30px;   

  13. }   

  14. </style>  

XML/HTML Code復制內容到剪貼板

  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復制內容到剪貼板

  1. <style>   

  2. .parent{   

  3.     display: flex;   

  4. }   

  5. .child{   

  6.     margin: auto;   

  7. }   

  8. </style>  

XML/HTML Code復制內容到剪貼板

  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和側軸對齊align-items

CSS Code復制內容到剪貼板

  1. <style>   

  2. .parent{   

  3.     display: flex;   

  4.     justify-content: center;   

  5.     align-items: center;   

  6. }   

  7. </style>  

XML/HTML Code復制內容到剪貼板

  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)水平垂直同時居中”的內容了,經過本文的學習后,相信大家對CSS怎么實現(xiàn)水平垂直同時居中這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關知識點的文章,歡迎關注!

網(wǎng)頁題目:CSS怎么實現(xiàn)水平垂直同時居中
鏈接URL:http://muchs.cn/article28/iehdjp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設、響應式網(wǎng)站網(wǎng)站導航、自適應網(wǎng)站、定制網(wǎng)站、Google

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設網(wǎng)站維護公司