一、水平居中:
(1). 行內元素的水平居中?
如果被設置元素為文本、圖片等行內元素時,在父元素中設置text-align:center實現行內元素水平居中,將子元素的display設置為inline-block,使子元素變成行內元素
![]()
<div class="parent" style=""> <div class="child" style="">DEMO</div> </div>
<style>
.parent{text-align: center;}
.child{display: inline-block;}
</style>
(2)塊狀元素的水平居中(定寬)
當被設置元素為定寬塊級元素時用 text-align:center 就不起作用了。可以通過設置“左右margin”值為“auto”來實現居中的。
![]()
<div class="parent" style=""> <div class="child" style="">DEMO</div> </div>
.child{
width: 200px;
margin: 0 auto;
}
(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>
.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;}
二、垂直居中:
和水平居中一樣,這里要講垂直居中,首先設定兩個條件即父元素是盒子容器且高度已經設定
場景1:子元素是行內元素,高度是由其內容撐開的
這種情況下,需要通過設定父元素的line-height為其高度來使得子元素垂直居中

<div class="wrap line-height">
<span class="span">111111</span>
</div>
.wrap{
width:200px ;
height: 300px;
line-height: 300px;
border: 2px solid #ccc;
}
.span{
background: red;
}
場景2:子元素是塊級元素但是子元素高度沒有設定,在這種情況下實際上是不知道子元素的高度的,無法通過計算得到padding或margin來調整,但是還是存在一些解法。
通過給父元素設定display:table-cell;vertical-align:middle來解決
<div class="wrap">
<div class="non-height ">11111</div>
</div>
.wrap{
width:200px ;
height: 300px;
border: 2px solid #ccc;
display: table-cell;
vertical-align: middle;
}
.non-height{
background: green;
}
結果

場景3:子元素是塊級元素且高度已經設定
計算子元素的margin-top或margin-bottom,計算方法為父(元素高度-子元素高度)/2
<div class="wrap ">
<div class="div1">111111</div>
</div>
.wrap{
width:200px ;
height: 300px;
border: 2px solid #ccc;
}
.div1{
width:100px ;
height: 100px;
margin-top: 100px;
background: darkblue;
}
結果

三、水平垂直居中:
3.1水平對齊+行高
text-align + line-height實現單行文本水平垂直居中
<style>
.test{
text-align: center;
line-height: 100px;
}
</style>
<div class="test" style="width: 200px;">測試文字</div>

3.2水平+垂直對齊
1. 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=" width:200px; height:100px;"> <div class="child" style="">測試文字</div> </div>

2.若子元素是圖像,可不使用table-cell,而是其父元素用行高替代高度,且字體大小設為0。子元素本身設置vertical-align:middle
<div class="parent" style=" width:200px; "> <img class="child" src="http://sandbox.runjs.cn/uploads/rs/26/ddzmgynp/img1.gif" width="50%" alt="test"> </div>
<style>
.parent{
text-align: center;
line-height: 100px;
font-size: 0;
}
.child{
vertical-align: middle;
}
</style>

3.3相對+絕對定位
使用absolute,利用絕對定位元素的盒模型特性,在偏移屬性為確定值的基礎上,設置margin:auto
<style>
.parent{
position: relative;
}
.child{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
height: 50px;
width: 80px;
margin: auto;
}
</style>
<div class="parent" style=" width:200px; height:100px; "> <div class="child" style="">測試文字</div> </div>

來自http://www.cnblogs.com/chaixiaozhi/p/8490725.html

