8种css居中实现的详细实现方式了

article/2025/9/20 7:25:38

css

这是一篇关于居中对齐方式的总结

开篇之前,先问一下大家都知道几种居中的实现方式?

面试时答出来两三个就不错了,就怕面试官还让你继续说。今天就来总结一下这些居中的方式

  1. 使用flex布局设置居中。
  2. 使用flex 时也能通过给子项设置margin: auto实现居中。
  3. 使用绝对定位的方式实现水平垂直居中。
  4. 使用grid设置居中。
  5. 使用grid时还能通过给子项设置margin: auto实现居中。
  6. 使用tabel-cell实现垂直居中。
  7. 还有一种不常用的方法实现垂直居中。
  8. 最后还有一种奇葩的方法。容器设置position: relative。孩子设置 top、left、bottom、right都设置为0

1.flex布局设置居中

常见的一种方式就是使用flex布局设置居中。

利用弹性布局(flex),实现水平居中,其中justify-content 用于设置弹性盒子元素在主轴(横轴)方向上的对齐方式

给容器设置:

  • display: flex;写在父元素上这就是定义了一个伸缩容器

  • justify-content 主轴对齐方式,默认是横轴

  • align-items 纵轴对齐方式,默认是纵轴

优点: 简单、方便、快速,三行代码搞定。

<div class="box"><div class="child">水平垂直居中</div>
</div><style>
.box {width: 200px;height: 200px;border: 1px solid;display: flex;align-items: center; // 纵轴对齐方式,默认是纵轴 子元素垂直居中justify-content: center; //纵轴对齐方式,默认是纵轴
}
.child {background: red;
}  
</style>

代码片段

2.flex-给子项设置

第一种方式是给父盒子设置属性,这一种是给子盒子设置margin: auto实现居中。给容器设置 display: flex; 子项设置 margin: auto;

<div class="box"><div class="child">水平垂直居中</div>
</div><style>
.box {width: 200px;height: 200px;border: 1px solid;display: flex;
}
.child {background: red;margin: auto; // 水平垂直居中
}  
</style>

代码片段

3.绝对定位

使用绝对定位的方式实现水平垂直居中。容器设置position: relative。子元素设置 position: absolute; left: 50%; top: 50%; transfrom: translate(-50%, -50%);

优点就是不用关心子元素的长和宽,但是这种方法兼容性依赖translate2d的兼容性

<div class="box"><div class="child">水平垂直居中</div>
</div><style>
.box {width: 200px;height: 200px;border: 1px solid;position: relative;
}
.child {position: absolute;left: 50%;top: 50%;transform: translate(-50%, -50%);background: red;
}  
</style>

代码片段

4.tabel-cell实现垂直居中

css新增的table属性,可以让我们把普通元素,变为table元素的现实效果,通过这个特性也可以实现水平垂直居中

而且tabel单元格中的内容天然就是垂直居中的,只要添加一个水平居中属性就好了

  • 使用tabel-cell实现垂直居中,容器设置 display: table-cell;;

  • vertical-align: middle属性设置元素的垂直对齐方式

  • 子元素如果是块级元素,直接使用左右margin:auto实现水平居中。如果是行内元素,给容器设置text-align: center

利用 text-align: center 可以实现在块级元素内部的内联元素水平居中。此方法对内联元素inline, 内联块inline-block, 内联表inline-table, inline-flex元素水平居中都有效。

<div class="box"><div class="child">水平垂直居中</div>
</div><style>
.box {width: 200px;height: 200px;border: 1px solid;display: table-cell;vertical-align: middle;  // 设置元素在垂直方向上的对齐方式text-align: center;
}
.child {background: red;display: inline-block;
}  
</style>

代码片段

5.grid设置居中

  1. 使用grid设置居中。给容器设置 display: grid; align-items: center; justify-content: center;

通过给容器设置属性,达到居中效果,但是这种方式兼容性较差,不推荐。

<div class="box"><div class="child">水平垂直居中</div>
</div><style>
.box {width: 200px;height: 200px;border: 1px solid;display: grid;align-items: center;justify-content: center;
}
.child {background: red;
}  
</style>

代码片段

6.grid给子项设置

使用grid时还能通过给子项设置margin: auto;实现居中。给容器设置 display: grid; 子项设置 margin: auto;

某些浏览器会不支持grid布局方式,兼容性较差,不推荐。

<div class="box"><div class="child">水平垂直居中</div>
</div><style>
.box {width: 200px;height: 200px;border: 1px solid;display: grid;
}
.child {background: red;margin: auto;
}  
</style>

代码片段

7.给容器加给伪元素

这是一种不常用的方法实现垂直居中。给容器加给伪元素,设置line-height等于容器的高度。给孩子设置display: inline-block;

<div class="box"><div class="child">水平垂直居中</div>
</div><style>
.box {width: 200px;height: 200px;border: 1px solid;text-align: center;
}
.box::after {content: "";line-height: 200px;
}
.child {display: inline-block;background: red;
}</style>

代码片段

8.还有一种奇葩的方法

这个奇葩方式和第三种使用绝对定位相似,只不过需要给子元素设置 position: absolute; 设置固定宽度和高度;top、left、bottom、right都设置为0; margin设置为auto;也能实现垂直水平居中。

<div class="box"><div class="child">水平垂直居中</div>
</div><style>
.box {width: 200px;height: 200px;border: 1px solid;position: relative;
}
.child {background: red;width: 100px;height: 40px;position: absolute;left: 0;top: 0;right: 0;bottom: 0;margin: auto;
}  
</style>

代码片段

以上就是一些我们常用的垂直居中的方案。


http://chatgpt.dhexx.cn/article/9FrySy31.shtml

相关文章

各种居中方法汇总(究极版)

本文部分参考文章&#xff1a;https://github.com/ljianshu/Blog/issues/29 每一部分后另附相关实践代码 前言 本文主要介绍水平居中&#xff0c;垂直居中&#xff0c;还有水平垂直居中各种办法&#xff0c;思维导图如下&#xff1a; 一、水平居中 1.行内元素水平居中 利用…

css居中大全(文字居中、块居中、水平居中、垂直居中)

css居中 一、水平居中 1.块本身水平居中 div{width: 100px;height: 100px;border: 1px solid #000;margin:auto;} <div>我本身水平居中 </div>div的margin不设值怎么表示&#xff1a;不设值&#xff0c;也是auto /*给块居中 上20&#xff0c;左右居中&#xff…

Skipped breakpoint at because it happened inside debugger evaluation

问题描述&#xff1a; 在多线程项目中&#xff0c;在idea中打断点时&#xff0c;有时会遇到下面这种情况&#xff1a; idea左下角出现一行红底或者绿底文字提示&#xff1a; Skipped breakpoint at because it happened inside debugger evaluation 然后我们能感受到的就是…

IDEA Debug出现:Skipped breakpoint at because it happened inside debugger evaluation

在使用IDEA debug功能时&#xff0c;没有出现断点处程序变量快照&#xff0c;而是进入了如下debug界面&#xff0c;并打开了URL类加载程序&#xff1a; 网上有一些方法&#xff0c;但其实都没用&#xff0c;或者说没有从根本上解决这个问题&#xff0c;下面给出我的方法。 ok&a…

ssh上传公钥报 All keys were skipped because they already exist on the remote system.

项目需要用Ansible进行多台服务器部署&#xff0c;服务器申请下来给app用户赋了权限后root用户就被上收了。 在做ssh免密登陆时报错&#xff1a; sudo ssh-copy-id -i /root/.ssh/id_rsa.pub app22.10.206.140 试了很多方法都没用&#xff0c;结果在140服务器新建/root/.ssh这…

创建vue项目的时候报错:Skipped git commit due to missing username and email in git config.

创建vue项目的时候报错&#xff1a; WARN Skipped git commit due to missing username and email in git config, or failed to sign commit. You will need to perform the initial commit yourself. 原因&#xff1a; git 进行初始化提交 没有绑定 对应的 git用户名和邮箱 …

Skipped breakpoint because it happened inside debugger evaluation

在debug项目时总是会莫名其妙的多出system.out的数据&#xff0c;莫名其妙&#xff0c;搜到这篇文章&#xff0c;帮助很大&#xff0c;转载一下 解决Skipped breakpoint at %code reference% because it happened inside debugger evaluation的通用方法。 先尝试去掉勾选 Ena…

Elasticsearch的search之_shards skipped之谜

es从 v5.6 开始引入了 pre-filter 机制&#xff08;skipped&#xff09;&#xff1a;对于 Date 类型的 Range 查询&#xff0c;在对分片执行搜索之前&#xff0c;先检查一下分片是否包括被查询的数据范围&#xff0c;如果查询的范围与分片持有的数据没有交集&#xff0c;就跳过…

Cadence常见问题:Analysis was skipped due to inability to compute operating point?

题主使用门电路创建sr触发器电路图如下&#xff1a; 欲在sr端加电压pulse验证sr输出特性&#xff0c;遇到以下报错&#xff1a; 显示输出不收敛 解决方法&#xff1a; 题主是直接调用ahdlLib中的或非门&#xff0c;是理想或非门&#xff0c;所以造成不收敛的问题 用晶体管自己搭…

linux 使用rz命令上传文件失败skipped by remote

一、背景 linux 通过rz命令上传 文件&#xff0c;显示xx skipped by remote 点击任意键后&#xff0c;文件并没有上传成功 二、解决方案 应该是权限问题&#xff0c;我当前并不是超级权限&#xff0c; 先切换超级权限 sudo su - 然后再上传 文件就可以了

SparkUI中显示stage skipped的原因【源码分析】

SparkUI中显示stage skipped的原因【源码分析】 Spark Job的ResultStage的最后一个Task成功执行之后&#xff0c;DAGScheduler.handleTaskCompletion方法会发送SparkListenerJobEnd事件&#xff0c;源码如下&#xff1a;JobProgressListener.onJobEnd方法负责处理SparkListener…

unprintable character xxxx skipped解决方案

在keil软件中输入源代码的时候 常常会出现上述错误 双击蓝色那一条文字 -----到34行处 可以发现注释中间的分号是在中文状态下 而在这里面我们只能用英文状态下的分号 所以将分号改为英文状态下的分号即可 若实在不行 就直接删除注释~~~

spark web ui中的skipped的含义

顾名思义&#xff0c;跳出的意思啦。 例如如图&#xff1a; skipped的stages代表是已经执行过了。所以不需要再执行了。 如何&#xff0c;你有一个 testRdd。然后先做 testRdd.Filter("xxx").map("xx")&#xff0c; 这个是transform 然后再分别做了count和…

svn的skipped,no versioned parent报错解决方法

http://blog.csdn.net/lanjinghai507/article/details/52327636 感谢分享 今天一大早&#xff0c;习惯性的用sublime_text,选择文件夹&#xff0c;然后删除该文件&#xff0c;再然后跟新&#xff0c;接着就出现skipped,no versioned parent的报错&#xff0c;如下图 skipped…

ntohs和htons的区别

虽然注册CSDN已经有好几年了&#xff0c;学习写程序也有两年的时间了。对于绝大数人来说&#xff0c;我还是得菜鸟。平时遇到什么问题也会来到这个平台搜索别人的解答。也从这个平台上获益很多。 今天在公司因为一个问题和同事就关于htons与ntohs有没有区别探讨了一下&#xff…

socket编程中的 htons()

文章目录 1.内存存储数据的方式1.1 数据字节序号1.2 小端字节序1.3 大端字节序 2. 转换顺序2.1 网络字节序2.2 主机字节序2.3 转换函数 在刚刚接触 socket 时&#xff0c;遇到了 htons() 函数&#xff0c;就直接懵逼了&#xff0c;这是什么东西&#xff0c;有什么用&#xff1f…

高并发解决方案——Redis(一)

简介 Redis作为重要的缓存数据库在高并发的解决方案中起着重要作用。为了系统的学习Redis&#xff0c;也为了秋招&#xff08;美团比较关注Redis 的掌握&#xff09;&#xff0c;计划编写该系列博客&#xff0c;也是为了整理知识点。 本篇主要介绍了Redis的基础知识与原理。之…

Jmeter超高并发解决方案

背景一亿用户量&#xff0c;平均每人每天10次的业务量&#xff0c;要求并发数在5000以上&#xff0c;峰值在5w到10w之间&#xff0c;QPS在25w以上 一、jmeter解决高并发的优化方案 1.1 优化监听&#xff08;GUI模式&#xff0c;尽量不考虑&#xff09; …

2023春招面试专题:高并发解决方案

如何理解高并发&#xff1f; 高并发意味着大流量&#xff0c;需要运用技术手段抵抗流量的冲击&#xff0c;这些手段好比操作流量&#xff0c;能让流量更平稳地被系统所处理&#xff0c;带给用户更好的体验。 我们常见的高并发场景有&#xff1a;淘宝的双11、春运时的抢票、微…

高并发解决方案之熔断处理

高并发解决方案之熔断处理 前言概念基本介绍三种状态熔断方式常用框架功能对比使用介绍 参考链接 前言 问题列表 跨系统、跨服务调用第三方接口时&#xff0c;第三方接口响应超时或者服务不可用&#xff0c;发生连锁故障进而导致雪崩效应。 举例说明 假设上游服务是A&#xff…