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

CSS 居中那些事

一、父子元素高度确定

简单粗暴, 直接通过设置合适的 paddingmargin 实现居中

<style>.p {padding: 20px 0;background: rgba(255, 0, 0, 0.1);}.c {width: 40px;height: 20px;background: blue;}
</style>
<div class="p"><div class="c"></div>
</div>

二、Flex(弹性) 布局

<style>.p {height: 100px;background: rgba(255, 0, 0, 0.1);display: flex;align-items: center; /* 垂直居中 */justify-content: center; /* 水平居中 */}.c {width: 40px;height: 20px;background: blue;}
</style>
<div class="p"><div class="c"></div>
</div>

三、Grid(网格) 布局

<style>.p {height: 100px;background: rgba(255, 0, 0, 0.1);display: grid;place-items: center; /* 直接水平垂直居中 */}.c {width: 40px;height: 20px;background: blue;}
</style>
<div class="p"><div class="c"></div>
</div>

四、子元素通过「绝对定位 + 偏移」实现居中

4.1 子元素高度不固定, 使用 transform 进行偏移

依据: 绝对定位百分比, 是相对于父元素进行计算、transform 百分比, 则是相对于自身进行计算

<style>.p {height: 100px;background: rgba(255, 0, 0, 0.1);position: relative;}.c {width: 40px;background: blue;top: 50%;position: absolute;transform: translateY(-50%);}
</style>
<div class="p"><div class="c">1111<br>2222</div>
</div>

4.2 子元素高度固定, 直接使用 margin 进行偏移

依据: 绝对定位百分比, 是相对于父元素进行计算

<style>.p {height: 100px;background: rgba(255, 0, 0, 0.1);position: relative;}.c {width: 40px;height: 40px;background: blue;position: absolute;top: 50%; /* 该百分比, 相对于父元素高度进行计算 */margin-top: -20px; /* 手动计算, 等于高度的一半 */}
</style>
<div class="p"><div class="c"></div>
</div>

4.3 「补充」marginpadding 百分比计算方式

marginpadding 上下两个方向的百分比, 相对于父元素自适应的一边进行计算, 默认情况下是根据父元素宽度进行计算的 (因为块元素默认宽度自适应), 之所以要相对于自适应一边进行计算, 是为了避免在未设置宽高情况下, 子元素设置了边距, 引起容器尺寸的变化, 从而造成百分比重新计算, 引起死循环

<style>.p {width: 200px;height: 400px;background: rgba(255, 0, 0, 0.1);}.c {margin: 10%;  /* 上下左右边距都, 相对于父元素宽度: 20px */padding: 10%; /* 上下左右边距都, 相对于父元素宽度: 20px */display: inline-block;background: blue;}
</style>
<div class="p"><div class="c"></div>
</div>

通过 writing-mode: vertical-lr 可元素内容 (文字、子元素) 从上到下垂直流动, 元素将从宽度自适应改为高度自适应, 这时其子元素 paddin margin 百分比将相对于父元素的高度进行计算

<style>.p {writing-mode: vertical-lr; /* 元素内容从上到下垂直流动、高度将自适应*/width: 200px;height: 400px;background: rgba(255, 0, 0, 0.1);}.c {margin: 10%;  /* 上下左右边距都, 相对于父元素宽度: 40px */padding: 10%; /* 上下左右边距都, 相对于父元素宽度: 40px */display: inline-block;background: blue;}
</style>
<div class="p"><div class="c"></div>
</div>

五、自适应特性 + margin: auto

众所周知 margin: auto, 可以很容器实现元素的水平居中, 而之所以能够实现水平居中, 是因为父元素宽度自适应, 只有在自适应情况下 margin: auto 才能正确计算出合适的值

<style>.p {width: 200px;height: 200px;background: rgba(255, 0, 0, 0.1);}.c {width: 40px;height: 40px;background: blue;margin: auto;}
</style>
<div class="p"><div class="c"></div>
</div>

从上面例子可以看出, margin: auto 之所以能实现水平居中, 就是因为父元素宽度自适应了, 那么很自然就可以想到, 如果可以使父元素高度自适应, 那么我们就可以借用 margin: auto 实现元素的垂直居中

5.1 通过 writing-mode 使元素高度自适应

writing-mode 属性定义了元素内容(文本、子元素)水平或垂直的排列方式, 其中 vertical-lr 属性值可使内容由水平流动改为垂直流动, 同时元素的宽度自适应也将变为高度自适应

<style>.p {width: 200px;height: 200px;background: rgba(255, 0, 0, 0.1);writing-mode: vertical-lr; /* 将子元素流向从水平改为垂直, 同时宽度自适应也将变为高度自适应 */}.c {width: 40px;height: 40px;background: blue;margin: auto;}
</style>
<div class="p"><div class="c"></div>
</div>

5.2 为子元素设置绝对定位, 使得在对应方向上具有自适应特性

<style>.p {width: 200px;height: 200px;background: rgba(255, 0, 0, 0.1);position: relative;}.c {width: 40px; /* 需要设置宽度 */height: 40px; /* 需要设置高度 */background: blue;top: 0;left: 0;right: 0;bottom: 0;position: absolute;margin: auto;}
</style>
<div class="p"><div class="c"></div>
</div>

六、子元素为文本或内联元素(包括内联块元素)

6.1 line-height 一把梭哈

对于父元素高度确定, 且子元素是单行文本或者内联元素, 可直接通过 line-height 实现居中

<style>.p {line-height: 80px;background: rgba(255, 0, 0, 0.1);}.inline-block {display: inline-block;height: 20px;width: 40px;background: blue;}
</style>
<div class="p">11111111111111111111
</div>
<br/>
<div class="p"><div class="inline-block"></div>
</div>

6.2 伪元素(::after) + vertical-align

通过伪类创建一个隐藏的内联元素, 高度为父元素高度, 并借用 vertical-align 使所有内容垂直居中

<style>.p {width: 200px;height: 200px;background: rgba(255, 0, 0, 0.1);}.p::after {content: '';height: 100%;display: inline-block;vertical-align: middle;}.c {width: 40px; /* 需要设置宽度 */height: 40px; /* 需要设置高度 */background: blue;display: inline-block;}
</style>
<div class="p"><div class="c"></div>
</div>
<br>
<div class="p">111111111111111111111
</div>

6.3 display: table-cell + vertical-align

<style>.p {width: 200px;height: 200px;background: rgba(255, 0, 0, 0.1);display: table-cell;vertical-align: middle;}.c {width: 40px; /* 需要设置宽度 */height: 40px; /* 需要设置高度 */background: blue;}
</style>
<div class="p"><div class="c"></div>
</div>
<br>
<div class="p">111111111111111111111
</div>

image

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

相关文章:

  • Java项目-基于springboot框架的智能热度分析和自媒体推送平台项目实战(附源码+文档)
  • 跨平台进程池背后的思想
  • 前端性能优化之加载篇
  • 数据结构(栈)
  • Aspose.PDF功能演示:使用 JavaScript 从 PDF 中提取文本
  • 计算机系统简介
  • 学习文档10/18
  • Redis入门到精通(二):入门Redis看这一篇就够了
  • 荒岛逃生游戏
  • 玫瑰花HTML源码
  • 【wpf】07 后端验证及令牌码获取步骤
  • 学习中,师傅b站泷羽sec——xss挖掘过程
  • 什么是双因素身份验证?双因素身份验证的凭据类型有哪些?
  • 【MR开发】在Pico设备上接入MRTK3(一)——在Unity工程中导入MRTK3依赖
  • 利用移动式三维扫描技术创建考古文物的彩色纹理网格【上海沪敖3D】
  • Spring AI Java程序员的AI之Spring AI(四)
  • 精选20个爆火的Python实战项目(含源码),直接拿走不谢!
  • Rocky Linux 9安装Asterisk 20和freepbx 17脚本——筑梦之路
  • PSPICE FOR TI笔记记录1
  • Java集合剖析4】LinkedList
  • 基于MATLAB/octave的容积卡尔曼滤波(CKF)【带逐行注释】
  • Python编程探索:从基础语法到循环结构实践(下)
  • 简介openwrt系统下/etc/config/network文件生成过程
  • javaWeb项目-Springboot+vue-XX图书馆管理系统功能介绍
  • 华为ENSP用户权限深度解析:构建安全高效的网络管理
  • NFC之NDEF
  • 学习第三十六行
  • 停车场问题
  • 海康相 机
  • 用map实现el-table全选