CSS常用垂直居中方式

在网页布局中,我们有很多种方法可以做到元素的垂直居中,那么今天就来总结一下常用的一些方法吧

基础HTML标签

<div class="father">
        <div class="son">
            1234
        </div>
</div>

基础CSS样式

.father{
            width: 1000px;
            height: 800px;
            background-color: aqua;
        }
.son{
background-color:blue;
}

宽高可知可不知(三种)

  1. flex布局
.father{
	display: flex;
	justify-content: center;
	align-items: center
}
  1. 定位(position)+位移(translate)
.father{
	position: relative;
}
.son{
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%,-50%);
}
  1. grid布局(兼容性一般)
.father{
	display: grid;
}
.son{
	align-self: center;
	justify-self: center;
}

宽高已定或已知(三种)

  1. 定位(position)+外边距(margin)
.father{
	position: relative;
}
.son{
	position: absolute;
	top: 50%;
	left: 50%;
    margin-left: -50px;
    margin-top: -50px; 
}
  1. 定位(position)+外边距(margin)
.father{
	position: relative;
}
.son{
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	margin: auto;
}
  1. 定位(position)+calc方式
.father{
	postion: relative;
}
.son{
	top: calc(50% - 50px);
	left: calc(50% - 50px);
}

必须不定宽高

table方式

.father{
	display: table-cell;
	text-align: center;
	vertical-align: middle;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值