|
|
【一】知道居中元素的宽高$ {. ?( _4 h! f0 V, @; J+ M
absolute + 负margin
9 j: h4 a6 t [* h- f" r代码实现- .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;}
复制代码 运行结果
/ j' y! w4 I+ e2 N: C: Z6 C' O5 S8 z6 Y2 p: X2 I. z& ]/ B8 R4 \* y
absolute + margin auto, l3 B0 s7 r: k" Q9 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;}
复制代码
$ s; e* w" ^. Q8 u! ~absolute + calc1 {2 @! L' ?" A# y7 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);}
复制代码 运行结果5 O4 L; L+ e. V, f8 W+ _1 B
4 c/ \$ S! h+ [: J7 ^/ K8 T三种对比总结/ T; J, H! X1 a1 P. k! t8 G" F
当居中元素知道宽高的时候,设置居中的方式比较简单单一。三种方法的本质是一样的,都是对居中元素进行绝对定位,在定位到上50%,左50%后再拉回居中元素的一半宽高实现真正的居中。三种方式的区别就在于拉回元素本身宽高的方式不同。/ O0 L/ [! r$ ?1 v( g3 n+ z
【二】居中元素的宽高未知6 }0 b9 U* z0 }9 q
absolute + transform
$ R* G4 Z9 \# X( e P6 t$ 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%);}
复制代码 运行结果2 R1 q: `1 I2 d$ a& L7 [$ _
8 q* |7 g9 T* F* {0 k) t6 B原理# f$ J- s# t8 K: \% J* l; B
原理类似于已知宽高的实现方式,只不过当前居中元素宽高未知,所以需要自动获取当前居中元素的宽高。translate属性正好实现了该功能。' O9 Q) R4 t3 d( T2 M- r
优缺点
% l! _9 s$ m, W$ I; C优点:自动计算本身的宽高' q( U) @, f s- a+ U& g3 @
缺点:如果同时使用transform的其他属性会产生相互影响。. u1 N4 W3 g E4 ?' j, E
所以:在不使用transform的其他属性时推荐使用该方式
6 C8 [1 _* S+ f) Uflex布局- .wrapBox2{ width: 300px; height: 300px; background: blue; display: flex; justify-content: center; align-items: center;}/*注意:即使不设置子元素为行块元素也不会独占一层*/.wrapItem2{ width: 100px; height: 50px; background:green;}
复制代码运行结果
8 @" n8 l+ P- U, S, o- _5 }+ h1 F* E
原理% r: L' `, S1 p* Y E- J9 [
将父级元素设置为流式布局,根据flex布局的属性特性,设置子元素居中。) t$ G+ p5 p8 T0 n: k' _3 K
优缺点 X/ W t- |9 j. j3 j
优点:flex布局灵活,不需要对子元素进行任何的设置
0 ~0 `3 V/ s8 h) |# f9 \缺点:具有兼容性。子元素的float、clear、position等无法使用,如果同时具有其他布局,容易产生影响。/ m4 I V; _0 y0 W. I) i9 T
table-cell布局
9 U9 H: t6 c$ O5 Z1 Y6 D代码实现- .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;}
复制代码 运行结果# S/ [6 m% [/ f6 |" j$ f1 @0 f
7 a; C' `' i+ k" P `+ h原理
[7 g; g) g' N6 h9 X1 p- [根据table的vertical-align属性,可以在表格元素内设置元素居中,再根据text-align设置水平居中
- }& C: b f5 D- k8 _# Ptable元素
2 v! y) l* @$ \& `6 y4 H代码实现- .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;}
复制代码 运行结果
8 x% J) {7 T) C, ?/ P6 K7 E) t
8 y) L" [: i" v6 E6 p; t总结
) y) o$ J w1 x i+ W使用table标签进行布局,主要还是使用了vertical-align属性和text-align属性。但是相对于上一种方式,使用table标签会产生大量的冗余代码。不推荐使用
( T3 i- W6 Y( `到此这篇关于css实现元素垂直居中显示的7种方式的文章就介绍到这了,更多相关css 元素垂直居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!4 c1 [7 `! ?; f4 ?" M6 o$ }) r
6 A7 J, y) K' Y9 R) l
来源:http://www.jb51.net/css/743771.html
- S1 I8 f& r, R p. d5 M# B免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|