|
|
【一】知道居中元素的宽高$ Q6 Q0 S0 t+ _5 `9 O
absolute + 负margin Y- x- V B! f7 k5 {5 O
代码实现- .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;}
复制代码 运行结果
9 T) R" L" k0 W7 {" O' I W( A" _/ ]1 ?4 J2 V$ ~5 E7 m
absolute + margin auto
; I- n+ e9 Y b5 S5 _代码实现- .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;}
复制代码 " e7 t/ H8 i& z( X
absolute + calc8 K I/ b4 w/ s2 r* H
代码实现- .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 q- n% M }% N/ v f& O; W- F) ?7 O' |$ t9 |4 p& |
三种对比总结9 p) H. n! I) ~' M: N
当居中元素知道宽高的时候,设置居中的方式比较简单单一。三种方法的本质是一样的,都是对居中元素进行绝对定位,在定位到上50%,左50%后再拉回居中元素的一半宽高实现真正的居中。三种方式的区别就在于拉回元素本身宽高的方式不同。* Y/ @' ?1 m, B, q1 a( _6 E. t3 r0 p% k
【二】居中元素的宽高未知
( }, s7 T) x& k) I% W6 {absolute + transform
, \' Z8 x4 H' p; s% n: U& a代码实现- .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 f* e* y2 `9 ?) G/ ~
9 [6 ~% ]% g4 _9 e( E
原理/ z& M& E) Z, s8 A# b4 k R
原理类似于已知宽高的实现方式,只不过当前居中元素宽高未知,所以需要自动获取当前居中元素的宽高。translate属性正好实现了该功能。2 T- u& H5 y5 W2 {! K" w+ J
优缺点
$ s+ c1 L8 d2 y; x' f/ e优点:自动计算本身的宽高
4 m7 _! z+ b% m t+ l6 n8 I j缺点:如果同时使用transform的其他属性会产生相互影响。& s4 ^1 O2 \& r) B
所以:在不使用transform的其他属性时推荐使用该方式7 x# i+ Q$ F3 S c4 `* t
flex布局- .wrapBox2{ width: 300px; height: 300px; background: blue; display: flex; justify-content: center; align-items: center;}/*注意:即使不设置子元素为行块元素也不会独占一层*/.wrapItem2{ width: 100px; height: 50px; background:green;}
复制代码运行结果 & t/ k. l8 }) s% z/ ~8 _' |6 Y
' J5 I [( b; U6 Q( q+ X8 \原理
2 _, H. I W6 n4 x2 D将父级元素设置为流式布局,根据flex布局的属性特性,设置子元素居中。7 _ c8 e% g# d% X% q8 |
优缺点
( \1 B6 V4 \ C% F" T优点:flex布局灵活,不需要对子元素进行任何的设置
! O' }2 d$ V5 S# }' H) `缺点:具有兼容性。子元素的float、clear、position等无法使用,如果同时具有其他布局,容易产生影响。
' V& ]! q; Z' j& ctable-cell布局2 h, F( c" f/ b/ I# H& v0 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;}
复制代码 运行结果
" _5 e; z8 H) J% Q0 o# Q0 R( O% R$ G6 P) Q
原理
5 u# z- @! i% @+ S& T根据table的vertical-align属性,可以在表格元素内设置元素居中,再根据text-align设置水平居中
1 g4 x( A6 d; {# {8 e4 mtable元素) J5 Y4 ?$ E. 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;}
复制代码 运行结果
$ g$ t5 \3 A |8 ?) c3 B2 e8 H \' B, Y4 W9 v- \: W+ c
总结8 w9 O5 s0 H* r
使用table标签进行布局,主要还是使用了vertical-align属性和text-align属性。但是相对于上一种方式,使用table标签会产生大量的冗余代码。不推荐使用/ m' O+ u4 S7 x+ K' | v' i- J
到此这篇关于css实现元素垂直居中显示的7种方式的文章就介绍到这了,更多相关css 元素垂直居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!; a: X; e8 [: {3 |; m- D! [) ^. Q h
" q7 W* Q* D9 I+ j9 V8 j/ n
来源:http://www.jb51.net/css/743771.html
7 Y" P, D) W, f% u8 A0 C# h免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|