用css讓一個容器水平垂直居中


閱讀目錄

這種css布局平時用的比較多,也是面試題常出的一個題,網上一搜一大丟,不過還是想自己總結一下。

這種方法比較多,本文只總結其中的幾種,以便加深印象。

 

效果圖都為這個:

 

方法一:position加margin

 

復制代碼
/**html**/
<div class="wrap"> <div class="center"></div> </div> /**css**/ .wrap { width: 200px; height: 200px; background: yellow; position: relative; } .wrap .center { width: 100px; height: 100px; background: green; margin: auto; position: absolute; left: 0; right: 0; top: 0; bottom: 0; }
復制代碼

兼容性:主流瀏覽器均支持,IE6不支持

 

方法二: diaplay:table-cell

 

復制代碼
<!-- html -->
<div class="wrap">
     <div class="center"></div>
</div>

/*css*/
.wrap{
    width: 200px;
    height: 200px;
    background: yellow;
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}
.center{
    display: inline-block;
    vertical-align: middle;
    width: 100px;
    height: 100px;
    background: green;
}
復制代碼

 兼容性:由於display:table-cell的原因,IE6\7不兼容

 

方法三:position加 transform

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- html -->
<div  class = "wrap" >
     <div  class = "center" ></div>
</div>
 
/* css */
.wrap {
     position: relative;
     background: yellow;
     width: 200px;
     height: 200px;}
 
.center {
     position: absolute;
     background: green;
     top:50%;
     left:50%;
     -webkit-transform:translate(-50%,-50%);
     transform:translate(-50%,-50%);
     width: 100px;
     height: 100px;
}

兼容性:ie9以下不支持 transform,手機端表現的比較好。

  

方法四:flex;align-items: center;justify-content: center

 

復制代碼
<!-- html -->
<div class="wrap">
    <div class="center"></div>
</div>

/* css */
.wrap {
    background: yellow;
    width: 200px;
    height: 200px;
    display: flex; 
    align-items: center; 
    justify-content: center;
}

.center {
    background: green;
    width: 100px;
    height: 100px;
}
復制代碼

 移動端首選

方法五:display:flex;margin:auto

復制代碼
<!-- html -->
<div class="wrap">
    <div class="center"></div>
</div>

/* css */
.wrap {
    background: yellow;
    width: 200px;
    height: 200px;
    display: flex; 
}

.center {
    background: green;
    width: 100px;
    height: 100px;
    margin: auto;
}
復制代碼

 移動端首選

 

方法六:純position

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!-- html -->
<div class= "wrap" >
     <div class= "center" ></div>
</div>
 
/* css */
.wrap {
     background : yellow;
     width 200px ;
     height 200px ;
     position relative ;
}
/**方法一**/
. center  {
     background green ;
     position absolute ;
     width 100px ;
     height 100px ;
     left 50px ;
     top 50px
  
}
/**方法二**/
. center  {
     background green ;
     position absolute ;
     width 100px ;
     height 100px ;
     left 50% ;
     top 50% ;
   margin-left : -50px ;
   margin-top : -50px ;
}

  兼容性:適用於所有瀏覽器

   方法六中的方法一計算公式如下:

  子元素(conter)的left值計算公式:left=(父元素的寬 - 子元素的寬 ) / 2=(200-100) / 2=50px;

  子元素(conter)的top值計算公式:top=(父元素的高 - 子元素的高 ) / 2=(200-100) / 2=50px;

 

  方法二計算公式:

  left值固定為50%;

  子元素的margin-left= -(子元素的寬/2)=-100/2= -50px;

  top值也一樣,固定為50%

     子元素的margin-top= -(子元素的高/2)=-100/2= -50px;

 

方法七:兼容低版本瀏覽器,不固定寬高

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!-- html -->
<div class= "table" >
     <div class= "tableCell" >
         <div class= "content" >不固定寬高,自適應</div>
     </div>
</div>
 
/*css*/
.table {
     height 200px ; /*高度值不能少*/
     width 200px ; /*寬度值不能少*/
     display : table;
     position relative ;
     float : left ;
     background : yellow;
}      
 
.tableCell {
     display table-cell ;
     vertical-align middle ;
     text-align center ;        
     * position absolute ;
     padding 10px ;
     * top 50% ;
     * left 50% ;
}
.content {
     * position : relative ;
     * top -50% ;
     * left -50% ;
      background green ;
}

  

   

暫時總結上面的七種,這種方法太多,其實只要習慣了其中的一兩種也就夠用了。

 

總結

 

如果是移動端,那么用方法四和方法五是比較方便的。而且支持不固定寬高的情況,快、准、狠

也就是用 flexalign-items: center; justify-content: center; 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!-- html -->
<div class= "wrap" >
     <div class= "center" ></div>
</div>
 
/* css */
.wrap {
     background : yellow;
     width 200px ;
     height 200px ;
     display : flex;
     align-items:  center ;
     justify- content center ;
}
 
. center  {
     background green ;
     width 100px ;
     height 100px ;
}

或者  display:flex;margin:auto;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- html -->
<div class= "wrap" >
     <div class= "center" ></div>
</div>
 
/* css */
.wrap {
     background : yellow;
     width 200px ;
     height 200px ;
     display : flex;
}
 
. center  {
     background green ;
     width 100px ;
     height 100px ;
     margin auto ;
}

  

如果是PC端,要考慮兼容性的話。方法六是不錯滴,也就是純position。

復制代碼
<!-- html -->
<div class="wrap">
    <div class="center"></div>
</div>

/* css */
.wrap {
    background: yellow;
    width: 200px;
    height: 200px;
    position: relative;
}
/**方法一**/
.center {
    background: green;
    position: absolute;
    width: 100px;
    height: 100px;
    left: 50px;
    top: 50px;  
  
}
/**方法二**/
.center {
    background: green;
    position: absolute;
    width: 100px;
    height: 100px;
    left: 50%;
    top: 50%; 
  margin-left:-50px; 
  margin-top:-50px;
} 
復制代碼

如果PC端的中間的元素高度不固定,那么就用方法七即可,代碼就不復制了

 

 這種css元素垂直的如果真的要總結起來,應該有十幾二十幾種。不過也沒必要全部掌握吧,只要大概了解一些,用起來沒有副作用就行。

有誤之處,歡迎指出

如果您覺得文章有用,也可以給咸魚老弟發個微信小額紅包鼓勵,
讓我可以有錢買書,吃頓飽飯,喝碗清湯

 

 

 

 

 

文字居中:

很多時候,我們需要讓元素居中顯示: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;
}
復制代碼
 
 
 
//或者DIV水平垂直居中

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style type="text/css">
.demo{
width: 200px;
height: 200px;
background: red;
position: relative;
}
.sb{
width: 100px;
height: 100px;
background: blue;
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
margin: auto;
}
</style>
</head>
<body>
<div class="demo">
<div class="sb"></div>
</div>
</body>
</html>

 
 
 

對於子元素,上下左右的定位值可以用 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="#">&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;
}
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM