|
|
【一】知道居中元素的宽高
" X1 }( l5 F2 C4 }) @; Nabsolute + 负margin
, e3 y( j8 `4 e4 W' H5 q' ^代码实现- .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# A4 k1 V& G6 _. l
! X l/ O3 g w* d& C# D6 Babsolute + margin auto Q' f( Y+ f4 [) N; S2 w
代码实现- .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 E! k; o- X5 h2 b; Xabsolute + calc
- K( R1 E0 |. U1 V4 |% {代码实现- .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);}
复制代码 运行结果4 d2 \' L8 X: t6 A5 x5 E" O7 R! h
! e& `1 R* v; p5 _& G- R; z
三种对比总结6 c j9 s7 g+ J
当居中元素知道宽高的时候,设置居中的方式比较简单单一。三种方法的本质是一样的,都是对居中元素进行绝对定位,在定位到上50%,左50%后再拉回居中元素的一半宽高实现真正的居中。三种方式的区别就在于拉回元素本身宽高的方式不同。0 o, w2 l+ {% q- Y _! `
【二】居中元素的宽高未知
4 K# Q( W' G# A' ~0 ]: @1 ]absolute + transform
% U- {9 ~3 t" g代码实现- .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%);}
复制代码 运行结果
! _6 H+ }6 m) p3 Z# P* Y, D+ x( n2 F% E1 e
原理3 a2 p$ g$ ]# W+ W& V
原理类似于已知宽高的实现方式,只不过当前居中元素宽高未知,所以需要自动获取当前居中元素的宽高。translate属性正好实现了该功能。4 ?3 j* |1 F2 U" o- a
优缺点, {& Z7 \3 Y! ^. V8 R& B/ s
优点:自动计算本身的宽高( F7 d6 g& o& n+ g# R9 X
缺点:如果同时使用transform的其他属性会产生相互影响。: l1 A/ H0 N# C! U' V
所以:在不使用transform的其他属性时推荐使用该方式
* }" P) e% m- \7 ~- s+ E: hflex布局- .wrapBox2{ width: 300px; height: 300px; background: blue; display: flex; justify-content: center; align-items: center;}/*注意:即使不设置子元素为行块元素也不会独占一层*/.wrapItem2{ width: 100px; height: 50px; background:green;}
复制代码运行结果
* Y; _) Q ]; V; W ]' Q4 ~/ I5 Q0 h
& h2 q: Q- c3 q1 t2 T- t* J5 W原理" [+ n0 M! b, m% ~, m/ Q+ Y! B
将父级元素设置为流式布局,根据flex布局的属性特性,设置子元素居中。5 W' P/ |$ ]5 Q/ Y' n; S5 c4 x
优缺点8 y" b+ K. i4 J+ i
优点:flex布局灵活,不需要对子元素进行任何的设置
; F5 E2 \, ?; w& t6 D j缺点:具有兼容性。子元素的float、clear、position等无法使用,如果同时具有其他布局,容易产生影响。7 B! v- W5 c1 w; Y
table-cell布局% ^- E. D9 [( Q5 s
代码实现- .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;}
复制代码 运行结果
' u# R$ O6 V7 E4 W- {! S4 ]% p+ U
原理! |/ b5 ~' G" T5 a7 z
根据table的vertical-align属性,可以在表格元素内设置元素居中,再根据text-align设置水平居中
4 m" ]# a, B; V: Rtable元素
7 o* C9 U2 }) @& T7 i( N代码实现- .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 M P1 F1 n: T/ [* b7 S
/ A5 T* L" }7 C
总结
; Z: G9 M' k. L; r' G使用table标签进行布局,主要还是使用了vertical-align属性和text-align属性。但是相对于上一种方式,使用table标签会产生大量的冗余代码。不推荐使用
% f9 F9 D3 o' E, ~% m' M到此这篇关于css实现元素垂直居中显示的7种方式的文章就介绍到这了,更多相关css 元素垂直居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!
8 s) c3 R% \1 o+ _1 {9 j, q- W
2 t, M! C/ M( q: p* i5 ?5 Z来源:http://www.jb51.net/css/743771.html
4 m+ |9 \3 Y4 w. ]免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|