CSS 居中那些事

devtools/2024/10/23 14:57:58/
webkit-tap-highlight-color: rgba(0, 0, 0, 0);">

一、父子元素高度确定

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

<style>css">.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>css">.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>css">.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>css">.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>css">.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>css">.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>css">.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>css">.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>css">.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>css">.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>css">.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>css">.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>css">.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.ppmy.cn/devtools/128174.html

相关文章

channelSftp.mkdir() 创建不了文件夹、没有权限

SFTP 不支持创建多级目录&#xff0c;可以循环创建 String[] folders path.split("/"); // 使用绝对路径的根目录&#xff0c;确保每次都从根目录开始创建 String basePath "/"; // 设置你想要的根路径&#xff0c;如 "/home/user"try {chann…

Linux下升级安装ImageMagick

服务器系统为 Centos 1.删除旧版本 /usr/bin/convert 是 ImageMagick 工具集中 convert 命令的可执行文件 #确定已安装版本 convert --version# 对于基于RPM的系统&#xff0c;如Red Hat、CentOS rpm -qf /usr/bin/convert #根据返回的版本信息&#xff0c;卸载&#xff0…

全球云观察:SAP大中华区发展简史(1992-2024)

【全球云观察 &#xff5c; 科技热点关注】 引言 SAP的全称是System Applications and Products&#xff0c;成立于1972年&#xff0c;总部位于德国沃尔多夫市&#xff0c;是一家全球领先的企业应用软件提供商。‌SAP公司最初只有五个人&#xff0c;现已发展成为一家大型跨国企…

引进Menu菜单与新增验证上传图片功能--系统篇

我的迭代小系统要更新2点。一是后台需要引进一种导航&#xff0c;众多导航之中我选择了Menu菜单。二是上传图片接口需要新增验证上传图片环节。先看看更新2点后的效果 引进Menu菜单效果如下&#xff0c;这部分修改后台前端代码 引进Menu菜单后&#xff0c;Menu菜单的默认数据我…

Linux 之 fdisk 【磁盘分区管理】

删除分区 1.查看磁盘信息 lsblk 2.删除分区sdb硬盘下的所有分区 # 1 进入d的磁盘分区 fdisk /dev/sdb # 2 输入p查看磁盘的分区信息 # 3 输入d进入删除磁盘分区命令 # 4 选择要删除的分区号 重复3&#xff0c;4 全部删除 # 5 w 保存退出并生效操作信息 &#xff08;输入q…

常用分布的数学期望、方差、特征函数

文章目录 相关教程相关文献常用分布的数学期望&方差&特征函数定义事件域概率条件概率随机变量分布函数连续随机变量的概率密度函数数学期望离散随机变量连续随机变量 方差与标准差最大似然估计特征函数 不等式Chebyshev&#xff08;切比雪夫&#xff09;不等式 作者&am…

Linux中vim的三种主要模式和具体用法

Vim编辑器的三种主要模式 Vim编辑器具有三种主要模式&#xff0c;它们是&#xff1a; 1.命令模式&#xff08;Normal mode&#xff09;&#xff1a;这是Vim的默认模式&#xff0c;用于执行编辑命令、移动光标、删除文本、复制粘贴等操作。在这个模式下&#xff0c;按下键盘上的…

WebSocked基础

一. WebSocket 基本概念 WebSocket是什么&#xff1f; WebSocket 是基于 TCP 的一种新的应用层网络协议。它提供了一个全双工的通道&#xff0c;允许服务器和客户端之间实时双向通信。因此&#xff0c;在 WebSocket 中&#xff0c;浏览器和服务器只需要完成一次握手&#xff…