|
|
【一】知道居中元素的宽高" X4 t! }. {% l7 S2 c9 x
absolute + 负margin
8 U( }- z. w0 ^5 h8 k% B代码实现- .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;}
复制代码 运行结果) P1 d- g# W+ g5 n2 D
! U. v; O; Q$ n
absolute + margin auto- ], |9 \2 \$ Q A2 C/ q
代码实现- .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;}
复制代码
5 p3 [5 k1 E( ~! d+ M0 ]absolute + calc
|4 [8 o4 ~$ [% a: W. i代码实现- .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);}
复制代码 运行结果
0 N8 M8 O# \5 k r9 g1 W
4 |( W6 B1 B4 R6 }$ n! c% u/ y, B三种对比总结5 m: r" b7 Y& w. S( O$ b' ~
当居中元素知道宽高的时候,设置居中的方式比较简单单一。三种方法的本质是一样的,都是对居中元素进行绝对定位,在定位到上50%,左50%后再拉回居中元素的一半宽高实现真正的居中。三种方式的区别就在于拉回元素本身宽高的方式不同。
+ n. [" }& r: q【二】居中元素的宽高未知& ?. g8 G/ z3 w" p9 ^- @( F0 x
absolute + transform
: _) G+ s; {- k代码实现- .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%);}
复制代码 运行结果
% ^; L8 `" \ U5 a$ w
/ s9 k3 e- {; o6 z6 i原理
- _9 K; \' a/ x$ @$ f Z原理类似于已知宽高的实现方式,只不过当前居中元素宽高未知,所以需要自动获取当前居中元素的宽高。translate属性正好实现了该功能。0 C) u5 t4 ~+ S# ^0 ?+ h7 H- H
优缺点1 V7 q u* w, D- S
优点:自动计算本身的宽高" E4 Z" {- k: I3 S x; c, f5 I
缺点:如果同时使用transform的其他属性会产生相互影响。2 o2 t0 u5 N. Y8 ~
所以:在不使用transform的其他属性时推荐使用该方式" U) _/ S' Q3 M4 [$ s% _( t
flex布局- .wrapBox2{ width: 300px; height: 300px; background: blue; display: flex; justify-content: center; align-items: center;}/*注意:即使不设置子元素为行块元素也不会独占一层*/.wrapItem2{ width: 100px; height: 50px; background:green;}
复制代码运行结果 " L5 `( H" O8 q# j, ~9 A- T
, \% ?8 w0 h n: |+ _
原理
" O* D( e. B9 J; O- d$ S将父级元素设置为流式布局,根据flex布局的属性特性,设置子元素居中。5 t A8 V5 i4 O$ K* _
优缺点
( N& ?' e' |. i- R( Z) s( q8 m7 |7 V优点:flex布局灵活,不需要对子元素进行任何的设置
! S( j5 h4 d3 K. \) E1 a8 Y7 [缺点:具有兼容性。子元素的float、clear、position等无法使用,如果同时具有其他布局,容易产生影响。7 D6 m3 H- q5 a: N" r; y' W4 _
table-cell布局
' e# Z5 Q# Z+ i5 k. @6 h- G% ?# T代码实现- .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! i/ |" F% L/ \' a: h( C6 w6 }
8 p6 b! k1 I' e* Y" p7 t原理
7 S j5 a& i7 \0 J0 T; W根据table的vertical-align属性,可以在表格元素内设置元素居中,再根据text-align设置水平居中6 a& f- e; N7 s
table元素
+ O1 ^, p/ a- A+ g: G" o0 @( Z/ 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;}
复制代码 运行结果( c* S5 `* @3 A$ y
1 U/ h" D2 d# o" L' O' P总结* H/ Y( B/ [7 E7 R* c# E% q, ?+ @' _! S
使用table标签进行布局,主要还是使用了vertical-align属性和text-align属性。但是相对于上一种方式,使用table标签会产生大量的冗余代码。不推荐使用
* A! L; r% K9 `6 _到此这篇关于css实现元素垂直居中显示的7种方式的文章就介绍到这了,更多相关css 元素垂直居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!6 }; |8 Z" m) j3 h+ S
3 X9 \1 u. H( ^6 R来源:http://www.jb51.net/css/743771.html
+ u! ]2 C% D3 J% D" u: G* J1 E免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|