|
|
【一】知道居中元素的宽高: F- l& ?+ b9 o- x, |5 z
absolute + 负margin
! _! G9 |/ ?5 ?2 i4 g代码实现- .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;}
复制代码 运行结果& b1 ^4 O, X1 i! n
( q0 S+ i( h9 O- U" c( Q4 z- j" t) K
absolute + margin auto
# d, x6 @8 W. \( Z代码实现- .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;}
复制代码 {. N& S+ `$ c" R
absolute + calc; T& M' }0 A! d5 c$ P5 [* p
代码实现- .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);}
复制代码 运行结果
4 J. M( |, n9 L! r0 r7 c6 A. s' z# O, a0 g
三种对比总结
/ H3 T2 R, ]/ V当居中元素知道宽高的时候,设置居中的方式比较简单单一。三种方法的本质是一样的,都是对居中元素进行绝对定位,在定位到上50%,左50%后再拉回居中元素的一半宽高实现真正的居中。三种方式的区别就在于拉回元素本身宽高的方式不同。- {( K' s. F6 p
【二】居中元素的宽高未知
1 P1 s" O" p0 i7 B" q+ F& vabsolute + transform
8 r n5 h* d% G7 k6 `% f1 H9 G代码实现- .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%);}
复制代码 运行结果. q1 D8 V, F. q
h; \+ }, }( u- ]( o$ J原理
: n& h7 U b7 M* s原理类似于已知宽高的实现方式,只不过当前居中元素宽高未知,所以需要自动获取当前居中元素的宽高。translate属性正好实现了该功能。$ _' R3 \5 f: v3 ]0 ?+ M% U
优缺点
4 {) l/ K3 P8 v8 g9 |! V& ]6 z优点:自动计算本身的宽高& O0 E/ ?8 T' S( o
缺点:如果同时使用transform的其他属性会产生相互影响。+ e) Y1 E/ @9 d: \* s# D: _" ]% v
所以:在不使用transform的其他属性时推荐使用该方式
; p4 p2 K; h% f4 c: a/ nflex布局- .wrapBox2{ width: 300px; height: 300px; background: blue; display: flex; justify-content: center; align-items: center;}/*注意:即使不设置子元素为行块元素也不会独占一层*/.wrapItem2{ width: 100px; height: 50px; background:green;}
复制代码运行结果 4 D ]8 L2 x2 C* S" F5 \7 M
5 _# B/ t6 Q4 v* q6 M8 F% M' S, `! b
原理
! e6 h9 t: ^9 w0 j将父级元素设置为流式布局,根据flex布局的属性特性,设置子元素居中。0 ?1 Q( Q" j. n$ N$ ^
优缺点
3 E# c$ m6 Z/ i% u& [) t7 _8 \ P优点:flex布局灵活,不需要对子元素进行任何的设置
5 s. s& Y h4 X8 S缺点:具有兼容性。子元素的float、clear、position等无法使用,如果同时具有其他布局,容易产生影响。6 }/ [: C4 B ^
table-cell布局3 `5 M8 G3 P: s" j3 v, d5 X
代码实现- .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;}
复制代码 运行结果
' i8 Q" Z5 h4 d: |4 W2 [; C6 f7 a% B, _5 n7 l( J
原理8 h6 B& P( z, A2 c" H# x
根据table的vertical-align属性,可以在表格元素内设置元素居中,再根据text-align设置水平居中
+ D' P( O; y7 ]& Ttable元素" [9 C" i1 a2 [7 Z T% ~( s
代码实现- .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;}
复制代码 运行结果
+ D8 {* Z3 P4 c; M( | {2 x' n" e% C# a- M. G, o4 y/ Y; P
总结* J& r5 w5 ?& o7 C
使用table标签进行布局,主要还是使用了vertical-align属性和text-align属性。但是相对于上一种方式,使用table标签会产生大量的冗余代码。不推荐使用
3 ~$ `9 R* U9 _; {) h4 k* U; K& q6 i5 n到此这篇关于css实现元素垂直居中显示的7种方式的文章就介绍到这了,更多相关css 元素垂直居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!
7 K+ Q" P/ V" T6 ^- {2 y( F/ W i* M* G& M
来源:http://www.jb51.net/css/743771.html3 p* h, Y: \8 b* @* ~5 f8 k$ W( e+ F
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|