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),一種對元素本身屬性進行設置,另一種在元素的父級元素中設置。兩者看上去內(nèi)容似乎差不多,不同的是在元素中設置的是 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資訊、技術干貨~
者|顏海鏡
編輯|覃云
出處丨前端之巔
本文已獲作者授權,轉(zhuǎn)載來源:
https://segmentfault.com/a/1190000016389031
劃重點,這是一道面試必考題,很多面試官都喜歡問這個問題,我就被問過好幾次了。
要實現(xiàn)上圖的效果看似很簡單,實則暗藏玄機,本文總結了一下 CSS 實現(xiàn)水平垂直居中的方式大概有下面這些,本文將逐一介紹一下,我將本文整理成了一個 github 倉庫在:https://github.com/yanhaijing/vertical-center
歡迎大家 star。
僅居中元素定寬高適用:
居中元素不定寬高:
為了實現(xiàn)上面的效果先來做些準備工作,假設 HTML 代碼如下,總共兩個元素,父元素和子元素:
<div class="wp"> <div class="box size">123123</div> </div>
wp 是父元素的類名,box 是子元素的類名,因為有定寬和不定寬的區(qū)別,size 用來表示指定寬度,下面是所有效果都要用到的公共代碼,主要是設置顏色和寬高。
注意:后面不在重復這段公共代碼,只會給出相應提示。
/* 公共代碼 */ .wp { border: 1px solid red; width: 300px; height: 300px; } .box { background: green; } .box.size{ width: 100px; height: 100px; } /* 公共代碼 */
絕對定位的百分比是相對于父元素的寬高,通過這個特性可以讓子元素的居中顯示,但絕對定位是基于子元素的左上角,期望的效果是子元素的中心居中顯示。
為了修正這個問題,可以借助外邊距的負值,負的外邊距可以讓元素向相反方向定位,通過指定子元素的外邊距為子元素寬度一半的負值,就可以讓子元素居中了,css 代碼如下。
/* 此處引用上面的公共代碼 */ /* 此處引用上面的公共代碼 */ /* 定位代碼 */ .wp { position: relative; } .box { position: absolute;; top: 50%; left: 50%; margin-left: -50px; margin-top: -50px; }
這是我比較常用的方式,這種方式比較好理解,兼容性也很好,缺點是需要知道子元素的寬高。
點擊查看完整 DEMO:
http://yanhaijing.com/vertical-center/absolute1.html
這種方式也要求居中元素的寬高必須固定,HTML 代碼如下:
<div class="wp"> <div class="box size">123123</div> </div>
這種方式通過設置各個方向的距離都是 0,此時再講 margin 設為 auto,就可以在各個方向上居中了。
/* 此處引用上面的公共代碼 */ /* 此處引用上面的公共代碼 */ /* 定位代碼 */ .wp { position: relative; } .box { position: absolute;; top: 0; left: 0; right: 0; bottom: 0; margin: auto; }
這種方法兼容性也很好,缺點是需要知道子元素的寬高。
點擊查看完整 DEMO:
http://yanhaijing.com/vertical-center/absolute2.html
這種方式也要求居中元素的寬高必須固定,所以我們?yōu)?box 增加 size 類,HTML 代碼如下:
<div class="wp"> <div class="box size">123123</div> </div>
感謝 css3 帶來了計算屬性,既然 top 的百分比是基于元素的左上角,那么在減去寬度的一半就好了,代碼如下
/* 此處引用上面的公共代碼 */ /* 此處引用上面的公共代碼 */ /* 定位代碼 */ .wp { position: relative; } .box { position: absolute;; top: calc(50% - 50px); left: calc(50% - 50px); }
這種方法兼容性依賴 calc 的兼容性,缺點是需要知道子元素的寬高。
點擊查看完整 DEMO:
http://yanhaijing.com/vertical-center/absolute3.html
還是絕對定位,但這個方法不需要子元素固定寬高,所以不再需要 size 類了,HTML 代碼如下:
<div class="wp"> <div class="box">123123</div> </div>
修復絕對定位的問題,還可以使用 css3 新增的 transform,transform 的 translate 屬性也可以設置百分比,其是相對于自身的寬和高,所以可以講 translate 設置為 -50%,就可以做到居中了,代碼如下:
/* 此處引用上面的公共代碼 */ /* 此處引用上面的公共代碼 */ /* 定位代碼 */ .wp { position: relative; } .box { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
這種方法兼容性依賴 translate2d 的兼容性。
點擊查看完整 DEMO:
http://yanhaijing.com/vertical-center/absolute4.html
利用行內(nèi)元素居中屬性也可以做到水平垂直居中,HTML 代碼如下:
<div class="wp"> <div class="box">123123</div> </div>
把 box 設置為行內(nèi)元素,通過 text-align 就可以做到水平居中,但很多同學可能不知道通過通過 vertical-align 也可以在垂直方向做到居中,代碼如下:
/* 此處引用上面的公共代碼 */ /* 此處引用上面的公共代碼 */ /* 定位代碼 */ .wp { line-height: 300px; text-align: center; font-size: 0px; } .box { font-size: 16px; display: inline-block; vertical-align: middle; line-height: initial; text-align: left; /* 修正文字 */ }
這種方法需要在子元素中將文字顯示重置為想要的效果。
點擊查看完整 DEMO:
http://yanhaijing.com/vertical-center/lineheight.html
很多同學一定和我一樣不知道 writing-mode 屬性,感謝 @張鑫旭老師的反饋,簡單來說 writing-mode 可以改變文字的顯示方向,比如可以通過 writing-mode 讓文字的顯示變?yōu)榇怪狈较颉?/p>
<div class="div1">水平方向</div> <div class="div2">垂直方向</div> .div2 { writing-mode: vertical-lr; }
顯示效果如下:
水平方向 垂 直 方 向
更神奇的是所有水平方向上的 css 屬性,都會變?yōu)榇怪狈较蛏系膶傩裕热?text-align,通過 writing-mode 和 text-align 就可以做到水平和垂直方向的居中了,只不過要稍微麻煩一點:
<div class="wp"> <div class="wp-inner"> <div class="box">123123</div> </div> </div> /* 此處引用上面的公共代碼 */ /* 此處引用上面的公共代碼 */ /* 定位代碼 */ .wp { writing-mode: vertical-lr; text-align: center; } .wp-inner { writing-mode: horizontal-tb; display: inline-block; text-align: center; width: 100%; } .box { display: inline-block; margin: auto; text-align: left; }
這種方法實現(xiàn)起來和理解起來都稍微有些復雜。
點擊查看完整 DEMO:
http://yanhaijing.com/vertical-center/writing-mode.html
曾經(jīng) table 被用來做頁面布局,現(xiàn)在沒人這么做了,但 table 也能夠?qū)崿F(xiàn)水平垂直居中,但是會增加很多冗余代碼:
<table> <tbody> <tr> <td class="wp"> <div class="box">123123</div> </td> </tr> </tbody> </table>
tabel 單元格中的內(nèi)容天然就是垂直居中的,只要添加一個水平居中屬性就好了。
.wp { text-align: center; } .box { display: inline-block; }
這種方法就是代碼太冗余,而且也不是 table 的正確用法。
點擊查看完整 DEMO:
http://yanhaijing.com/vertical-center/table.html
css 新增的 table 屬性,可以讓我們把普通元素,變?yōu)?table 元素的現(xiàn)實效果,通過這個特性也可以實現(xiàn)水平垂直居中。
<div class="wp"> <div class="box">123123</div> </div>
下面通過 css 屬性,可以讓 div 顯示的和 table 一樣:
.wp { display: table-cell; text-align: center; vertical-align: middle; } .box { display: inline-block; }
這種方法和 table 一樣的原理,但卻沒有那么多冗余代碼,兼容性也還不錯。
點擊查看完整 DEMO:
http://yanhaijing.com/vertical-center/css-table.html
flex 作為現(xiàn)代的布局方案,顛覆了過去的經(jīng)驗,只需幾行代碼就可以優(yōu)雅的做到水平垂直居中。
<div class="wp"> <div class="box">123123</div> </div> .wp { display: flex; justify-content: center; align-items: center; }
目前在移動端已經(jīng)完全可以使用 flex 了,PC 端需要看自己業(yè)務的兼容性情況。
點擊查看完整 DEMO:
http://yanhaijing.com/vertical-center/flex.html
感謝 @一絲姐 反饋的這個方案,css 新出的網(wǎng)格布局,由于兼容性不太好,一直沒太關注,通過 grid 也可以實現(xiàn)水平垂直居中。
<div class="wp"> <div class="box">123123</div> </div> .wp { display: grid; } .box { align-self: center; justify-self: center; }
代碼量也很少,但兼容性不如 flex,不推薦使用。
點擊查看完整 DEMO:
http://yanhaijing.com/vertical-center/grid.html
下面對比下各個方式的優(yōu)缺點,肯定又雙叒叕該有同學說回字的寫法了,簡單總結下:
小貼士:關于 flex 的兼容性決方案,請看這里:
https://yanhaijing.com/css/2016/08/21/flex-practice-on-mobile/
最近發(fā)現(xiàn)很多同學都對 css 不夠重視,這其實是不正確的,比如下面的這么簡單的問題都有那么多同學不會,我也是很無語:
<div class="red blue">123</div> <div class="blue red">123</div> .red { color: red } .blue { color: blue }
問兩個 div 的顏色分別是什么,竟然只有 40% 的同學能夠答對,這 40% 中還有很多同學不知道為什么,希望這些同學好好補習下 CSS 基礎。
/* HTML */
<div class='father'>
<div class='son'></div>
</div>
<style>
.father {
display: table-cell;
vertical-align: middle;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
width: 50px;
height: 50px;
background-color: aqua;
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
position: relative;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
position: absolute;
background-color: aqua;
width: 50px;
height: 50px;
top: 0;
bottom: 0;
margin: auto;
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
position: relative;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
position: absolute;
width: 100px;
height: 100px;
background-color: aqua;
top: 50%;
/* 負margin須是高度的一半 */
margin-top: -50px;
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
position: relative;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
position: absolute;
width: 100px;
height: 100px;
background-color: aqua;
/* 注意"-"兩邊要隔開 減去的須是高度的一半*/
top: calc(50% - 50px);
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
position: relative;
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
position: absolute;
width: 100px;
height: 100px;
background-color: aqua;
top: 50%;
transform: translateY(-50%);
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
line-height: 300px;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
display: inline-block;
vertical-align: middle;
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
display: flex;
align-items: center;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
display: grid;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
align-self: center;
}
</style>
復制代碼
<!-- HTMl -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
display: block;
}
.father::after {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
.son {
background-color: aqua;
width: 50px;
height: 50px;
display: inline-block;
vertical-align: middle;
}
</style>
復制代碼
<!-- HTML -->
<div class="father">
<div class="hide"></div>
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
}
.son {
background-color: aqua;
width: 50%;
height: 50%;
}
.hide {
width: 50px;
height: 25%;
}
</style>
復制代碼
<!-- HTML -->
<div class="father">
<div class="son"></div>
</div>
<style>
.father {
width: 300px;
height: 300px;
border: 3px solid red;
writing-mode: vertical-lr;
text-align: center;
}
.son {
background-color: aqua;
width: 100px;
height: 100px;
writing-mode: horizontal-tb;
display: inline-block;
}
</style>
復制代碼
作者:soloplayer
鏈接:https://juejin.cn/post/6904138129612439560
來源:掘金
*請認真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。