|
|
【一】知道居中元素的宽高
* \7 b5 O9 s1 o7 O, C: o% ^absolute + 负margin& i! ~; q1 G' Z( U" f
代码实现- .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;}
复制代码 运行结果
7 d2 l& Y9 e h7 |/ j( j2 E4 p' z- x. Y/ m' X8 c( v
absolute + margin auto
* Y! I: e9 s1 o' h& t4 ?6 [( ], H代码实现- .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;}
复制代码 ! D* w `" E5 c$ `
absolute + calc
z9 x% N8 j& t1 M8 S* d' B代码实现- .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);}
复制代码 运行结果
: [! f+ Y' t8 b) D
( g T* C$ N. n4 y9 Y" K三种对比总结( H1 N8 H8 q8 G9 _! N+ F# K' H
当居中元素知道宽高的时候,设置居中的方式比较简单单一。三种方法的本质是一样的,都是对居中元素进行绝对定位,在定位到上50%,左50%后再拉回居中元素的一半宽高实现真正的居中。三种方式的区别就在于拉回元素本身宽高的方式不同。
0 k& Z, l* A; W! K% X【二】居中元素的宽高未知" i; O3 T, Y/ p
absolute + transform) Q* u* M0 P& B8 |5 t: [' 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%);}
复制代码 运行结果
, S# @" `& u7 I+ P. R: O; s$ r/ W) X* B4 W" M, G
原理
) {0 k0 V* L$ r+ P) V1 M原理类似于已知宽高的实现方式,只不过当前居中元素宽高未知,所以需要自动获取当前居中元素的宽高。translate属性正好实现了该功能。
s7 C/ a K9 q* o+ ~优缺点0 s4 V5 u( [) U. V/ S
优点:自动计算本身的宽高2 d, k0 S4 t/ d) T
缺点:如果同时使用transform的其他属性会产生相互影响。1 Z! t3 j* p3 L+ R7 ?
所以:在不使用transform的其他属性时推荐使用该方式3 H, X+ n0 K/ e, @* E
flex布局- .wrapBox2{ width: 300px; height: 300px; background: blue; display: flex; justify-content: center; align-items: center;}/*注意:即使不设置子元素为行块元素也不会独占一层*/.wrapItem2{ width: 100px; height: 50px; background:green;}
复制代码运行结果 4 H3 [" \2 O3 ~& @. P6 o
* e, x3 e4 ^1 `% ]* U2 G/ t" R6 [1 M原理
. F: a( F. k- A% z% g* E$ z将父级元素设置为流式布局,根据flex布局的属性特性,设置子元素居中。
2 C% _/ u% s& G& Q8 M6 f优缺点
7 ?7 k3 S$ @+ D优点:flex布局灵活,不需要对子元素进行任何的设置
7 V. s7 v' P/ S' o6 y4 t缺点:具有兼容性。子元素的float、clear、position等无法使用,如果同时具有其他布局,容易产生影响。9 s1 I# B' T" [0 `* i
table-cell布局- s; V/ \8 }2 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;}
复制代码 运行结果+ n8 d+ d1 ~- ^! N" C' H0 [1 p' `! p$ }
# t: Q. g; ]6 E8 ^0 }, Y原理
: H, o, G# P3 `0 w# m根据table的vertical-align属性,可以在表格元素内设置元素居中,再根据text-align设置水平居中7 E, o; l4 \! }
table元素! a2 U5 G, l0 |+ F2 u% U4 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;}
复制代码 运行结果$ _5 m" t' i2 @' [( T
1 g# G8 S0 k" s: W3 U5 M/ j, u
总结! H' ]- z/ A6 m: p, W+ P
使用table标签进行布局,主要还是使用了vertical-align属性和text-align属性。但是相对于上一种方式,使用table标签会产生大量的冗余代码。不推荐使用
3 C# O. F i$ k0 _到此这篇关于css实现元素垂直居中显示的7种方式的文章就介绍到这了,更多相关css 元素垂直居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!
H: K4 |* U$ F- |& \, K9 c& w) N" h
来源:http://www.jb51.net/css/743771.html: M5 }) ^8 t; F' ^& E
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|