目录

前言

居中元素宽高已知

1、absolute + margin auto

2、absolute + 负 margin

3、absolute + calc

居中元素宽高未知

1、absolute + transform

2、line-height + vertical-align

3、table 表格元素(不推荐)

4、css-table(display:table-cell)

5、flex 布局(推荐)

6、flex + margin auto

7、grid 网格布局(一)

8、grid 网格布局(二)

场景推荐

    • *

前言

无论是实际开发中,还是求职面试中,css 垂直居中往往都是一个绕不开的话题,其中有许多面试者在遭受多次打击之后,却没有一个很好的反击点,刚好结合自己的经历与痛处,来给大家一个锤爆面试官大佬们的机会。

垂直居中主要分为了两种类型:居中元素宽高已知居中元素宽高未知,那么我们就结合这两种类型来一一举例。

居中元素宽高已知

1、absolute + margin auto

注意:父元素与当前元素的宽高已知

.parent{  position: relative;  width: 500px;  height: 500px;  border: 1px solid blue;} .child{  background: green;  width: 200px;   height: 200px;  /* 核心代码 */  position:absolute;  top: 0;   bottom: 0;   left: 0;   right: 0;  margin: auto;}

2、absolute + 负 margin

注意:负 margin 是基于自身的高度和宽度来进行位移的(设置为自身的 -1/2)

.parent{  position:relative;  width: 500px;  height: 500px;  border: 1px solid blue;} .child{  background: green;  width: 200px;   height: 200px;  /* 核心代码 */  position:absolute;  top: 50%;   left: 50%;  margin-top: -100px;  margin-left: -100px;}

3、absolute + calc

注意:使用 CSS3 的一个计算函数来进行计算(相当于负 margin 的简化版)

.parent{  position:relative;  width: 500px;  height: 500px;  border: 1px solid blue;} .child{  background: green;  width: 200px;   height: 200px;  /* 核心代码 */  position:absolute;  top: calc(50% - 100px);  left: calc(50% - 100px);}

居中元素宽高未知

1、absolute + transform

注意:transform 的 translate 属性值如果是一个百分比,那么这个百分比是基于自身的宽高进行计算

.parent{  position: relative;  width: 500px;  height: 500px;  border: 1px solid blue;} .child{  background: green;  /* 核心代码 */  position: absolute;  top: 50%;  left: 50%;  transform: translate(-50%, -50%);}

2、line-height + vertical-align

把当前元素设置为行内元素,然后通过设置父元素的 text-align: center来实现水平居中;同时通过设置当前元素的 vertical-align: middle来实现垂直居中;最后设置当前元素的 line-height: initial来继承父元素的line-height

.parent{  width: 500px;  border: 1px solid blue;  /* 核心代码 */  line-height: 500px;  text-align: center;} .child{  background: green;  /* 核心代码 */  display: inline-block;  vertical-align: middle;  line-height: initial;}

3、table 表格元素(不推荐)

通过经典的table来进行布局(不推荐)

<div class="child"></div>
<style> .parent{ width: 500px; height: 500px; border: 1px solid blue; / 核心代码 / text-align: center; } .child{ background: green; / 核心代码 / display: inline-block; }</style> ### 4、css-table(display:table-cell) 不写 table 元素,也可以使用 table 的特性,需使用 css-table(display:table-cell) .parent{ width: 500px; height: 500px; border: 1px solid blue; / 核心代码 / display: table-cell; text-align: center; vertical-align: middle;} .child{ background: green; / 核心代码 / display: inline-block;} ### 5、flex 布局(推荐) .parent{ width: 500px; height: 500px; border: 1px solid blue; / 核心代码 / display: flex; / 水平居中 / justify-content: center; / 垂直居中 / align-items: center;}.child{ background: green;} justify-content:设置或检索弹性盒子元素在主轴(横轴)方向上的对齐方式; align-items:设置或检索弹性盒子元素在侧轴(纵轴)方向上的对齐方式。 更多细节请参考本人对flex布局的详细介绍 Css 弹性布局(Flex)详细介绍(Flex 属性详解、场景分析)_前端不释卷leo的博客-CSDN博客_flex布局前言我们知道,网页展示就好比一个个盒子堆叠在一起,通过调整盒子的大小、位置、样式等,形成了各式各样的页面。当我们在开发一个页面的时候,我们常规的做法可能是:搭建框架、划分区域、定制排版、调整位置、嵌入内容、微调与增添样式。今天所介绍的是基础且关键的一环--布局排版。其中,展开布局中常用的技术:Flex布局。Flex布局是什么?Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。任何一个容器都可以指定为 Flex 布局。.box {2022 CSS 水平垂直居中方式汇总,全到没朋友(锤爆面试官系列)https://blog.csdn.net/qq_41809113/article/details/121869338?spm=1001.2014.3001.5502 ### 6、flex + margin auto .parent{ width: 500px; height: 500px; border: 1px solid blue; / 核心代码 / display: flex;} .child{ background: green; / 核心代码 / margin: auto;} ### 7、grid 网格布局(一) 注意:由于grid布局实在是太超前,导致了兼容性不是那么理想 .parent{ width: 500px; height: 500px; border: 1px solid blue; / 核心代码 / display: grid; justify-content: center; align-items: center;} .child{ background: green;} 仔细观察,以上样式与flex相似,且主要在父元素设置。 ### 8、grid 网格布局(二) 跟第7点不同,以下主要在子元素设置 .parent{ width: 500px; height: 500px; border: 1px solid blue; / 核心代码 / display: grid;} .child{ background: green; / 核心代码 / justify-self: center; align-self: center;} 2022 CSS 水平垂直居中方式汇总,全到没朋友(锤爆面试官系列) 场景推荐 ---- PC 端有兼容性要求并且宽高固定,推荐使用 absolute + margin: auto 方法实现; PC 端有兼容性要求并且宽高不固定,推荐使用 css-table(display:table-cell)方式实现; PC 端无兼容性要求 ,推荐使用 flex ,如果不考虑 IE 的话,grid 也是个不错的选择; 移动端 ,推荐使用 flex ,grid 也可作为备选。