|
|
【一】知道居中元素的宽高: Y2 z! l7 k4 |. q% m: a
absolute + 负margin0 ] m. j1 L: O% Y) j0 i
代码实现- .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;}
复制代码 运行结果3 u9 [- A9 d. E5 N
4 }; [) |+ z0 v: w; mabsolute + margin auto
* q5 `% n6 E* \4 x9 B6 C6 t代码实现- .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;}
复制代码
2 J0 J) k# b% ]. X X* Yabsolute + calc2 y4 a! m* E) N
代码实现- .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);}
复制代码 运行结果" v: l0 K g7 `8 \) F; `3 c2 w! N% g
4 o! ~" N- ]! F: X三种对比总结* D5 B# S0 N2 c, C% d4 p' l$ w: J& D
当居中元素知道宽高的时候,设置居中的方式比较简单单一。三种方法的本质是一样的,都是对居中元素进行绝对定位,在定位到上50%,左50%后再拉回居中元素的一半宽高实现真正的居中。三种方式的区别就在于拉回元素本身宽高的方式不同。
6 v" B' g% Z, Y% c4 ]【二】居中元素的宽高未知
) l! z, W& t; r* cabsolute + transform
o; m( g5 W( q/ c7 @代码实现- .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%);}
复制代码 运行结果
) s3 h3 I6 [/ ?! h* W/ n* Y4 x% V5 r3 W+ ]" f- ^ T
原理
- t F) h3 r& u$ L原理类似于已知宽高的实现方式,只不过当前居中元素宽高未知,所以需要自动获取当前居中元素的宽高。translate属性正好实现了该功能。
2 u% q' D& R3 G) V+ Q& J0 ~优缺点
2 S, y! `2 L9 o优点:自动计算本身的宽高# _3 x& Y0 s) K: o- s3 h( W
缺点:如果同时使用transform的其他属性会产生相互影响。( N9 f& p/ j$ q: i
所以:在不使用transform的其他属性时推荐使用该方式. V b7 y* e3 t9 ~3 H7 Q
flex布局- .wrapBox2{ width: 300px; height: 300px; background: blue; display: flex; justify-content: center; align-items: center;}/*注意:即使不设置子元素为行块元素也不会独占一层*/.wrapItem2{ width: 100px; height: 50px; background:green;}
复制代码运行结果
9 }! N$ A! ~0 I) ^$ R. x$ I, r' l" i: ` c; @$ ~ Q8 @
原理8 q2 H- x1 I- A5 { D- N
将父级元素设置为流式布局,根据flex布局的属性特性,设置子元素居中。
3 ~5 G. |% v) ~9 _: P优缺点# {2 S: \1 ?2 ~' F, f# s! Q. H1 v
优点:flex布局灵活,不需要对子元素进行任何的设置
) u5 V7 X2 @9 A" I) p q# E缺点:具有兼容性。子元素的float、clear、position等无法使用,如果同时具有其他布局,容易产生影响。" E, ^. g/ n0 C+ d/ E1 N+ G
table-cell布局
' ?6 W& X. G6 Q7 h8 P: U& C代码实现- .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;}
复制代码 运行结果
2 J- o6 H S; Q# n9 d" f9 |0 T5 C# `5 B: O% C# y1 a1 F( J2 |
原理2 I3 L4 v1 S# a1 t7 J
根据table的vertical-align属性,可以在表格元素内设置元素居中,再根据text-align设置水平居中
) a9 y# j, y$ X/ x" ftable元素
& i( \3 G( Y1 O* g l% ~! c代码实现- .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;}
复制代码 运行结果; _2 A' E# w% k- a& L
% b* K- K$ x6 |, Q: {' b7 k
总结
* \$ Z2 C1 {; O2 h. i( i使用table标签进行布局,主要还是使用了vertical-align属性和text-align属性。但是相对于上一种方式,使用table标签会产生大量的冗余代码。不推荐使用/ K# c( i% K; c3 v- k
到此这篇关于css实现元素垂直居中显示的7种方式的文章就介绍到这了,更多相关css 元素垂直居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!8 u1 u5 j6 D& n5 U: W
2 ^% V* a7 I& K4 q+ ?
来源:http://www.jb51.net/css/743771.html
# ^/ u& D8 s, m& c* Y0 X免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|