|
|
【一】知道居中元素的宽高, f2 R9 o( ?* W' l( p
absolute + 负margin
2 H# b& X/ s' B2 k* l0 S代码实现- .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;}
复制代码 运行结果0 A: f+ q0 Y$ b3 l
! W6 B2 f; Y! {% I2 C
absolute + margin auto
" Y+ N& O+ v) K* x# v代码实现- .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* E2 ?# ~ O
absolute + calc
6 H; ], D9 a! e: Z( x代码实现- .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);}
复制代码 运行结果
* e) L% w1 E' ~3 k0 s' H/ m" p! p4 e5 U
三种对比总结
0 h1 [, |5 d/ L6 i" z' g当居中元素知道宽高的时候,设置居中的方式比较简单单一。三种方法的本质是一样的,都是对居中元素进行绝对定位,在定位到上50%,左50%后再拉回居中元素的一半宽高实现真正的居中。三种方式的区别就在于拉回元素本身宽高的方式不同。
/ E* Z; n$ I2 E【二】居中元素的宽高未知
) @) P( a9 n# f# U2 jabsolute + transform2 h) p0 l, w4 {
代码实现- .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%);}
复制代码 运行结果
3 f9 d% i0 D1 Q3 ?8 V1 \) Z3 y7 \" W3 z4 c6 ]0 E
原理! }4 i/ [- J5 _
原理类似于已知宽高的实现方式,只不过当前居中元素宽高未知,所以需要自动获取当前居中元素的宽高。translate属性正好实现了该功能。9 m. t1 G5 X. @' |- `
优缺点
; ~9 x( k% u5 I! E% s+ s5 W9 s优点:自动计算本身的宽高6 Z4 q5 q- S6 A& q; }" d
缺点:如果同时使用transform的其他属性会产生相互影响。
: b$ z! y, w7 M2 ]: n2 O所以:在不使用transform的其他属性时推荐使用该方式3 Z- X: d T7 d* I b b3 n1 E
flex布局- .wrapBox2{ width: 300px; height: 300px; background: blue; display: flex; justify-content: center; align-items: center;}/*注意:即使不设置子元素为行块元素也不会独占一层*/.wrapItem2{ width: 100px; height: 50px; background:green;}
复制代码运行结果
- y) o3 f0 u) ^2 q" i' j C( F, V
( b9 j! T F) l7 w( U9 S1 x3 g原理2 p4 U- C" `) h" f
将父级元素设置为流式布局,根据flex布局的属性特性,设置子元素居中。- s) D1 L% U5 h3 a; }: ]- z5 O5 x8 d
优缺点
& X, j4 q7 s+ ]$ \优点:flex布局灵活,不需要对子元素进行任何的设置
0 w) y( e( ~( L( u7 p' w3 F) c9 U. h缺点:具有兼容性。子元素的float、clear、position等无法使用,如果同时具有其他布局,容易产生影响。/ d/ N0 k1 K; |( q
table-cell布局
7 R4 F( q/ W! I1 e" s2 l0 q; {代码实现- .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;}
复制代码 运行结果
- O4 N9 |* P% R8 J w8 x0 j: ?
原理( S5 V7 E J) n. G O' h$ ]% F) L2 ]
根据table的vertical-align属性,可以在表格元素内设置元素居中,再根据text-align设置水平居中
9 S' U! r& Q/ L; itable元素
9 }/ v% B( B3 P* L) Z3 k5 s) C* A代码实现- .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;}
复制代码 运行结果
+ K0 M& M) p8 j* y+ l4 H* d: E+ |" P8 E2 `' F- Q; E
总结) W) ?4 Y) p7 K6 {# Y
使用table标签进行布局,主要还是使用了vertical-align属性和text-align属性。但是相对于上一种方式,使用table标签会产生大量的冗余代码。不推荐使用
" }8 h5 C$ E, @7 n* G0 j到此这篇关于css实现元素垂直居中显示的7种方式的文章就介绍到这了,更多相关css 元素垂直居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!- ^$ U o& \* }+ J8 p R* L
' x3 J: X6 c4 X7 c* t- S# q6 [! L
来源:http://www.jb51.net/css/743771.html
! f9 S8 L" K8 J% A. p) h8 H3 Y免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|