《图解CSS》水平垂直居中

简介: 在面试的过程中,经常会被问到有关CSS的一个问题,那便是居中问题.......

本文对应github地址:https://github.com/956159241/TuJieQianDuan/ 前端面试CSS中。

在面试的过程中,经常会被问到有关CSS的一个问题,那便是居中问题。

![该图由以下分析得出,建议最后再看](https://upload-images.jianshu.io/upload_images/2789632-7ca04d6d21a34e45.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

首先是如何进行水平居中,然后如何进行垂直居中,最后如何进行水平垂直居中。

接下来,一个问题一个问题研究~

水平居中,是基于两个或多个元素之间的位置关系。可能一个父元素和一个子元素,可能一个父元素或多个子元素。

那么父元素和子元素可能存在哪些情况呢?

1. 父元素为行内元素,子元素为行内元素

2. 父元素为行内元素,子元素为块级元素

3. 父元素为块级元素,子元素为行内元素

4. 父元素为块级元素,子元素为块级元素

在设置水平居中的过程中,父元素或者子元素的宽度、高度,子元素单行/多行有时也需要考虑其中。

在研究水平居中/垂直居中之前,先把一些不需要考虑的特殊情况挑出来:

#### 父元素为行内元素,子元素为行内元素(不存在居中问题)

由于父元素,子元素都是行内元素。且行内元素基本都是不可设置宽高的(除去替换元素),所以没有居中的概念。

#### 父元素为行内元素,子元素为块级元素

由于行内元素包含块级元素,导致行内元素也独占一行,可以使用在父元素内添加```text-align: center```,但是不符合开发规范。

###### 注意:不建议在行内元素中嵌套块元素,这样不仅不符合开发规范,还会导致行内元素也独占一行。

## 水平居中

#### 父元素为块级元素,子元素为行内元素

解决方案一:在父元素内添加```text-align: center```。

```html

<!DOCTYPE html>

<html lang="en">

<head>

<style>

 .parent {

  height: 80px;

  margin: 10px;

 }

 .parent1 {

  background-color: white;

  text-align: center;

 }

       .child1 {

           background-color:red;

       }

       .parent2 {

  background-color: blue;

  text-align: center;

           width: 300px;

 }

       .child2 { background-color: red; }

</style>

</head>

<body>

<div class="parent  parent1">

 <code class="child1">1class ClassName {}</code>

   </div>

   <div class="parent  parent2">

       <code class="child2">2class ClassName {}</code>

   </div>

</body>

</html>

```

![](https://upload-images.jianshu.io/upload_images/2789632-40f76260fc4ea60f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

解决方案二: 在父元素添加```display: flex; justify-content: center```,子元素如果不希望高度扩展,在子元素上设置子元素的高度。

```html

<!DOCTYPE html>

<html lang="en">

<head>

<style>

 .parent {

  height: 80px;

  margin: 10px;

 }

 .parent1 {

  background-color: white;

  display: flex;

  justify-content: center;

 }

       .child1 {

           background-color:red;

           height: 20px;

       }

       .parent2 {

  background-color: blue;

  display: flex;

  justify-content: center;

           width: 300px;

 }

       .child2 { background-color: red; height: 20px; }

</style>

</head>

<body>

<div class="parent  parent1">

 <code class="child1">1class ClassName {}</code>

   </div>

   <div class="parent  parent2">

       <code class="child2">2class ClassName {}</code>

   </div>

</body>

</html>

```

![](https://upload-images.jianshu.io/upload_images/2789632-3ec0e2d1128563d6.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

#### 父元素为块级元素,子元素为块级元素

解决方案一: 在父元素添加```display: flex; justify-content: center```,子元素如果不希望高度扩展,在子元素上设置子元素的高度。

```html

<!DOCTYPE html>

<html lang="en">

<head>

<style>

 .parent {

  height: 80px;

  margin: 10px;

 }

 .parent1 {

  background-color: white;

  display: flex;

  justify-content: center;

 }

       .child1 {

           background-color:red;

           height: 20px;

       }

       .parent2 {

  background-color: blue;

  display: flex;

  justify-content: center;

           width: 300px;

 }

       .child2 { background-color: red; height: 20px; }

</style>

</head>

<body>

<div class="parent  parent1">

 <div class="child1">1class ClassName {}</div>

   </div>

   <div class="parent  parent2">

       <div class="child2">2class ClassName {}</div>

   </div>

</body>

</html>

```

![](https://upload-images.jianshu.io/upload_images/2789632-fa67f77f53be14a3.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

解决方案二: 如果子元素宽度固定,在子元素添加```margin: 0 auto```。

```html

<!DOCTYPE html>

<html lang="en">

<head>

<style>

 .parent {

  height: 80px;

  margin: 10px;

 }

 .parent1 {

  background-color: white;

 }

       .child1 {

           background-color:red;

           height: 20px;

           width: 200px;

           margin: 0 auto;

       }

       .parent2 {

  background-color: blue;

 }

       .child2 { background-color: red; height: 20px; width: 30%; margin: 0 auto; }

</style>

</head>

<body>

<div class="parent  parent1">

 <div class="child1">1class ClassName {}</div>

   </div>

   <div class="parent  parent2">

       <div class="child2">2class ClassName {}</div>

   </div>

</body>

</html>

```

![](https://upload-images.jianshu.io/upload_images/2789632-92daddba0616e6b7.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

#### 方案三: 利用position进行定位

父元素添加```position: relative```, 子元素添加```position: absolute;transform: translateX(-50%);```。如果不不适用```transform: translateX(-50%);```就不是真正意义上的居中显示,而是从居中位置开始向后显示,如下实例:

```html

<!DOCTYPE html>

<html lang="en">

<head>

<style>

 .parent {

  height: 80px;

  margin: 10px;

 }

 .parent1 {

  background-color: white;

  position: relative;

 }

 .child1 {

  background-color: red;

  width: 30%;

  position: absolute;

  left: 50%;

 }

 .parent2 {

  background-color: blue;

  position: relative;

 }

 .child2 {

  background-color: red;

  width: 30%;

  position: absolute;

  left: 50%;

  transform: translateX(-50%);

 }

</style>

</head>

<body>

<div class="parent  parent1">

 <div class="child1">1class ClassName {}</div>

</div>

<div class="parent  parent2">

 <div class="child2">2class ClassName {}</div>

</div>

</body>

</html>

```

![](https://upload-images.jianshu.io/upload_images/2789632-474040ff44aee42b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

## 垂直居中

#### 父元素为块级元素,子元素为行内元素

子元素为单行时,可以使用: line-height 为父元素的height,然后设置vertical-align:middel或者父元素设置: dispaly:table-cell;vertical-align: midde;

子元素为多行时: 父元素设置: dispaly:table-cell;vertical-align: midde;

```html

<!DOCTYPE html>

<html lang="en">

<head>

<style>

 .parent {

  height: 80px;

  margin: 10px;

           margin: 10px 0;

 }

 .parent1 {

  background-color: white;

           height: 100px;

 }

 .child1 {

  background-color: red;

           line-height: 100px;

           vertiacl-align: middel;

 }

 .parent2 {

  background-color: yellow;

           width: 100px;

           display: table-cell;

           vertical-align: middle;

 }

 .child2 {

  background-color: red;

 }

       .parent3 {

  background-color: blue;

           height: 100px;

           display: table-cell;

           vertical-align: middle;

 }

 .child3 {

  background-color: red;

 }

</style>

</head>

<body>

<div class="parent  parent1">

 <span class="child1">单行</span>

</div>

   <div class="parent  parent2">

 <span class="child2">单行</span>

</div>

<div class="parent  parent3">

 <span class="child3">多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行多行</span>

</div>

</body>

</html>

```

![](https://upload-images.jianshu.io/upload_images/2789632-2aa6daf5e76455ae.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

#### 父元素为块级元素,子元素为块级元素

###### 方案一: position定位

###### 方案二:table-cell

###### 方案三: flex

```html

<!DOCTYPE html>

<html lang="en">

<head>

<style>

 .parent {

  height: 80px;

  margin: 10px;

           margin: 10px 0;

 }

   /* 方案一: 绝对定位 */

 .parent1 {

  background-color: white;

           height: 100px;

           position: relative;

 }

 .child1 {

  background-color: red;

           position: absolute;

           top: 50%;

           transform: translateY(-50%);

 }

/* 方案二:table-cell  */

 .parent2 {

  background-color: yellow;

           width: 100px;

           display: table-cell;

           vertical-align: middle;

 }

 .child2 {

  background-color: red;

 }

       /*方案三: flex */

       .parent3 {

  background-color: blue;

           height: 100px;

           display: flex;

           align-items:center;

 }

 .child3 {

  background-color: red;

 }

</style>

</head>

<body>

<div class="parent  parent1">

 <div class="child1">内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容</div>

</div>

   <div class="parent  parent2">

 <div class="child2">内容内容内容内容内容内</div>

</div>

<div class="parent  parent3">

 <div class="child3">内容内容内容内容内容内容内容内容内容内内容内容内容内容内容内容内容内容内容内内容内容内容内容内容内容内容内容内容内</div>

</div>

</body>

</html>

```

![](https://upload-images.jianshu.io/upload_images/2789632-42653dda5615505e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

## 水平垂直居中

垂直水平居中就可以利用上面的方法进行组合就好啦,下面推荐使用一些常见的方式:

#### 方案一 flex

```css

align-items:center;

```

#### 方案二 position定位

#### 方案三 grid

align-items: center;

justify-items:center;

place-items: center;

```html

<!DOCTYPE html>

<html lang="en">

<head>

<style>

 .parent {

  height: 80px;

  margin: 10px;

           margin: 10px 0;

 }

   /* 方案一: 绝对定位 */

 .parent1 {

  background-color: white;

           height: 100px;

           position: relative;

 }

 .child1 {

  background-color: red;

           position: absolute;

           top: 50%;

           transform: translateY(-50%);

           left: 50%;

           transform: translateX(-50%);

 }

/* 方案二:flex  */

 .parent2 {

  background-color: yellow;

           display: flex;

           align-items: center;

           justify-content: center;

 }

 .child2 {

  background-color: red;

 }

       /*方案三: gird 1 */

       .parent3 {

  background-color: blue;

           display: grid;

           align-items: center;

           justify-items: center;

           /* 或者简写: place-items: center; */

 }

 .child3 {

  background-color: red;

 }

</style>

</head>

<body>

<div class="parent  parent1">

 <div class="child1">内容内容内容内容内容内容内容内容</div>

</div>

   <div class="parent  parent2">

 <div class="child2">内容内容内容内容内容内</div>

</div>

<div class="parent  parent3">

 <div class="child3">内容内内容内容内容内容内容内容内容内容内容内</div>

</div>

</body>

</html>

```

![](https://upload-images.jianshu.io/upload_images/2789632-1df431b0de4fd1fb.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

参考网址:

https://zhuanlan.zhihu.com/p/83553320

https://blog.csdn.net/weixin_37580235/article/details/82317240

 

目录
相关文章
|
8天前
|
前端开发
CSS水平居中与垂直居中的方法
CSS水平居中与垂直居中的方法
|
8天前
|
前端开发
css flex布局两个元素水平居中垂直居中
css flex布局两个元素水平居中垂直居中
19 1
|
8天前
|
前端开发 容器
CSS面试考点:隐藏元素、BFC、垂直居中、CSS3新特性
【4月更文挑战第2天】 CSS面试考点:隐藏元素、BFC、垂直居中、CSS3新特性
22 10
|
8天前
|
前端开发 Java C++
面试官:用 CSS 实现一个元素的水平垂直居中,写出你能想到的所有答案
面试官:用 CSS 实现一个元素的水平垂直居中,写出你能想到的所有答案
|
10月前
|
前端开发
DIV+CSS文字与图片上下垂直居中-div css居中
DIV+CSS文字与图片上下垂直居中-div css居中
46 0
|
8天前
|
前端开发
CSS 垂直居中
CSS 垂直居中
|
9月前
|
前端开发 小程序
CSS 盒子垂直居中的方法
CSS 盒子垂直居中的方法
109 0
|
9月前
|
XML 前端开发 数据格式
前端样式CSS水平垂直居中的十中方法
首先我们先来了解下什么是CSS 层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现[HTML](https://baike.baidu.com/item/HTML)([标准通用标记语言](https://baike.baidu.com/item/标准通用标记语言/6805073)的一个应用)或[XML](https://baike.baidu.com/item/XML)(标准通用标记语言的一个子集)等文件样式的计算机语言。CSS不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化 CSS 能够对网页中元素位置的排版进行像素级精
52 0
|
10月前
|
前端开发
css如何显示文字垂直居中
css如何显示文字垂直居中
50 0
|
11月前
|
前端开发
CSS 水平垂直居中的方式
CSS 水平垂直居中的方式