元素水平垂直居中的方法有哪些?如果元素不定宽高呢?

本文详细介绍了在开发中如何通过定位、margin、transform、table布局、flex布局和grid布局等方法实现元素的水平垂直居中,区分了针对内联和块级元素的不同策略。
摘要由CSDN通过智能技术生成
在开发中经常遇到这个问题,即让某个元素的内容在水平和垂直方向上都居中,内容不仅限于文字,可 能是图片或其他元素
居中是⼀个非常基础但又是非常重要的应用场景,实现居中的方法存在很多,可以将这些方法分成两个大类:
居中元素(子元素)的宽高已知
居中元素宽高未知

实现方式

  • 利⽤定位+margin:auto
  • 利⽤定位+margin:负值
  • 利⽤定位+transform
  • table布局
  • flex布局
  • grid布局

1. 利⽤定位+margin:auto  

<style>
 .father{
 width:500px;
 height:300px;
 border:1px solid #0a3b98;
 position: relative;
 }
 .son{
 width:100px;
 height:40px;
 background: #f0a238;
 position: absolute;
 top:0;
 left:0;
 right:0;
 bottom:0;
 margin:auto;
 }
</style>
<div class="father">
 <div class="son"></div>
</div>

父级设置为相对定位,子级绝对定位 ,并且四个定位属性的值都设置了0,那么这时候如果子级没有设置宽高,则会被拉开到和父级⼀样宽高

这里子元素设置了宽高,所以宽高会按照我们的设置来显示,但是实际上子级的虚拟占位已经撑满了整个父级,这时候再给它⼀个 margin:auto 它就可以上下左右都居中了

2. 利⽤定位+margin:负值

绝大多数情况下,设置父元素为相对定位, 子元素移动自身50%实现水平垂直居中

<style>
 .father {
 position: relative;
 width: 200px;
 height: 200px;
 background: skyblue;
 }
 .son {
 position: absolute;
 top: 50%;
 left: 50%;
 margin-left:-50px;
 margin-top:-50px;
 width: 100px;
 height: 100px;
 background: red;
 }
</style>
<div class="father">
 <div class="son"></div>
</div>
这种方案不要求父元素的高度,也就是即使父元素的高度变化了,仍然可以保持在父元素的垂直居中位置,水平方向上是⼀样的操作
但是该方案需要知道子元素自身的宽高,但是我们可以通过下面  transform 属性进行移动

3. 利用定位+transform

<style>
 .father {
 position: relative;
 width: 200px;
 height: 200px;
 background: skyblue;
 }
 .son {
 position: absolute;
 top: 50%;
 left: 50%;
 transform: translate(-50%,-50%);
 width: 100px;
 height: 100px;
 background: red;
 }
</style>
<div class="father">
 <div class="son"></div>
</div>
translate(-50%, -50%) 将会将元素位移自己宽度和高度的-50%这种方法其实和最上面被否定掉的margin负值用法⼀样,可以说是 margin 负值的替代方案,并不需要知道自身元素的宽高

4. table布局

设置父元素为 display:table-cell ,子元素设置 display: inline-block 。利用  vertical 和 text-align 可以让所有的行内块级元素水平垂直居中
<style>
 .father {
 display: table-cell;
 width: 200px;
 height: 200px;
 background: skyblue;
 vertical-align: middle;
 text-align: center;
 }
 .son {
 display: inline-block;
 width: 100px;
 height: 100px;
 background: red;
 }
</style>
<div class="father">
 <div class="son"></div>
</div>

5. flex弹性布局

<style>
 .father {
 display: flex;
 justify-content: center;
 align-items: center;
 width: 200px;
 height: 200px;
 background: skyblue;
 }
 .son {
 width: 100px;
 height: 100px;
 background: red;
 }
</style>
<div class="father">
 <div class="son"></div>
</div>
css3 中的  flex 布局,可以非常简单实现垂直水平居中,这里以简单看看 flex 布局的关键属性作用:
  • display: flex时,表示该容器内部的元素将按照flex进行布局
  • align-items: center表示这些元素将相对于本容器水平居中
  • justify-content: center也是同样的道理垂直居中

6. grid⽹格布局

<style>
 .father {
 display: grid;
 align-items:center;
 justify-content: center;
 width: 200px;
 height: 200px;
 background: skyblue;
 }
 .son {
 width: 10px;
 height: 10px;
 border: 1px solid red
 }
</style>
<div class="father">
 <div class="son"></div>
</div>
这里看到, gird 网 格布局和 flex 弹性布局都简单粗暴

小结

上述方法中,不知道元素宽高大小仍能实现水平垂直居中的方法有:

  • 利用定位+margin:auto
  • 利用定位+transform
  • flex布局
  • grid布局

根据元素标签的性质,可以分为:

  • 内联元素居中布局
  • 块级元素居中布局
1.内联元素居中布局

水平居中

  • 行内元素可设置:text-align: center
  •  flex布局设置父素:display: flex; justify-content: center
垂直居中
单行文本父元素确认高度:height === line-height
多行文本父元素确认高度:display: table-cell; vertical-align: middle
2. 块级元素居中布局
水平居中
  • 定宽: margin: 0 auto
  • 绝对定位+left:50%+margin:负自身⼀半

垂直居中

  • position: absolute设置left、top、margin-left、margin-top(定⾼)
  • display: table-cell
  • transform: translate(x, y)
  • flex(不定高,不定宽)
  • grid(不定高,不定宽),兼容性相对⽐较差
  • 32
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

格子衫大背头

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

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

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

打赏作者

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

抵扣说明:

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

余额充值