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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3478|回复: 0

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

[复制链接]

18

主题

0

回帖

10

积分

新手上路

积分
10
发表于 2020-9-22 00:50:13 | 显示全部楼层 |阅读模式 来自 中国
【一】知道居中元素的宽高& g. ?  t* T9 i: |8 z$ c; ?4 m  U
absolute + 负margin
1 z- K  L  e, [; @+ l& G2 R代码实现
  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;}
复制代码
运行结果+ r: g5 |1 t% Y! C* x  q& Y
: J7 v& w7 C* e  S) K
absolute + margin auto0 i: M& b; R. c1 f
代码实现
  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;}
复制代码

3 j% f9 R4 Y, _2 yabsolute + calc
. Z4 f* ~/ [4 r1 w: Z  M% c代码实现
  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);}
复制代码
运行结果/ C! ?: H) B# f
9 q. r" u' e. s! D' I! `
三种对比总结
6 K* S$ r3 t$ l6 ]. m; v# n; Y# r- U当居中元素知道宽高的时候,设置居中的方式比较简单单一。三种方法的本质是一样的,都是对居中元素进行绝对定位,在定位到上50%,左50%后再拉回居中元素的一半宽高实现真正的居中。三种方式的区别就在于拉回元素本身宽高的方式不同。
. m! f  D9 n8 W9 h【二】居中元素的宽高未知0 Y: i) J' A9 Y% \: a& e5 ~5 E
absolute + transform: o& |. q: l  _5 E9 h$ j: g. y8 G
代码实现
  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%);}
复制代码
运行结果3 [/ u+ q, a  q5 \
$ w2 b" e. ~6 K7 w* p! ^
原理% O, H8 B+ f" _8 ~6 N& w2 ?- q
原理类似于已知宽高的实现方式,只不过当前居中元素宽高未知,所以需要自动获取当前居中元素的宽高。translate属性正好实现了该功能。" Z) |( Z2 K: l: V9 d9 }  d
优缺点3 z) T7 M/ |- A$ ^3 _3 i
优点:自动计算本身的宽高3 V# n- D" f4 [9 k5 f
缺点:如果同时使用transform的其他属性会产生相互影响。
7 J% g8 D7 K4 }- G所以:在不使用transform的其他属性时推荐使用该方式, _( z9 L5 x0 s8 p
flex布局
  1. .wrapBox2{    width: 300px;    height: 300px;    background: blue;    display: flex;    justify-content: center;    align-items: center;}/*注意:即使不设置子元素为行块元素也不会独占一层*/.wrapItem2{    width: 100px;    height: 50px;    background:green;}
复制代码
运行结果

+ ~% t9 o- Q3 b8 x0 G+ n

7 D9 X8 k( T! q# \$ @原理* d- x( x( m5 U! v) k
将父级元素设置为流式布局,根据flex布局的属性特性,设置子元素居中。
) e% k, W- O) `2 \% K优缺点3 N+ x0 }  a9 N0 e; F# \' o, u
优点:flex布局灵活,不需要对子元素进行任何的设置
5 ]8 d: v& ]& k, k" u缺点:具有兼容性。子元素的float、clear、position等无法使用,如果同时具有其他布局,容易产生影响。- W1 l& F1 s7 U0 l  U
table-cell布局% U5 d6 n3 y# }  H
代码实现
  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;}
复制代码
运行结果% L$ ?1 C; F5 G& a
: R9 ]- t" L2 e7 s+ g; ?
原理' g+ b8 K6 I2 i2 M1 j
根据table的vertical-align属性,可以在表格元素内设置元素居中,再根据text-align设置水平居中
! p- y5 V4 k) k# b' Itable元素' a3 L2 [2 g3 a6 n( ^
代码实现
  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;}
复制代码
运行结果4 @& R6 P) v) A# [7 y

! I! y8 W, W2 P$ X总结- q( f' Y# {; p' M) l1 Y8 I( Y: c
使用table标签进行布局,主要还是使用了vertical-align属性和text-align属性。但是相对于上一种方式,使用table标签会产生大量的冗余代码。不推荐使用
% H" ?! E& H- }3 z6 h% A1 ^! b到此这篇关于css实现元素垂直居中显示的7种方式的文章就介绍到这了,更多相关css 元素垂直居中内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!( a9 S5 K+ V# t  C) K

: P  x4 Z) L$ T1 A: [8 M来源:http://www.jb51.net/css/743771.html' u' R; W* ?1 m5 r! Z
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

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

×

帖子地址: 

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-22 17:14 , Processed in 0.043770 second(s), 23 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2026 Discuz! Team.

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