水平居中和垂直居中已經單獨介紹過,本文將介紹水平垂直同時居中的6種思路css
【思路一】text-align + line-height實現單行文本水平垂直居中html
<style> .test{ text-align: center; line-height: 100px; } </style>
<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>結構來實現table-cell的效果;用display:inline;zoom:1;來實現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>
【2】若子元素是圖像,可不使用table-cell,而是其父元素用行高替代高度,且字體大小設爲0。子元素自己設置vertical-align:middle字體
<style> .parent{ text-align: center; line-height: 100px; font-size: 0; } .child{ vertical-align: middle; } </style>
<div class="parent" style="background-color: gray; width:200px; "> <img class="child" src="http://sandbox.runjs.cn/uploads/rs/26/ddzmgynp/img1.gif" width="50%" alt="test"> </div>
【思路三】margin + vertical-alignflex
要想在父元素中設置vertical-align,須設置爲table-cell元素;要想讓margin:0 auto實現水平居中的塊元素內容撐開寬度,須設置爲table元素。而table元素是能夠嵌套在tabel-cell元素裏面的,就像一個單元格里能夠嵌套一個表格spa
[注意]若兼容IE7-瀏覽器,需將結構改成<table>結構code
<style> .parent{ display:table-cell; vertical-align: middle; } .child{ display: table; margin: 0 auto; } </style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; "> <div class="child" style="background-color: lightblue;">測試文字</div> </div>
【思路四】使用absoluteorm
【1】利用絕對定位元素的盒模型特性,在偏移屬性爲肯定值的基礎上,設置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="background-color: lightgray; width:200px; height:100px; "> <div class="child" style="background-color: lightblue;">測試文字</div> </div>
【2】利用絕對定位元素的偏移屬性和translate()函數的自身偏移達到水平垂直居中的效果
[注意]IE9-瀏覽器不支持
<style> .parent{ position: relative; } .child{ position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); } </style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; "> <div class="child" style="background-color: lightblue;">測試文字</div> </div>
【3】在子元素寬高已知的狀況下,能夠配合margin負值達到水平垂直居中效果
<style> .parent{ position: relative; } .child{ position: absolute; top: 50%; left: 50%; width: 80px; height: 60px; margin-left: -40px; margin-top: -30px; } </style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; "> <div class="child" style="background-color: lightblue;">測試文字</div> </div>
【思路五】使用flex
[注意]IE9-瀏覽器不支持
【1】在伸縮項目上使用margin:auto
<style> .parent{ display: flex; } .child{ margin: auto; } </style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; "> <div class="child" style="background-color: lightblue;">測試文字</div> </div>
【2】在伸縮容器上使用主軸對齊justify-content和側軸對齊align-items
<style> .parent{ display: flex; justify-content: center; align-items: center; } </style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; "> <div class="child" style="background-color: lightblue;">測試文字</div> </div>
【思路六】使用grid
[注意]IE10-瀏覽器不支持
【1】在網格項目中設置justify-self、align-self或者margin: auto
<style> .parent{ display: grid; } .child{ align-self: center; justify-self: center; /* margin: auto; */ } </style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; "> <div class="child" style="background-color: lightblue;">測試文字</div> </div>
【2】在網格容器上設置justify-items、align-items或justify-content、align-content
<style> .parent{ display: grid; align-items: center; justify-items: center; /* align-content: center; */ /* justify-content: center; */ } </style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; "> <div class="child" style="background-color: lightblue;">測試文字</div> </div>