|
|
【一】知道居中元素的宽高6 _% o4 n9 V1 D) U6 J
absolute + 负margin1 T2 _5 T' r- A/ }' A: 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;}
复制代码 运行结果
: T! \, I! f4 ^( @: q/ h8 `, W1 G5 K$ |
absolute + margin auto9 q2 B: b' F8 t
代码实现- .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;}
复制代码
6 L5 d5 i$ t2 vabsolute + calc5 \+ R. s$ e2 @3 A- p9 }# M# U! T
代码实现- .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 m8 S. j0 Z5 v, [( j
" j% `9 a: L! A [4 W8 I
三种对比总结! M" V G7 U( r2 v) x6 d' l
当居中元素知道宽高的时候,设置居中的方式比较简单单一。三种方法的本质是一样的,都是对居中元素进行绝对定位,在定位到上50%,左50%后再拉回居中元素的一半宽高实现真正的居中。三种方式的区别就在于拉回元素本身宽高的方式不同。( N3 l/ t) a# W) f! h
【二】居中元素的宽高未知
/ R* ^" K7 F0 C; U7 J wabsolute + transform
X+ R" h$ @8 V& [! 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%);}
复制代码 运行结果
5 s" d' Q# h% T- C, S$ [' s3 F" A' s* i# i" L
原理7 N" b7 c$ Q* Z
原理类似于已知宽高的实现方式,只不过当前居中元素宽高未知,所以需要自动获取当前居中元素的宽高。translate属性正好实现了该功能。2 t4 c1 T! U3 a* ]$ m* ~7 G9 c% S$ q% m
优缺点
; I; X) y1 I; d5 Y2 Y0 S3 F优点:自动计算本身的宽高
9 m' s- [0 o0 v7 }# L i! t( C4 f缺点:如果同时使用transform的其他属性会产生相互影响。; q$ H8 j! t; R
所以:在不使用transform的其他属性时推荐使用该方式
+ p2 b2 l; L: X5 ]; S1 R, tflex布局- .wrapBox2{ width: 300px; height: 300px; background: blue; display: flex; justify-content: center; align-items: center;}/*注意:即使不设置子元素为行块元素也不会独占一层*/.wrapItem2{ width: 100px; height: 50px; background:green;}
复制代码运行结果
$ n6 z* p. Z( h1 s+ d B3 m) N7 n! H- ^' ~+ b
原理
* w8 J( ^6 Y9 _7 @+ c: ]将父级元素设置为流式布局,根据flex布局的属性特性,设置子元素居中。1 O9 I2 T5 A( ]6 r8 E
优缺点6 e! X8 s4 i* @# Q8 R; c
优点:flex布局灵活,不需要对子元素进行任何的设置* X( i# l# I! Y' C
缺点:具有兼容性。子元素的float、clear、position等无法使用,如果同时具有其他布局,容易产生影响。
, Y( y8 ^$ Y* @! p; L; b6 V' `" ^table-cell布局
) _/ G" Y% I1 K0 s" z代码实现- .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;}
复制代码 运行结果, o6 r( N0 G% o% z. Z# g; C
7 G" _! c6 l# E" }6 ^$ U3 {原理
( l8 D. |3 o% x( l" @7 _根据table的vertical-align属性,可以在表格元素内设置元素居中,再根据text-align设置水平居中
4 {4 ?, E. }- b$ i1 M* \table元素* _% k) B' m5 F1 V
代码实现- .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;}
复制代码 运行结果& e+ `6 e- D) o9 t1 m2 e: J4 V3 O. d
3 L" @! y( U$ y7 |$ ^! u* k总结# m5 V/ t! Y. ^" ^5 ~
使用table标签进行布局,主要还是使用了vertical-align属性和text-align属性。但是相对于上一种方式,使用table标签会产生大量的冗余代码。不推荐使用
0 e; v1 O2 D8 b& y7 C: @ C: \到此这篇关于css实现元素垂直居中显示的7种方式的文章就介绍到这了,更多相关css 元素垂直居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!
9 t! a0 ~% S) |6 n. U1 \( Q
9 r: i* W; \7 u. S: l. a0 Q8 p8 C9 {来源:http://www.jb51.net/css/743771.html( K( z; }, T# Y, D
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|