轉自https://blog.csdn.net/qq_26780317/article/details/80899402
一、水平居中:
(1)行內元素的水平居中
如果被設置的元素為文本、圖片等行內元素時,在父元素中設置text-align:center實現行內元素水平居中,將子元素的設置為display:inline-block,使子元素變成行內元素;
<div class="parent" style="background-color: gray;"> <div class="child" style="background-color: light-blue;">demo</div> </div>
<style> .parent { text-align: center; } .child { display: inline-block; } </style>
(2)塊狀元素的水平居中(定寬)
當被設置元素為定寬塊級元素時用text-align:center;就不起作用了。可以通過設置“margin: 0 auto;”來實現居中的。
<div class="parent" style="background-color: gray;"> <div class="child" style="background-color: lightblue;">demo</div> </div>
<style> .child { width: 200px; margin: 0 auto; } </style>
(3)塊狀元素的水平居中(不定定寬)
在實際工作中,我們會遇到需要為“不定寬度的塊級元素”設置居中,如網頁上的分頁導航,因為分頁的數量是不確定的,所以,不能通過設置寬度來限制它的彈性。
可以直接給補丁款的塊級元素設置text-align:center;來實現,也可以給父元素加text-align:center;來實現居中效果。
當不定寬塊級元素的寬度不要占一行時,可以設置display為inline類型或inline-block(設置為行內元素顯示或行內塊元素)。
<div class="container"> <ul> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> </ul> </div>
<style> .container { text-align: center; background: beige; } .container ul { list-style: none; margin: 0; padding: 0; display: inline-block; } .container li { margin-right: 8px; display: inline-block; } </style>
二、垂直居中
和水平居中一樣,垂直居中,首先需設定兩個條件即父元素是盒子容器且高度已經設定。
(1)子元素是行內元素,高度是由其內容撐開的。
這種情況下,需要通過設定父元素的line-height為其高度來使得子元素垂直居中。
<div class="wrap line-height"> <span class="span">111111</span> </div>
<style> .wrap { width: 200px; height: 300px; line-height: 300px; border: 2px solid #ccc; } .span { background: red; } </style>
(2)子元素是塊級元素但子元素高度沒有設定,在這種情況下,實際上是不知道子元素的高度的,無法通過計算得到padding或margin來調整。
可以通過給父元素設定display:table-cell;vertical-align:middle;來解決。
<div class="wrap"> <div class="non-height">111111</div> </div>
(3)子元素是塊級元素且高度已經設定
計算子元素的margin-top或margin-bottom,計算方法為父(元素高度-子元素高度)/ 2;
<div class="wrap"> <div class="div1">111111</div> </div>
<style> .wrap { width: 200px; height: 300px; border: 2px solid #ccc; } .div1 { width: 100px; height: 100px; margin-top: 100px; background: darkblue; } </style>
三、水平垂直居中
(1)水平對齊+行高
text-align + line-height實現單行文本水平垂直居中
<style> .test { text-align: center; line-height: 100px; } </style>
<div class="test" style="background-color: lightblue;width: 200px;"></div>
(2)水平+垂直對齊
①text-align + vertical-align 在父元素設置text-align和vertical-align,並將父元素設置為table-cell元素,子元素設置為inline-block元素。
<style> .parent { display: table-cell; text-align: center; vertical-align: middle; } .child { display: inline-block; } </style>
<div class="parent" style="background-color: gray; width: 200px; height:100px;> <div class="child" style="background-color: lightblue;">測試文字</div> </div>
②若子元素是圖像,可不使用table-cell,而是其父元素用行高代替高度,且字體大小設為0。子元素本身設置vertical-align:middle;
<div class="parent" style="background-color: gray; width: 200px;"> <img class="child" src="images/1.png" width="50%" alt="test"> </div>
<style> .parent { text-align: center; line-height: 100px; font-size: 0; } .child { vertical-align: middle; } </style>
(3)相對+絕對定位
使用absolute,利用絕對定位元素的盒模型特性,在偏移屬性為確定值的基礎上,設置margin: auto;
<div class="parent" style="background-color: lightgray;width: 200px; height: 100px;> <div class="child" style="background-color: lightblue;">測試文字</div> </div>
<style> .parent { position: relative; } .child { position: absolute; top: 0; left: 0; right: 0; bottom: 0; width: 80px; margin: auto; } </style>
PS:01、補充《三》中的水平垂直居中的方式(2018/08/29)
translate()函數可以在不知道自身寬高的情況下,利用它進行水平垂直居中。但是,有兼容性問題,支持IE9+的游覽器。
使用translate函數,配合left,top屬性,可以讓目標元素始終保持水平垂直居中,當游覽器窗口發生變化時,也能一直保持水平垂直居中。
<div class="wrap"> 不知道寬高,可以水平垂直居中 </div>
<style> .wrap { padding: 10px; background: green; color: #fff; border-radius: 5px; position: absolute; top: 50%; left: 50%; /*定位margin-left:50%的位置*/ -webkit-transform: translate(-50%,-50%); /*Safari,Chrome*/ -moz-transform: translate(-50%,-50%); /*IE9+*/ transform: translate(-50%,-50%); /*使元素本身向左移動寬度的一半*/ /*transform使元素在當前位置分別往x軸,y軸正向平移自身寬度的一半距離。*/ } </style>
02、補充《三》中的水平垂直居中的方式(2018/11/29)
場景:使用背景圖片屬性,使圖片在父元素的盒子中始終居中,但是,背景圖片的尺寸大小不固定。
關鍵:background-position:center center;(background-position:50% 50%;)屬性。
<div class="index-imgBlock"> <div class="index-image"></div> </div>
<style> .index-image { height: 268px;/*選取三張圖片中最大高度的尺寸設定*/ width: 100%; max-width: 484px; /*選取三張圖片中的最大寬度的尺寸設定*/ background: url(images/403.png); /*403圖片尺寸為484*206*/ /*background: url(images/404.png);*/ /*403圖片尺寸為428*268*/ /*background: url(images/500.png);*/ /*500圖片尺寸為475*236*/ background-position: center center; border: 1px solid #333333; }
</style>