|
|
【一】知道居中元素的宽高% Y, {2 z) q2 d3 R: e. }4 u
absolute + 负margin
$ t9 u7 S) E9 Y/ j" `' 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;}
复制代码 运行结果
" Z: H2 P& g3 u7 w$ j, s5 _& R ?- N$ X
absolute + margin auto% m1 t y9 \8 j
代码实现- .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;}
复制代码
" w( S, a, _6 G2 I/ _* Mabsolute + calc+ c% |( Y5 ?; ~& W
代码实现- .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);}
复制代码 运行结果
% i$ e: t R4 b
) e2 |6 J6 m+ ^6 [三种对比总结: h d+ j/ l8 _' R+ u4 ^
当居中元素知道宽高的时候,设置居中的方式比较简单单一。三种方法的本质是一样的,都是对居中元素进行绝对定位,在定位到上50%,左50%后再拉回居中元素的一半宽高实现真正的居中。三种方式的区别就在于拉回元素本身宽高的方式不同。0 [9 E( ]) }# F) f4 G
【二】居中元素的宽高未知
! a; Y# }5 m* T& Z; s" }absolute + transform& t/ l+ Q) m2 \6 v
代码实现- .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%);}
复制代码 运行结果
N9 y5 |6 f% o1 m, v) X. c+ h5 A* c, ?: n1 i
原理
1 ?, V& J! }& z2 P原理类似于已知宽高的实现方式,只不过当前居中元素宽高未知,所以需要自动获取当前居中元素的宽高。translate属性正好实现了该功能。1 {3 O7 |9 x" \2 H& r% y, z& M `! s
优缺点: G- Z9 S; X- p+ A" R! {8 y
优点:自动计算本身的宽高
1 ~6 O8 ~: ~; T4 V, A' M缺点:如果同时使用transform的其他属性会产生相互影响。( z9 E5 E* Y5 }: W2 z* V
所以:在不使用transform的其他属性时推荐使用该方式! O F+ i/ L0 |8 w$ v, k+ F7 F6 O
flex布局- .wrapBox2{ width: 300px; height: 300px; background: blue; display: flex; justify-content: center; align-items: center;}/*注意:即使不设置子元素为行块元素也不会独占一层*/.wrapItem2{ width: 100px; height: 50px; background:green;}
复制代码运行结果 . X5 O/ q. ^$ X! B: P) H
" O J* M% P! { o原理
, b; [0 K- @' _( V# }将父级元素设置为流式布局,根据flex布局的属性特性,设置子元素居中。* \1 C1 D" O" f8 l T5 U
优缺点0 [& Y9 J0 K& A+ z
优点:flex布局灵活,不需要对子元素进行任何的设置5 F+ c- t( c( k! H: `
缺点:具有兼容性。子元素的float、clear、position等无法使用,如果同时具有其他布局,容易产生影响。/ X8 F. w7 G: m& E$ h1 E4 u
table-cell布局
# B% R3 g! G/ ?8 o代码实现- .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;}
复制代码 运行结果
" y7 V# Z( {6 T8 N0 T% N. h
% i3 c( i4 _5 c! `0 f7 l0 Q7 I0 g原理
7 j! g$ G! J* ^: ?根据table的vertical-align属性,可以在表格元素内设置元素居中,再根据text-align设置水平居中" {( Q6 @7 j8 \, i
table元素: c) ]& h8 M# a- T1 i7 @! O
代码实现- .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;}
复制代码 运行结果
; o& L) [8 g7 k3 P9 Z( n' Z5 Y7 R8 \2 U9 _' \2 Y1 _' E/ T
总结
) F1 d- I$ R2 y; F' t# p使用table标签进行布局,主要还是使用了vertical-align属性和text-align属性。但是相对于上一种方式,使用table标签会产生大量的冗余代码。不推荐使用
/ L% L6 N s, {3 R+ Y/ S8 E到此这篇关于css实现元素垂直居中显示的7种方式的文章就介绍到这了,更多相关css 元素垂直居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!5 T* G# Q' w8 j5 m' r$ F* M
0 Z5 |5 E( e6 X5 A+ [5 Q8 l
来源:http://www.jb51.net/css/743771.html+ I. W4 {* O- A/ h N+ S
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|