淺析CSS實現(xiàn)水平垂直同時居中的5種思路

水平居中和垂直居中已經(jīng)單獨介紹過,本文將介紹水平垂直同時居中的5種思路
思路一: text-align + line-height實現(xiàn)單行文本水平垂直居中
- <style>
- .test{
- text-align: center;
- line-height: 100px;
- }
- </style>
- <div class="test" style="background-color: lightblue;width: 200px;">測試文字</div>
思路二: text-align + vertical-align
【1】在父元素設(shè)置text-align和vertical-align,并將父元素設(shè)置為table-cell元素,子元素設(shè)置為inline-block元素
[注意]若兼容IE7-瀏覽器,將結(jié)構(gòu)改為<table>結(jié)構(gòu)來實現(xiàn)table-cell的效果;用display:inline;zoom:1;來實現(xiàn)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,而是其父元素用行高替代高度,且字體大小設(shè)為0。子元素本身設(shè)置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-align
要想在父元素中設(shè)置vertical-align,須設(shè)置為table-cell元素;要想讓margin:0 auto實現(xiàn)水平居中的塊元素內(nèi)容撐開寬度,須設(shè)置為table元素。而table元素是可以嵌套在tabel-cell元素里面的,就像一個單元格里可以嵌套一個表格
[注意]若兼容IE7-瀏覽器,需將結(jié)構(gòu)改為<table>結(jié)構(gòu)
- <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>
思路四: 使用absolute
【1】利用絕對定位元素的盒模型特性,在偏移屬性為確定值的基礎(chǔ)上,設(shè)置margin:auto
- <style>
- .parent{
- position: relative;
- }
- .child{
- position: absolute;
- top: 0;
- left: 0;
- rightright: 0;
- bottombottom: 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()函數(shù)的自身偏移達到水平垂直居中的效果
[注意]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和側(cè)軸對齊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>
以上這篇淺析CSS實現(xiàn)水平垂直同時居中的5種思路就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用CSS3的flexbox實現(xiàn)水平垂直居中與三列等高布局
這篇文章給大家介紹了三個小節(jié)的內(nèi)容,其中包括關(guān)于css3中flexbox需要掌握的概念、flexbox實現(xiàn)水平垂直居中對齊和三列等高自適應(yīng)頁腳區(qū)域黏附底部的布局,有需要的可以參考2016-09-12Flexbox制作CSS布局實現(xiàn)水平垂直居中的簡單實例
下面小編就為大家?guī)硪黄狥lexbox制作CSS布局實現(xiàn)水平垂直居中的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-27- 下面小編就為大家?guī)硪黄孌IV水平垂直居中的兩種完美方法推薦。一起跟隨小編過來看看吧。希望給大家一個參考2016-03-15
- 這篇文章主要介紹了CSS解決頁面圖片水平垂直居中問題的方法,文中給出了三種方案而無需依賴JavaScript,需要的朋友可以參考下2016-03-10
全面總結(jié)使用CSS實現(xiàn)水平垂直居中效果的方法
這篇文章主要介紹了使用CSS實現(xiàn)水平垂直居中效果的方法的總結(jié),涵蓋了從最原始的高度設(shè)置到令人興奮的CSS3的Flexbox方式,非常全面,十分推薦!需要的朋友可以參考下2016-03-10- 這篇文章主要介紹了CSS定位“十字架”之水平垂直居中的相關(guān)資料,CSS如何定位“十字架”實現(xiàn)水平垂直居中效果,小編為大家解答,感興趣的小伙伴們可以參考一下2016-03-02
- 這篇文章主要針對HTML對于元素水平垂直居中進行的探討,對元素水平垂直居中操作進行講解,感興趣的小伙伴們可以參考一下2016-02-24
- 本文是小編日常遇到的關(guān)于html水平垂直居中的一些問題小結(jié),特此分享在腳本之家網(wǎng)站供大家參考2015-11-18
- 這篇文章主要介紹了DIV或者DIV里面的圖片水平與垂直居中的方法,需要的朋友可以參考下2018-11-15