很多時候,我們需要讓元素居中顯示:1. 一段文本的水平居中,2. 一張圖片的水平居中,3. 一個塊級元素的水平居中;4. 單行文本的豎直居中,5. 不確定高度的一段文本豎直居中,6. 確定高度的塊級元素豎直居中等等。現在分別對其進行總結下(這篇文章也在 imooc 里發表過手記,可是因為板式的原因不太容易讀懂。):
更新(20181115):
讓元素水平垂直居中,也可以使用讓元素顯示為單元格,讓單元格原本的垂直居中發揮作用:
<div class="parent"> <div class="child">我在父元素中水平垂直居中</div> </div>
.parent { width: 400px; height: 200px; border: 1px solid #f00; display: table-cell; text-align: center; vertical-align: middle; }
1. 讓元素水平居中,使用 text-align: center;
<div class="text-center">水平居中</div> .text-center { width: 200px; height: 100px; text-align: center; /* 讓文本水平居中 */ color: #fff; background-color: #f54; }
2. 讓圖片水平居中,父元素使用 text-align: center;
<div class="img-center"> <img src="fenjing.jpg" alt="藍天白雲青山綠水"> </div> .img-center { width: 200px; height: 120px; text-align: center; /* 讓圖片水平居中 */ background-color: #f54; }
說明:
圖片是行內元素,從一開始我視頻學習的時候,有一個老師好像說過圖片是行內塊級元素(inline-block),聽起來好像很有道理的,因為圖片可以使用 text-align: center; 將其水平居中顯示,並且還能設置寬和高,很長時間以來沒有懷疑過!后來喜歡上了“溯本求源”,才發現了原來不是那么回事:
在 ie, edge, chrome, firefox, opera 中對於 img 的默認顯示方式是: display: inline;
ie:
edge:
chrome:
firefox:
opera:
img 是 inline,還是比較容易想得通,像文本一樣可以通過 text-align: center; 設置為水平居中
3. 塊級元素水平居中,使用 margin-right: auto; margin-left: auto;
<div class="parent-box"> <div class="child-box">塊級元素水平居中</div> </div> .parent-box { width: 250px; height: 150px; background-color: #f98; } .child-box { width: 200px; height: 100px; background-color: #f00; margin-left: auto; margin-right: auto; }
4. 單行文本的垂直居中,讓 line-height 和 height 相等。
<div class="text-middle">單行文本豎直居中</div> .text-middle { width: 200px; height: 100px; line-height: 100px; background-color: #f00; color: #fff; }
注意:
這里說的 height 和 line-height 相等,有一個注意事項:
當 box-sizing: content-box; 時(這也是默認的值)。將 height 和 line-height 的值設置為一樣就行了;當 box-sizing: border-box; 時, line-height 的值要從 height 里減去 padding-top, padding-bottom, border-top, border-bottom 四個的值,也就是和分配給內容的有效高度相等。
5. 不確定高度的一段文本豎直居中,這里不適用高度,使用 padding-top: ...; padding-bottom: ...; padding-top 和 padding-bottom 值相同.
<div class="text-middle-padding">不確定高度的一段文本豎直居中</div> .text-middle-padding { width: 150px; padding-top: 30px; padding-bottom: 30px; color: #fff; background-color: #f00; }
說明:對於高度確定的元素,它的文本的行數不確定的情況下,怎么讓文本垂直居中呢?在后面會提到。
6. 確定高度的塊級元素豎直居中,使用 position: absolute; top: 50%; margin-top: ...;(margin-top的值為自身高度的值的一半的負值);
<div class="parent-box"> <div class="child-box">確定高度的塊級元素豎直居中</div> </div> .parent-box { position: relative; width: 250px; height: 150px; background-color: #f00; } .child-box { position: absolute; top: 50%; width: 200px; height: 100px; margin-top: -50px; background-color: #f54; }
7. 絕對定位實現水平垂直居中,使用 position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto;
<div class="parent-box"> <div class="child-box">絕對定位實現水平垂直居中居中</div> </div> .parent-box { position: relative; width: 250px; height: 150px; background-color: #f00; } .child-box { position: absolute; top: 0; right: 0; bottom: 0; left: 0; width: 200px; height: 100px; margin: auto; background-color: #f54; }
說明:對於塊兒級元素的垂直居中,推薦這么做,這也是我比較喜歡的方法。
需要注意的地方是,對父元素要使用 position: relative; 對子元素要使用 position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; 缺一不可。如果只需要垂直居中,right: 0; 和 left: 0; 可以省略不寫,margin: auto; 可以換成 margin-top: auto; margin-bottom: auto;;如果只需要水平居中,top: 0; bottom: 0; 可以省略不寫,margin: auto; 可以換成 margin-rihgt: auto; margin-left: auto; 。
8. 平移實現水平垂直居中法:通過使用 transform: translate(-50%,-50%); 添加廠商前綴 -webkit- 兼容 Safari 和 Chrome
<div class="parent-box"> <div class="child-box">平移實現水平垂直居中法</div> </div> .parent-box { width: 200px; height: 200px; background-color: #f00; } .child-box { position: relative; top: 50%; left: 50%; width: 150px; height: 150px; background-color: #f54; -webkit-transform: translate(-50%,-50%); transform: translate(-50%,-50%); }
9. 讓瀏覽器計算子元素的寬高並讓其水平垂直居中:通過使用定位position: absolute; top:...; right: ...; bottom: ...; left: ...; 四個方向上的值缺一不可。
<div class="parent-box"> <div class="child-box">讓瀏覽器計算子元素的寬高並讓其水平垂直居中</div> </div> .parent-box { position: relative; width: 200px; height: 200px; background-color: #f00; } .child-box { position: absolute; top: 20%; right: 20%; bottom: 20%; left: 20%; background-color: #f54; }
對於子元素,上下左右的定位值可以用 px 作為單位,也可以用 % 作為單位。
10. css3伸縮布局實現元素水平垂直居中,通過使用 display:flex; align-items: center; justify-content: center;
<div class="parent-box"> <div class="child-box">我是子元素,這里使用了 css3 的彈性伸縮布局</div> </div> .parent-box { width: 400px; height: 150px; display: flex; justify-content: center; /* 讓子元素水平居中 */ align-items: center; /* 讓子元素垂直居中 */ border: 1px solid #999; } .child-box { background-color: #fe5454; color: #fff; }
說明:
ie 10 及以上版本瀏覽器支持,chrome, firefox, opera, edge 均支持,不需要添加廠商前綴。
另外:這里也解釋了第5點中“對於高度確定的元素,它的文本的行數不確定的情況下,怎么讓文本垂直居中呢?”的問題,使用這里提到的 css3 彈性布局方式。對付元素使用 display: flex; justify-content: center; align-items: center; 來解決。
注意:
1. 如果不添加 justify-content: center; 子元素不會水平居中;
2. 如果不添加 align-items: center; 子元素會鋪滿父元素的高度,而不是我們希望的只有包含住文本的高度!
記憶方法:
我們知道:text-align: justify; 能將文本按照兩端對其的方式對文本進行布局,這個處理的是水平方向上的問題。聯想記憶,justify-content 也是處理水平方向上的事情,所以 justify-contnet: center; 就是讓元素水平居中了。
更新(20170220)
對於第五點中提到的不確定高度的多行文本豎直居中的問題,最近又在項目中遇到了。在新聞列表頁面,每一個項目列表包含圖片(居左) ,時間和標題(居右),然后不論標題是多少行,多要使文本和圖片看起來豎直居中。
這其實很簡單,就用到了兩個屬性:{ display: inline-block; vertical-align: middle; },本來 vertical-align: middle; 的意思就是豎直排列方式為居中,可只有在 display: inline-block; 的情況下才起作用。
順便貼上代碼:
<li class="news-item"> <a href="/case/74.html" title="成都市政中心"> <div class="news-item-text"> <h4>成都市政中心成都市政中心成</h4> <div class="item-info"> 發布時間:<time>2017-01-09 12:59</time> </div> </div> <img src="img/1-1612011611540-L.jpg" alt="成都市政中心" class="news-item-img"> </a> </li> <li class="news-item"> <a href="/case/74.html" title="成都市政中心"> <div class="news-item-text"> <h4>成都市政中心成都市政中心成都市政中心成都市政中心</h4> <div class="item-info"> 發布時間:<time>2017-01-09 12:59</time> </div> </div> <img src="img/1-1612011611540-L.jpg" alt="成都市政中心" class="news-item-img"> </a> </li>
/*===公司動態===*/.news-item { padding-top: 15px; padding-bottom: 15px; border-bottom: 1px solid #fee; } .news-item a { display: block; font-size: 0; } .news-item-text { width: 67%; padding-right: 14px; display: inline-block; vertical-align: middle; } .news-item-text h4 { font-weight: 500; font-size: 14px; line-height: 1.5; color: #666; } .news-item-text .item-info { font-size: 12px; margin-top: 10px; color: #999; } .news-item-img { display: inline-block; vertical-align: middle; width: 33%; height: auto; min-height: 60px; }
(更新20170928)
對於水平豎直居中,還可以參照表格單元格的方式:
<div class="parent-box"> <div class="child-box"></div> </div>
.parent-box { width: 300px; height: 300px; border: 1px solid #ccc; text-align: center; display: table-cell; vertical-align: middle; } .child-box { display: inline-block; width: 200px; height: 200px; background-color: #ccc; }
擴展:
需求:我們經常做分頁時,需要將分頁的列表項置於水平居中的位置,就像下面的 dom 一樣:
<ul class="pagination"> <li><a href="#">«</a></li> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">»</a></li> </ul>
解決方法:
可以為父元素 ul 添加 text-align: center; 同時給子元素 li 添加 display: inline-block;
完整的代碼:
<ul class="pagination"> <li><a href="#">«</a></li> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">»</a></li> </ul>
ul.pagination { margin-top: 20px; text-align: center; font-size: 0; /* 設置 font-size 的大小為 0,目的是讓顯示方式為 inline-block 的子元素去除外邊距(外邊距是由於 html 的空格所導致的) */ } ul.pagination li { display: inline-block; } ul.pagination li a { display: inline-block; padding: 7px 14px; border-width: 1px 0 1px 1px; border-style: solid; border-color: #f1f2f3; font-size: 15px; /* 這里一定要設置 font-size,別指望去繼承了,因為如果不設置,將會繼承 ul 的大小 0 */ transition: all .3s ease 0; } ul.pagination li:first-child a { border-top-left-radius: 5px; border-bottom-left-radius: 5px; } ul.pagination li:last-child a { border-right: 1px solid #f1f2f3; border-top-right-radius: 5px; border-bottom-right-radius: 5px; } ul.pagination li a:hover { background-color: #fe5454; color: #fff; border-color: #fe5454; }