|
|
【一】知道居中元素的宽高& |* a: B9 ^1 r
absolute + 负margin
7 O9 R3 `% M1 p0 K代码实现- .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 b- u- L3 b& E7 {
! s; d; j) `" f2 G0 X8 Z
absolute + margin auto' S& M; X4 Q6 U" _
代码实现- .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;}
复制代码
: Y5 i) I1 L7 ?2 w3 k/ @absolute + calc+ N2 _1 p, k c& s! \2 L$ [: v( 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);}
复制代码 运行结果
+ q0 @8 R3 s* {% z5 L1 g0 M' L) n* [. L! r
三种对比总结
& l2 Y' ~5 h, k y当居中元素知道宽高的时候,设置居中的方式比较简单单一。三种方法的本质是一样的,都是对居中元素进行绝对定位,在定位到上50%,左50%后再拉回居中元素的一半宽高实现真正的居中。三种方式的区别就在于拉回元素本身宽高的方式不同。6 G4 ^0 V! e- g4 }# Y
【二】居中元素的宽高未知
j* F! M7 h; }# t' _absolute + transform7 T! s/ ?0 d7 C
代码实现- .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%);}
复制代码 运行结果
8 X: Z/ e6 h- U7 E( { h( }0 D8 C, E5 r5 M5 I' m' c: m) _
原理7 W- U; S0 T" K* l
原理类似于已知宽高的实现方式,只不过当前居中元素宽高未知,所以需要自动获取当前居中元素的宽高。translate属性正好实现了该功能。
- ^9 w* z4 T. }% Z优缺点
9 F% U) x. j' _$ E% ?' g7 X3 Q( j优点:自动计算本身的宽高0 n N( V6 e# q+ U' u7 a
缺点:如果同时使用transform的其他属性会产生相互影响。$ {! L+ _8 S0 q D
所以:在不使用transform的其他属性时推荐使用该方式* A& ^3 e9 q+ R+ l* K$ _8 e. D
flex布局- .wrapBox2{ width: 300px; height: 300px; background: blue; display: flex; justify-content: center; align-items: center;}/*注意:即使不设置子元素为行块元素也不会独占一层*/.wrapItem2{ width: 100px; height: 50px; background:green;}
复制代码运行结果 6 k* t* ~ v7 @. E* R! u5 d- z
5 I1 F$ P8 G$ d& w9 c! b
原理
. b1 B7 ?! {6 F( j w0 Z将父级元素设置为流式布局,根据flex布局的属性特性,设置子元素居中。
! u" T% G; X: Z2 f6 d8 ~0 @优缺点$ h9 @- ^& S- j# t
优点:flex布局灵活,不需要对子元素进行任何的设置
$ f1 x' f/ c7 i* V+ ^& n- z缺点:具有兼容性。子元素的float、clear、position等无法使用,如果同时具有其他布局,容易产生影响。
+ `( i i% m) I% c: Xtable-cell布局* Y7 V1 G0 i( v' S; f( |, ?
代码实现- .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 |6 u6 n& u2 I
* c& n8 v. z& D+ j. n* Z原理
) F- V: p4 B7 h* d" g: W5 \根据table的vertical-align属性,可以在表格元素内设置元素居中,再根据text-align设置水平居中, @# p; u+ O" |. X, [; G
table元素( V7 C* x: H" O9 g
代码实现- .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 Q, N, U7 V4 F, y1 t
& ~: P- E$ ]8 T- ?# g总结2 L' q( z( J7 }) ~1 B: B" ^
使用table标签进行布局,主要还是使用了vertical-align属性和text-align属性。但是相对于上一种方式,使用table标签会产生大量的冗余代码。不推荐使用
. e4 I' G: m7 Q) k9 v0 p' S到此这篇关于css实现元素垂直居中显示的7种方式的文章就介绍到这了,更多相关css 元素垂直居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!
5 t; Y) V" Q$ ^! d7 s8 d2 `9 U$ J
2 [3 m) R( ]! \/ W# k来源:http://www.jb51.net/css/743771.html' Z( E5 }0 L8 [& `5 h5 n* j
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|