CSS 垂直居中
http://www.laixiangran.cn/2018/04/30/CSS%E5%AE%9E%E7%8E%B0%E5%85%83%E7%B4%A0%E5%B1%85%E4%B8%AD%E5%8E%9F%E7%90%86%E8%A7%A3%E6%9E%90/
?
CSS實現元素居中原理解析
在 CSS 中要設置元素水平垂直居中是一個非常常見的需求了。但就是這樣一個從理論上來看似乎實現起來極其簡單的,在實踐中,它往往難住了很多人。
讓元素水平居中相對比較簡單:如果它是一個行內元素,就對它的父元素應用?text-align: center;如果它是一個塊級元素,就對它自身應用?margin: auto。
然而如果要對一個元素進行垂直居中,那就沒有那么容易了,有時候光是想想就令人頭皮發麻了。
本文分別從行內元素和塊級元素進行說明,將目前比較流行的實現方式進行匯集并解析實現原理,方便大家查閱。這里要說明一點,每一種方式都不是十全十美的,關鍵要看自己的需求,從而分析出哪種實現方式是最合適的。
行內元素
首先我們先把基礎代碼寫出來:
| 1 2 3 | <div class="main"> <span class="content">我是要居中的行內元素span</span> </div> |
| 1 2 3 4 5 6 7 8 9 10 | .main { width: 300px; height: 300px; background-color: #50ba8b; } .content { background-color: #5b4d4e; color: #FFFFFF; } |
class 為?.main?的 div 包裹這一個 class 為?.content?的行內元素 span,我們的目的就是要讓?.content?元素 在?.main元素中居中。
水平居中
text-align
行內元素的水平居中比較簡單,我們直接在?.main?中添加?text-align: center;?即可,此時?.main?變為:
| 1 2 3 4 5 6 7 | .main { width: 300px; height: 300px; background-color: #50ba8b; text-align: center; /* 水平居中 */ } |
實現原理:?設置?text-align?的值為?center?,因為該屬性規定元素中的文本的水平對齊方式,那么設置為?center?則文本就水平居中了。
垂直居中
line-height
行內元素的垂直居中我們分為?一行?和?多行或者圖片等替換元素?來說明。
如果是?一行,那么我們可以使用?line-height?來實現,此時?.main?元素 css 代碼變為:
| 1 2 3 4 5 6 7 | .main { width: 300px; height: 300px; /* 可以不設置 */ background-color: #50ba8b; line-height: 300px; /* 垂直居中 */ } |
其實設置了?line-height?就可以讓文本垂直居中,并不需要同時設置?height,這里也是一直存在的一個誤區。
實現原理:?這種方式實現垂直居中運用的是 CSS 中“行距的上下等分機制”,這也說明了為什么該方式只適用于?一行?的文本。
還有一點需要說明是,這種方式實現的垂直居中是“近似”的,并不是完美的垂直居中,因為文字字形的垂直中線位置普遍要比真正的“行框盒子”的垂直中線位置低,而由于我們平時使用的 font-size 比較小,使得這點偏差不容易察覺出來,那么感官上也就看成是垂直居中了。
line-height 及 vertical-align
下面再來說說?多行或者圖片等替換元素?的垂直居中效果實現,這里我們需要同時借助?line-height?和?vertical-align來實現。
先讓文本換行:
| 1 2 3 4 5 6 | <div class="main"> <span class="content"> 我是要居中的行內元素span <br> 我是要居中的行內元素span </span> </div> |
再看修改之后的 css 代碼:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | .main { width: 300px; background-color: #50ba8b; line-height: 300px; } .content { display: inline-block; background-color: #5b4d4e; color: #FFFFFF; line-height: 20px; margin: 0 20px; vertical-align: middle; } |
實現原理:
塊級元素
依然先把基礎代碼寫出來:
| 1 2 3 | <div class="main"> <div class="content">我是要居中的塊級元素div</div> </div> |
| 1 2 3 4 5 6 7 8 9 10 11 | .main { width: 300px; height: 300px; background-color: #50ba8b; } .content { width: 150px; height: 150px; background-color: #5b4d4e; } |
class 為?.main?的 div 包裹這一個 class 為?.content?的 塊級元素 div,我們的目的就是要讓?.content?元素 在?.main?元素中居中。
position + margin: auto
實現代碼如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | .main { width: 300px; height: 300px; background-color: #50ba8b; /*關鍵代碼*/ position: relative; } .content { width: 150px; height: 150px; background-color: #5b4d4e; /*關鍵代碼*/ position: absolute; top: 0; left: 0; bottom: 0; right: 0; margin: auto; } |
實現原理:
position + margin-left/top
實現代碼如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | .main { width: 300px; height: 300px; background-color: #50ba8b; /*關鍵代碼*/ position: relative; } .content { width: 150px; height: 150px; background-color: #5b4d4e; /*關鍵代碼*/ position: absolute; top: 50%; left: 50%; margin-left: -75px; margin-top: -75px; } |
實現原理:
position + translate
實現代碼如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | .main { width: 300px; height: 300px; background-color: #50ba8b; /*關鍵代碼*/ position: relative; } .content { width: 150px; height: 150px; background-color: #5b4d4e; /*關鍵代碼*/ position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } |
實現原理:
Flexbox
實現代碼如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | .main { width: 300px; height: 300px; background-color: #50ba8b; /*關鍵代碼*/ display: flex; } .content { width: 150px; height: 150px; background-color: #5b4d4e; /*關鍵代碼*/ margin: auto; } |
實現原理:
Flexbox 的另一個好處在于,它還可以將匿名容器(即沒有被標簽包裹的文本節點)垂直居中。比如我們不設置?.main?元素為?display: flex;,而是設置?.content?元素為?display: flex;,并借助 Flexbox 規范所引入的 align-items 和 justify-content 屬性,我們可以讓它內部的文本也實現居中(我們可以對.main?元素使用相同的屬性來使?.content?元素元素居中,但比?margin: auto?方法要更加優雅一些,并且同時起到了回退的作用)。
| 1 2 3 4 5 6 7 8 9 10 11 | .content { width: 150px; height: 150px; background-color: #5b4d4e; /*關鍵代碼*/ display: flex; align-items: center; justify-content: center; margin: auto; } |
轉載于:https://www.cnblogs.com/rjjs/p/10489347.html
總結
- 上一篇: 查询
- 下一篇: CSS响应式:根据分辨率加载不同CSS的