京东6.18大促主会场领京享红包更优惠

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3888|回复: 0

css实现元素垂直居中显示的7种方式

[复制链接]

18

主题

0

回帖

10

积分

新手上路

积分
10
发表于 2020-9-22 00:50:13 | 显示全部楼层 |阅读模式 来自 中国
【一】知道居中元素的宽高9 b# I! c2 E5 W
absolute + 负margin# k% e8 o3 l1 w2 C# p/ h% ]
代码实现
  1. .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;}
复制代码
运行结果2 J7 ]' j5 E( |7 g  o" m

0 |: x7 ?7 j8 }5 A9 l2 Vabsolute + margin auto  g; Z  e. m: @
代码实现
  1. .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;}
复制代码

$ Q* f; Y# _* q% vabsolute + calc) f8 _! E! z7 @0 {' `! D
代码实现
  1. .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);}
复制代码
运行结果
# R. E$ ]) l! P7 Z3 Y! t4 v
' a% z$ E3 o9 |
三种对比总结1 n! X% `7 {! J( H
当居中元素知道宽高的时候,设置居中的方式比较简单单一。三种方法的本质是一样的,都是对居中元素进行绝对定位,在定位到上50%,左50%后再拉回居中元素的一半宽高实现真正的居中。三种方式的区别就在于拉回元素本身宽高的方式不同。/ N- \! I  [& M$ c; P
【二】居中元素的宽高未知* t" ?0 D, _( ?6 Y
absolute + transform
6 `+ S0 v( \4 ?2 d, o代码实现
  1. .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%);}
复制代码
运行结果/ h, e' g. V. S: o2 ]5 l9 ]( k. w

! {8 c% e/ l5 K3 O2 p1 Z原理
3 o) W% v: M+ }( E原理类似于已知宽高的实现方式,只不过当前居中元素宽高未知,所以需要自动获取当前居中元素的宽高。translate属性正好实现了该功能。8 o6 U* a/ b  d& \
优缺点8 S: ]! h! G" q
优点:自动计算本身的宽高
2 \9 y9 ~8 D5 P5 w9 S( T缺点:如果同时使用transform的其他属性会产生相互影响。
# S5 x" P& f1 B# _所以:在不使用transform的其他属性时推荐使用该方式
! U$ Y6 F5 R) [" Dflex布局
  1. .wrapBox2{    width: 300px;    height: 300px;    background: blue;    display: flex;    justify-content: center;    align-items: center;}/*注意:即使不设置子元素为行块元素也不会独占一层*/.wrapItem2{    width: 100px;    height: 50px;    background:green;}
复制代码
运行结果

2 W( P, r0 ?5 l

  `0 G! W# m! |; i0 O* ]& u原理
9 S& C: Q- x& \2 v8 |将父级元素设置为流式布局,根据flex布局的属性特性,设置子元素居中。
$ f/ p8 r# _5 r+ {1 B( [优缺点
: l( o; \. L7 S优点:flex布局灵活,不需要对子元素进行任何的设置
) l0 d# |: L. w. P缺点:具有兼容性。子元素的float、clear、position等无法使用,如果同时具有其他布局,容易产生影响。7 g0 u5 c1 I8 `2 ?9 p
table-cell布局' S% X8 K$ o) f5 N2 G3 o) }$ t
代码实现
  1. .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;}
复制代码
运行结果
/ F3 Q. x( @  t; x% o# T% P

9 Q  A3 F! m. @% O( m/ X原理
/ l" O0 Z0 v' G+ k根据table的vertical-align属性,可以在表格元素内设置元素居中,再根据text-align设置水平居中
( K5 c5 \$ z0 R) R( d  \table元素* b0 _" ~4 {) d  g3 _5 S- I& e
代码实现
  1. .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;}
复制代码
运行结果1 j( E7 G) b( J7 X
0 R# O# O1 B. q' j
总结6 T. q, ]0 u2 g$ i
使用table标签进行布局,主要还是使用了vertical-align属性和text-align属性。但是相对于上一种方式,使用table标签会产生大量的冗余代码。不推荐使用4 j" n; ^4 e" o6 T; y
到此这篇关于css实现元素垂直居中显示的7种方式的文章就介绍到这了,更多相关css 元素垂直居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!. p1 ^$ s4 Z: q0 H5 x- h! w

: S$ a3 }# n2 G! u来源:http://www.jb51.net/css/743771.html
" ]( w2 a- D+ U% L0 s免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

帖子地址: 

梦想之都-俊月星空 优酷自频道欢迎您 http://i.youku.com/zhaojun917
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|梦想之都-俊月星空 ( 粤ICP备18056059号 )|网站地图

GMT+8, 2026-10-21 22:21 , Processed in 0.041232 second(s), 24 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表