css中元素置中總結

來源:互聯網
上載者:User

標籤:

很多時候,我們需要讓元素置中顯示:1. 一段文本的水平置中,2. 一張圖片的水平置中,3. 一個區塊層級元素的水平置中;4. 單行文本的豎直置中,5. 不確定高度的一段文本豎直置中,6. 確定高度的區塊層級元素豎直置中等等。現在分別對其進行總結下(這篇文章也在 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 的Auto Scaling布局</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="#">&laquo;</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="#">&raquo;</a></li></ul>      

解決方案:

可以為父元素 ul 添加 text-align: center; 同時給子項目 li 添加 display: inline-block;

完整的代碼:

<ul class="pagination">    <li><a href="#">&laquo;</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="#">&raquo;</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, 置中

css中元素置中總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.