当前位置: 首页 > news >正文

css盒子水平垂直居中

目录

 1采用flex弹性布局:

2子绝父相+margin:负值:

3.子绝父相+margin:auto:

4子绝父相+transform:

5通过伪元素

6table布局

7grid弹性布局


文字 水平垂直居中链接:文字水平垂直居中-CSDN博客

以下为盒子水平垂直居中

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

 1采用flex弹性布局:

在父元素设置display: flex表示该容器内部的元素将按照flex进行布局,再设置align-items: center表示这些元素将相对于本容器水平居中,justify-content: center实现垂直居中。

.father{width: 400px;height: 300px;background-color: rebeccapurple;display: flex;justify-content: center;align-items: center;
}
.son{width: 200px;height: 100px;background-color: aqua;
}
效果

2子绝父相+margin:负值:

设置left和top为50%,此时位置会偏右自身元素的宽高,再设margin-left和margin-top为自身元素宽高的负一半,实现水平垂直居中。


.father {width: 400px;height: 300px;background-color: rebeccapurple;position: relative;
}
.son {width: 200px;height: 100px;background-color: palegoldenrod;position: absolute;top: 50%;left: 50%;//宽度的一半margin-left: -100px;//高度的一半margin-top: -50px;
}
效果:

3.子绝父相+margin:auto:

,设置top、left、right、bottom为0,在设置margin:auto

.father{width:400px;height:300px;background-color: rebeccapurple;position: relative;   //父级设置为相对定位}.son{width:100px;height:40px;background: red;position: absolute;   //设置为子级绝对定位top:0;left:0;right:0;bottom:0;margin:auto;}
效果

4子绝父相+transform:

设置left和top为50%,此时位置会偏右自身元素的宽高,再设transform: translateX(-50px) translateY(-50px);

.father{width: 400px;height: 300px;background-color: rebeccapurple;position: relative;
}
.son{width: 200px;height: 100px;background-color: skyblue;position: absolute;left: 50%;top: 50%;transform: translateX(-100px) translateY(-50px);
}
效果

5通过伪元素

.father{width: 400px;height: 300px;background-color: rebeccapurple;text-align: center;
}
.father::before{content : '';display: inline-block;vertical-align: middle;height: 100%;
}
.son{width: 200px;height: 100px;background-color: pink;vertical-align: middle;margin: 0 auto;display: inline-block;
}
效果

6table布局

设置父元素为display:table-cell,子元素设置 display: inline-block。利用verticaltext-align可以让所有的行内块级元素水平垂直居中。

.father {display: table-cell;width: 400px;height: 300px;background: rebeccapurple;vertical-align: middle;text-align: center;
}
.son {display: inline-block;width: 200px;height: 100px;background: forestgreen;
}
效果

7grid弹性布局

在父元素设置display: drid表示该容器内部的元素将按照flex进行布局,再设置align-items: center表示这些元素将相对于本容器水平居中,justify-content: center实现垂直居中。

.father{width: 400px;height: 300px;background-color: rebeccapurple;display: grid;justify-content: center;align-items: center;
}
.son{width: 200px;height: 100px;background-color: greenyellow;
}
效果

http://www.lryc.cn/news/521856.html

相关文章:

  • px、em 和 rem 的区别:深入理解 CSS 中的单位
  • 基于STM32设计的粮食仓库(粮仓)环境监测系统
  • 【后端面试总结】tls中.crt和.key的关系
  • 日拱一卒(20)——leetcode学习记录:大小为 K 且平均值大于等于阈值的子数组数目
  • 项目练习:若依管理系统字典功能-Vue前端部分
  • apache-skywalking-apm-10.1.0使用
  • 计算机视觉算法实战——视频分析(Video Analysis)
  • 全网首发:编译libssh,产生类似undefined reference to `EVP_aes_256_ctr@OPENSSL_1_1_0‘的大量错误
  • 用python实战excel和word自动化
  • 【云计算】OpenStack云计算平台
  • 好用的php商城源码有哪些?
  • docker安装Nginx UI
  • 为深度学习创建PyTorch张量 - 最佳选项
  • 详解数据增强中的平移shft操作
  • CCLINKIE转ModbusTCP网关,助机器人“掀起”工业智能的“惊涛骇浪”
  • 类型安全与代码复用的C# 泛型
  • 卷积神经05-GAN对抗神经网络
  • vscode使用Marscode编程助手
  • 网络分析仪测试S参数
  • docker mysql5.7如何设置不区分大小写
  • 【1】Word:邀请函
  • 【gin】中间件使用之jwt身份认证和Cors跨域,go案例
  • 【JAVA实战】@FeignClient注解类通用请求封装
  • [c语言日寄]精英怪:三子棋(tic-tac-toe)3命慢通[附免费源码]
  • GORM(Go语言数据交互库)
  • Redis主从同步是怎么实现的?
  • Flutter中Get.snackbar避免重复显示的实现
  • [Qt]常用控件介绍-多元素控件-QListWidget、QTableWidget、QQTreeWidget
  • 深入Android架构(从线程到AIDL)_32 JNI架构原理_Java与C的对接05
  • 【gRPC】clientPool 客户端连接池简单实现与go案例