|
|
【一】知道居中元素的宽高
5 ` Q1 x0 C/ A, a' dabsolute + 负margin; O$ {5 N ]) n3 _
代码实现- .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;}
复制代码 运行结果/ C4 J/ b; Z* `! A" S% @
- t$ y2 u3 o: ]8 g, E) ?
absolute + margin auto5 s' e$ r0 H4 E! b: 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;}
复制代码
6 x, ]9 @0 G {. D+ ?) Wabsolute + calc9 I% c/ E9 ]& M2 u6 E1 f$ Z3 H* \5 e- T
代码实现- .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);}
复制代码 运行结果
( A, }! r2 v( ?/ D
. W7 B* F) E* |* v% @3 M8 C三种对比总结
" } x) ]% _( t3 H当居中元素知道宽高的时候,设置居中的方式比较简单单一。三种方法的本质是一样的,都是对居中元素进行绝对定位,在定位到上50%,左50%后再拉回居中元素的一半宽高实现真正的居中。三种方式的区别就在于拉回元素本身宽高的方式不同。
& M( @4 c0 f1 I$ Y【二】居中元素的宽高未知
8 E0 r" w, \4 y( L4 K9 u. wabsolute + transform# I4 @: D( b7 K3 V2 M, ]3 i
代码实现- .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%);}
复制代码 运行结果
9 L. i& v* W* Y4 t! @$ h& l$ g' H' l1 @2 V8 _& o% x
原理
. `# \7 s6 K5 _原理类似于已知宽高的实现方式,只不过当前居中元素宽高未知,所以需要自动获取当前居中元素的宽高。translate属性正好实现了该功能。2 R; |9 Q" E6 j* u8 G$ [$ h# D
优缺点' h7 o" T( B0 L, h5 q" d
优点:自动计算本身的宽高9 r1 Z% S& u# m4 k
缺点:如果同时使用transform的其他属性会产生相互影响。
u8 x# D! P* b" Q( s- S; C2 ~7 [所以:在不使用transform的其他属性时推荐使用该方式7 }$ m8 R5 G& \5 L' J. J
flex布局- .wrapBox2{ width: 300px; height: 300px; background: blue; display: flex; justify-content: center; align-items: center;}/*注意:即使不设置子元素为行块元素也不会独占一层*/.wrapItem2{ width: 100px; height: 50px; background:green;}
复制代码运行结果
9 T3 ]2 G2 P" O9 M3 I$ ` m5 A- z0 f& s
0 l3 A8 ?/ _2 q原理5 Z; [. m. ?' i8 R8 H* X
将父级元素设置为流式布局,根据flex布局的属性特性,设置子元素居中。
! V6 m: }8 t% Y5 c; j优缺点
7 W$ d: p$ v$ }+ \优点:flex布局灵活,不需要对子元素进行任何的设置' a) E f1 a t
缺点:具有兼容性。子元素的float、clear、position等无法使用,如果同时具有其他布局,容易产生影响。; K8 e/ R: H/ \: c/ c
table-cell布局
$ C3 H' m: ?6 I3 w代码实现- .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;}
复制代码 运行结果$ Q' l, H6 W; ?3 ]+ p) Z; s
$ m3 y! C, s7 ~* z' X f! U
原理* u; S5 d, ^1 }+ j9 L; ?+ F
根据table的vertical-align属性,可以在表格元素内设置元素居中,再根据text-align设置水平居中* Q3 a% o* K; F T p9 m
table元素
r3 H7 |5 X. w- V6 L! Q1 E代码实现- .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;}
复制代码 运行结果
, g2 c( e# k( h3 r. G% f% ]% K$ C* ], C% }, p- v
总结9 |/ A/ o4 ?5 L
使用table标签进行布局,主要还是使用了vertical-align属性和text-align属性。但是相对于上一种方式,使用table标签会产生大量的冗余代码。不推荐使用/ J1 s, O* ^( u. o
到此这篇关于css实现元素垂直居中显示的7种方式的文章就介绍到这了,更多相关css 元素垂直居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!, e5 }$ d4 H/ S" G* }% V3 }+ p3 o0 _
: R* e* i. t W1 r来源:http://www.jb51.net/css/743771.html
9 ~7 ?! I% p9 |! P" P/ s" M T免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|