CSS常用水平垂直居中的几种方法

为方便理解,欢迎查看线上效果,在线试一试

一、利用margin:auto

元素有宽度和高度时,利用margin:auto设置元素水平垂直居中:
居中

HTML代码如下:

<div class="div1">
	<div class="center"></div>
</div>

CSS代码如下:

.div1 {
  background-color: #eee;
  width: 200px;
  height: 200px;
  position: relative;
}
.div1 .center {
  width: 50px;
  height: 50px;
  background-color: forestgreen;
  position: absolute;
  margin: auto;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

二、利用position: absolute

HTML代码:

<div class="div2">
	<div class="center"></div>
</div>

已知元素宽度和高度时,可以设置position: absolutemargin为负的宽高的一半,CSS代码如下:

.div2 {
  background-color: #eee;
  width: 200px;
  height: 200px;
  position: relative;
  margin-top: 20px;
}
.div2 .center {
  width: 50px;
  height: 50px;
  background-color: rgb(34, 71, 139);
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -25px;
  margin-top: -25px;
}

已知元素宽度和高度时,也可以利用calc计算属性设置topleft,CSS代码如下:

.center {
  width: 50px;
  height: 50px;
  background-color: rgb(34, 71, 139);
  position: absolute;
  left: calc(50% - 25px);
  top: calc(50% - 25px);
}

元素宽度和高度未知时,可以设置position: absolutetransform: translate(-50%, -50%),CSS代码如下:

.center {
  width: 50px;
  height: 50px;
  background-color: rgb(34, 71, 139);
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

实现效果:

居中

三、弹性盒子

通过为其父元素设置display: flex来实现居中。
HTML代码如下:

<div class="div4">
	<div class="center"></div>
</div>

CSS代码如下:

.div4 {
  background-color: #eee;
  width: 200px;
  height: 200px;
  position: relative;
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.div4 .center {
  width: 50px;
  height: 50px;
  background-color: rgb(240, 248, 166);
}

实现效果:
居中

四、利用水平对齐和行高

设置text-alignline-height 实现单行文本水平垂直居中。
HTML代码如下:

<div class="div5">
	<p class="center">我要居中</p>
</div>

CSS代码如下:

.div5 {
  background-color: #eee;
  width: 200px;
  height: 200px;
  margin-top: 20px;
}
.div5 .center {
  text-align: center;
  line-height: 200px;
}

实现效果:
在这里插入图片描述

五、grid

HTML代码如下:

<div class="div6">
	<p class="center">我要居中</p>
</div>

在网格项目中设置justify-selfalign-self或者margin: auto,CSS代码如下:

.div6 {
  background-color: #eee;
  width: 200px;
  height: 200px;
  margin-top: 20px;
  display: grid;
}
.div6 .center {
  align-self: center;
  justify-self: center;
  /* margin: auto; */
}

在网格容器上设置justify-itemsalign-itemsjustify-contentalign-content,CSS代码如下:

.div6 {
  background-color: #eee;
  width: 200px;
  height: 200px;
  margin-top: 20px;
  display: grid;
  align-items: center;
  justify-items: center;
  /* align-content: center;
  justify-content: center; */
}
  • 15
    点赞
  • 114
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白之旅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值