|
|
【一】知道居中元素的宽高
2 f- N( g9 h6 uabsolute + 负margin
$ i4 T* x7 h. R5 o) j# U1 s/ V代码实现- .wrapBox5{ width: 300px; height: 300px; border:1px solid red; position: relative;}.wrapItem5{ width: 100px; height: 50px; position: absolute; background:yellow; top: 50%; left:50%; margin-top: -25px; margin-left: -50px;}
复制代码 运行结果5 o( f3 A% c" n/ L \
$ r5 @" N7 L8 j% `3 }
absolute + margin auto, L3 w0 ^0 b( O' o& q
代码实现- .wrapBox{ width: 300px; height: 300px; background: yellow; position: relative;}.wrapItem{ width: 100px; height: 50px; background:green; display: inline-block; position: absolute; top: 0px; bottom:0px; left: 0px; right: 0px; margin:auto;}
复制代码 " O) L7 B6 t3 i$ W3 R- c' r
absolute + calc4 h/ R, {+ b' [+ Q' F1 U- R
代码实现- .wrapBox6{ width: 300px; height: 300px; border:1px solid green; position: relative;}.wrapItem6{ width: 100px; height: 50px; position: absolute; background:yellow; top: calc(50% - 25px); left:calc(50% - 50px);}
复制代码 运行结果9 G# [3 p9 X! U0 n7 A3 c# h
2 B) U2 h3 h& ~三种对比总结0 b7 G. a% V( i# Y4 x+ U
当居中元素知道宽高的时候,设置居中的方式比较简单单一。三种方法的本质是一样的,都是对居中元素进行绝对定位,在定位到上50%,左50%后再拉回居中元素的一半宽高实现真正的居中。三种方式的区别就在于拉回元素本身宽高的方式不同。
* R- b3 m$ ] }7 v( @' _. X$ H t【二】居中元素的宽高未知) J& b8 F% a) l0 a9 q- `
absolute + transform
! ^4 k7 p( X8 E5 c5 Y代码实现- .wrapBox{ width: 300px; height: 300px; background:#ddd; position: relative;}.wrapItem{ width: 100px; height: 50px; background:green; position: absolute; top: 50%; left:50%; transform: translate(-50% , -50%);}
复制代码 运行结果
- g0 c: G1 R7 D/ L
1 Y/ A9 J: a }0 ]2 z原理
7 C# ?7 L& d2 f; |原理类似于已知宽高的实现方式,只不过当前居中元素宽高未知,所以需要自动获取当前居中元素的宽高。translate属性正好实现了该功能。
' }# P4 b) ^% T' f- h g优缺点: C' M6 _5 E/ J' u8 c( Q" P
优点:自动计算本身的宽高
+ O: c3 D9 E! d, l缺点:如果同时使用transform的其他属性会产生相互影响。9 d$ h8 a( k P' B
所以:在不使用transform的其他属性时推荐使用该方式
" d& O w3 f; C4 ?: ?3 D1 ] T4 Zflex布局- .wrapBox2{ width: 300px; height: 300px; background: blue; display: flex; justify-content: center; align-items: center;}/*注意:即使不设置子元素为行块元素也不会独占一层*/.wrapItem2{ width: 100px; height: 50px; background:green;}
复制代码运行结果
$ O2 e! Z& f4 d* p1 }- `2 {/ f" I4 K% x" X2 ]& ]
原理# e( L% J0 f! I) P
将父级元素设置为流式布局,根据flex布局的属性特性,设置子元素居中。
9 n) X, o8 d$ s优缺点6 F7 f" C3 t/ ^" A: X, [
优点:flex布局灵活,不需要对子元素进行任何的设置! t* o5 H9 k! U# b
缺点:具有兼容性。子元素的float、clear、position等无法使用,如果同时具有其他布局,容易产生影响。# ]2 ]9 a9 R& R! z$ w3 S$ }7 {
table-cell布局
* f1 `4 q) o7 S4 ^2 \代码实现- .wrapBox3{ width: 300px; height: 300px; background: yellow; display: table-cell; vertical-align: middle; text-align: center;}.wrapItem3{ width: 100px; height: 50px; background:green; display: inline-block;}
复制代码 运行结果6 a& V7 ~$ S% w, z5 P$ l
5 i# r4 F# g4 s1 f4 j1 \8 G# P" H原理4 W8 M" v$ Y: s& P$ N$ I
根据table的vertical-align属性,可以在表格元素内设置元素居中,再根据text-align设置水平居中
' t: J; ?' m8 z! E7 |+ A- N& k9 Stable元素
. \0 s" N/ t( Z代码实现- .tableBox{ border:2px solid yellow; width: 300px; height: 300px;}.tableBox table{ width:100%; height:100%;}.centerWrap{ text-align: center; vertical-align: middle;}.centerItem{ display: inline-block; background:pink;}
复制代码 运行结果
- _. D& x' N+ U- V! M) S1 @: t1 \6 S4 L: V/ T7 m9 i
总结 {% h6 d+ ?2 k, N* P- [
使用table标签进行布局,主要还是使用了vertical-align属性和text-align属性。但是相对于上一种方式,使用table标签会产生大量的冗余代码。不推荐使用
/ H! e$ L$ W0 u9 r5 @( }/ f* ?到此这篇关于css实现元素垂直居中显示的7种方式的文章就介绍到这了,更多相关css 元素垂直居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!& Z- [4 K0 n0 g3 s! n; m
. m- S* k5 B* o5 t5 j
来源:http://www.jb51.net/css/743771.html
, `; v/ ~' W% E- Q J4 L, J z免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|