css中元素居中总结
很多時候,我們需要讓元素居中顯示:1. 一段文本的水平居中,2. 一張圖片的水平居中,3. 一個塊級元素的水平居中;4. 單行文本的豎直居中,5. 不確定高度的一段文本豎直居中,6. 確定高度的塊級元素豎直居中等等?,F在分別對其進行總結下(這篇文章也在 imooc 里發表過手記,可是因為板式的原因不太容易讀懂。):
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; 就是讓元素水平居中了。
擴展:
需求:我們經常做分頁時,需要將分頁的列表項置于水平居中的位置,就像下面的 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; } 標簽:?css,?display,?居中轉載于:https://www.cnblogs.com/libin-1/p/5918477.html
總結
以上是生活随笔為你收集整理的css中元素居中总结的全部內容,希望文章能夠幫你解決所遇到的問題。