SS 是前端里面的基礎之一,也是非常重要的一部分,它往往決定了你所做出來的網(wǎng)頁頁面是否美觀。在設計網(wǎng)頁頁面的過程中,總會有將元素或者文字進行水平垂直居中的要求。下面w3cschool編程獅就為大家介紹 CSS 中幾種常用到的水平垂直居中的方法。
當元素有給定的高度以及寬度的時候,使用 margin: auto; 元素僅會水平居中,并不會進行垂直居中。此時就需要設置元素的 position 為 absolute,父級元素的 position 為 relative,同時元素的上下左右都需要設置為 0。
HTML 代碼
<div class="box">
<div class="center1"></div>
</div>
CSS 代碼
.box{
width: 200px;
height: 200px;
background-color: #eee;
position: relative;
margin-top: 20px;
}
.center1{
width: 50px;
height: 50px;
background-color: #00ACED;
margin: auto;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
效果展示:
當已經(jīng)知道了要進行水平垂直居中的元素的寬高時,就可以通過設置 position: absolute 來實現(xiàn)。但是,使用的同時還需要結合其他屬性才完整實現(xiàn)。因為,單是設置 absolute,上左距離均為一半,就會出現(xiàn)下面這種情況。很顯然可以看到,元素并不是完全居中,僅只有左上角的位置在中心點
概念圖:
因此想要實現(xiàn)元素完全水平垂直居中,在設置了 absolute 定位后,可以設置 margin 值為負,或者使用 calc 來計算,上左距離在 50% 的基礎上還要減去元素本身一半的寬高。
margin 值為負或者 calc 計算均是在已知元素寬高的情況下,假設不知道元素的寬高,那么怎么實現(xiàn)水平垂直居中呢?這里就可以使用 transform 屬性,通過坐標位移來實現(xiàn)居中。
CSS 代碼
/* 結合 margin */
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: 50%;
top: 50%;
margin-left: -25px;
margin-top: -25px;
}
/* 結合 calc 計算*/
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: calc(50% - 25px)
top: calc(50% - 25px);
}
/* 結合 transform */
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
效果展示
03
PART
可以通過彈性布局來設置水平垂直居中,這里需要設置父級元素 display:flex; 還需要設置兩個屬性,水平布局 justify-content 以及垂直布局 align-items。
HTML代碼
<div class="box2">
<div class="center4"></div>
</div>
CSS代碼:
.box2{
background-color: #eee;
width: 200px;
height: 200px;
position: relative;
margin-top: 20px ;
display: flex;
justify-content: center;
align-items: center;
}
.center4{
width: 50px;
height: 50px;
background-color: #B39873;
}
效果展示:
前面介紹的是元素如何實現(xiàn)水平垂直居中,下面介紹的是如何將文字進行水平垂直居中。這第一個方法也是最經(jīng)常用的,使用文本水平對齊 text-align 和行高 line-height 來實現(xiàn)的。
HTML 代碼
<div class="box3">
<div class="center5">文字居中</div>
</div>
CSS 代碼
.box3{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
}
.center5{
text-align: center;
line-height: 200px;
}
效果展示
05
PART
第二個方法可以通過網(wǎng)格布局 grid 來實現(xiàn)。而這里通過 grid 有兩種方式實現(xiàn),一種對元素本身屬性進行設置,另一種在元素的父級元素中設置。兩者看上去內容似乎差不多,不同的是在元素中設置的是 align-self 還要多了一個 margin,父級元素中是 align-items。
相關代碼:
/* grid 元素中設置 */
.box4{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
display: grid;
}
.center6{
align-self: center;
justify-content: center;
margin: auto;
}
/* grid 父級元素中設置 */
.box5{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
display: grid;
align-items: center;
justify-content: center;
}
效果展示:
以上就是關于 CSS 如何將元素或者文字進行水平垂直居中的幾種常用方法,大家還其他關于 CSS 實現(xiàn)水平垂直居中的方法嗎?請在評論區(qū)留下你的想法。
關注w3cschool編程獅訂閱更多IT資訊、技術干貨~
通常首選方法是使用flexbox居中內容。只需三行代碼即可:display:flex,然后使用 align-items:center 和 justify-content:center 將子元素垂直和水平居中。
如下代碼:
html:
<div class="flexbox-centering">
<div>Centered content.</div>
</div>
css:
.flexbox-centering {
display: flex;
justify-content: center;
align-items: center;
height: 100px;
}
使用grid(網(wǎng)格)與flexbox非常相似,也是一種常見的技術,尤其是布局中已經(jīng)使用網(wǎng)格的情況下。與前一種flexbox技術的唯一區(qū)別是它顯示為柵格。
如下代碼:
html:
<div class="grid-centering">
<div class="child">Centered content.</div>
</div>
css:
信ID:WEB_wysj(點擊關注) ◎ ◎ ◎ ◎ ◎◎◎◎◎一┳═┻︻▄
(頁底留言開放,歡迎來吐槽)
● ● ●
在網(wǎng)頁上使 HTML 元素居中看似一件很簡單的事情. 至少在某些情況下是這樣的,但是復雜的布局往往使一些解決方案不能很好的發(fā)揮作用。
在網(wǎng)頁布局中元素水平居中比元素垂直居中要簡單不少,同時實現(xiàn)水平居中和垂直居中往往是最難的。現(xiàn)在是響應式設計的時代,我們很難確切的知道元素的準確高度和寬度,所以一些方案不大適用。據(jù)我所知, 在CSS中至少有六種實現(xiàn)居中的方法。我將使用下面的HTML結構從簡單到復雜開始講解:
1 2 3 |
|
鞋子圖片會改變,但是他們都會保持500pxX500px的大小。 HSL colors 用于使背景顏色保持一致。
使用text-align水平居中
有時顯而易見的方案是最佳的選擇:
1 2 3 4 5 6 7 8 |
|
這種方案沒有使圖片垂直居中:你需要給<div> 添加 padding 或者給內容添加 margin-top和 margin-bottom使容器與內容之間有一定的距離。
使用 margin: auto 居中
這種方式實現(xiàn)水平居中和上面使用text-align的方法有相同局限性。
1 2 3 4 5 6 7 8 9 |
|
注意: 必須使用display: block使 margin: 0 auto對img元素生效。
使用table-cell居中
使用 display: table-cell, 而不是使用table標簽; 可以實現(xiàn)水平居中和垂直居中,但是這種方法需要添加額外的元素作為外部容器。
1 2 3 4 5 |
|
CSS:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
注意:為了使div 不折疊必須加上 width: 100%,外部容器元素也需要加上一定高度使得內容垂直居中。給html和body設置高度后,也可以使元素在body垂直居中。此方法在IE8+瀏覽器上生效。
使用absolute定位居中
這種 方案 有非常好的跨瀏覽器支持。有一個缺點就是必須顯式聲明外部容器元素的height:
.absolute-aligned { position: relative; min-height: 500px; background: hsl(200, 100%, 97%);
}
.absolute-aligned img { width: 50%; min-width: 200px; height: auto; overflow: auto; margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0;
}
Stephen在他的 博客 中演示了這種方案的幾種變化。
使用translate居中
Chris Coiyer 提出了一個使用 CSS transforms 的新方案。 同樣支持水平居中和垂直居中:
1 2 3 4 5 6 7 8 9 10 11 |
|
但是有以下幾種缺點:
CSS transform 在部分就瀏覽器上需要使用 前綴。
不支持 IE9 以下的瀏覽器。
外部容器需要設置height (或者用其他方式設置),因為不能獲取 絕對定位 的內容的高度。
如果內容包含文字,現(xiàn)在的瀏覽器合成技術會使文字模糊不清。
使用Flexbox居中
當新舊語法差異和瀏覽器前綴消失時,這種方法會成為主流的居中方案。
1 2 3 4 5 6 7 8 9 |
|
在很多方面 flexbox 是一種簡單的方案, 但是它有新舊兩種語法以及早期版本的IE缺乏支持 (盡管可以使用 display: table-cell作為降級方案)。
現(xiàn)在規(guī)范已經(jīng)最終確定,現(xiàn)代瀏覽器也大都支持,我寫了一篇詳細的教程 教程。
使用calc居中
在某些情況下比flexbox更全面:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
很簡單,calc 允許你基于當前的頁面布局計算尺寸。在上面的簡單計算中, 50% 是容器元素的中心點,但是如果只設置50%會使圖片的左上角對齊div的中心位置。 我們需要把圖片向左和向上各移動圖片寬高的一半。計算公式為:
1 2 |
|
在現(xiàn)在的瀏覽其中你會發(fā)現(xiàn),這種方法更適用于當內容的寬高為固定尺寸:
1 2 3 4 5 6 |
|
我在 這篇文章 中詳細講解了calc。
這種方案和flex一樣有許多相同的缺點: 雖然在現(xiàn)代瀏覽器中有良好的支持,但是在較早的版本中仍然需要瀏覽器前綴,并且不支持IE8。
1 2 3 4 5 6 |
|
當然還有 其他更多的方案。理解這七種方案之后,web開發(fā)人員在面對元素居中的時候會有更多的選擇。
干貨!免費領取騰訊高級講師網(wǎng)頁設計教程
點我領取
點擊下方“閱讀原文”結交更多有才華的設計師!
↓↓↓
*請認真填寫需求信息,我們會在24小時內與您取得聯(lián)系。