前面的話
水平居中是經常遇到的問題。看似方法較多,條條大路通羅馬。但系統梳理下,其實都圍繞着幾個思路展開。本文將介紹關於水平居中的5種思路
text-align
【思路一】:在父元素中設置text-align:center實現行內元素水平居中
將子元素的display設置為inline-block,使子元素變成行內元素
[注意]若要兼容IE7-瀏覽器,可使用display:inline;zoom:1;來達到inline-block的效果
<style> .parent{text-align: center;} .child{display: inline-block;} </style>
<div class="parent" style="background-color: gray;"> <div class="child" style="background-color: lightblue;">DEMO</div> </div>
這種方法的不足之處在於,子元素的text-align繼承了父元素的center,文字也居中顯示,所以需要在子元素中設置text-align:left
margin
【思路二】:在本身元素設置margin: 0 auto實現塊級元素水平居中
【1】將子元素的display為table,使子元素成為塊級元素,同時table還具有包裹性,寬度由內容撐開
[注意]若要兼容IE7-瀏覽器,可把child的結構換成<table class="child">DEMO</table>
<style> .child{ display: table; margin: 0 auto; } </style>
<div class="parent" style="background-color: gray;"> <div class="child" style="background-color: lightblue;">DEMO</div> </div>
該方案的優點在於,只設置父級元素即可實現居中效果
【2】若子元素定寬,則可以使用絕對定位的盒模型屬性,實現居中效果;若不設置寬度時,子元素被拉伸
<style> .parent{ position: relative; } .child{ position: absolute; left: 0; right: 0; margin: 0 auto; width: 50px; } </style>
<div class="parent" style="background-color: gray;height: 20px;"> <div class="child" style="background-color: lightblue;">DEMO</div> </div>
absolute
【思路三】: 通過絕對定位的偏移屬性實現水平居中
【1】配合translate()位移函數
translate函數的百分比是相對於自身寬度的,所以left:50%配合translateX(-50%)可實現居中效果
[注意]IE9-瀏覽器不支持
<style> .parent{ position: relative; } .child{ position: absolute; left: 50%; transform:translateX(-50%); } </style>
<div class="parent" style="background-color: gray;height: 20px;"> <div class="child" style="background-color: lightblue;">DEMO</div> </div>
【2】配合relative
relative數值型的偏移屬性是相對於自身的,但百分比卻是相對於包含塊的。因為子元素已經被設置為absolute,所以若使用relative,則需要增加一層<div>結構,使其寬度與子元素寬度相同
[注意]該方法全兼容,但是增加了html結構
<style> .parent{ position: relative; } .childWrap{ position: absolute; left: 50%; } .child{ position: relative; left: -50%; } </style>
<div class="parent" style="background-color: gray;height: 20px;"> <div class="childWrap"> <div class="child" style="background-color: lightblue;">DEMO</div> </div> </div>
【3】配合負margin
margin的百分比是相對於包含塊的,所以需要增加一層<div>結構。由於寬度width的默認值是auto,當設置負margin時,width也會隨着變大。所以此時需要定寬處理
[注意]雖然全兼容,但需要增加頁面結構及定寬處理,所以限制了應用場景
<style> .parent{ position: relative; } .childWrap{ position: absolute; left: 50%; } .child{ width:50px; margin-left:-50%; } </style>
<div class="parent" style="background-color: gray;height: 20px;"> <div class="childWrap"> <div class="child" style="background-color: lightblue;">DEMO</div> </div> </div>
flex
【思路四】: 使用彈性盒模型flex實現水平居中
[注意]IE9-瀏覽器不支持
【1】在伸縮容器上設置主軸對齊方式justify-content:center
<style> .parent{ display: flex; justify-content: center; } </style>
<div class="parent" style="background-color: gray;"> <div class="child" style="background-color: lightblue;">DEMO</div> </div>
【2】在伸縮項目上設置margin: 0 auto
<style> .parent{display: flex;} .child{margin: 0 auto;} </style>
<div class="parent" style="background-color: gray;"> <div class="child" style="background-color: lightblue;">DEMO</div> </div>
grid
【思路五】: 使用柵格布局grid實現水平居中
[注意]IE10-瀏覽器不支持
【1】在網格容器上設置justify-items或justify-content
<style> .parent{ display:grid; justify-items:center;
/*justify-content:center;*/
} </style>
<div class="parent" style="background-color: gray;"> <div class="child" style="background-color: lightblue;">DEMO</div> </div>
【2】在網格項目中設置justify-self或者margin: 0 auto
<style> .parent{ display:grid; } .child{ justify-self:center;
/*margin: 0 auto;*/
} </style>
<div class="parent" style="background-color: gray;"> <div class="child" style="background-color: lightblue;">DEMO</div> </div>
