前言

在CSS布局中,水平居中与垂直居中一直是用到比较多的,在本篇中将介绍水平居中、垂直居中的几种方式。

示例

HTML

<div class="parent">
<div class="child"></div>
</div>

  

CSS

.parent {
width: 200px;
height: 100px;
position: relative;
background-color: #374858;
} .parent .child {
width: 100px;
height: 50px;
background-color: #9dc3e6;
}

  

效果

1. 水平居中

这里将分别介绍当子元素的样式为内联块级以及绝对定位时的水平居中布局方案。

1.1 子元素为内联样式

说明:当子元素为内联样式时(display: inline-block | inline-flex | inline-grid | inline-table 也含有内联样式特性),只需要设置父元素的text-align: center。

CSS

.parent {width: 200px;height: 100px;position: relative;background-color: #374858;}
.parent .child {width: 100px;height: 50px;background-color: #9dc3e6;} .parent {
text-align: center;
}
.parent .child {
display: inline-block;
}

1.2 子元素为块级样式

说明:父元素和子元素都是块级元素时,设置子元素的margin: 0 auto即可。

注意:此时的子元素需要设置width。

CSS

.parent {width: 200px;height: 100px;position: relative;background-color: #374858;}
.parent .child {width: 100px;height: 50px;background-color: #9dc3e6;} .parent {} .parent .child {
display: block;
margin: 0 auto;
}

1.3 子元素 position: absolute

CSS

.parent {width: 200px;height: 100px;position: relative;background-color: #374858;}
.parent .child {width: 100px;height: 50px;background-color: #9dc3e6;} .parent {} .parent .child {
position: absolute;
left: 50%;
transform: translate(-50%, 0);
}

1.4 父元素 display: flex

说明:此时子元素可为内联、块级元素。

浏览器支持:IE 11。

CSS

.parent {width: 200px;height: 100px;position: relative;background-color: #374858;}
.parent .child {width: 100px;height: 50px;background-color: #9dc3e6;} .parent {
display: flex;
justify-content: center;
} .child {
display: inline;
margin: 0 auto;
}

  

2. 垂直居中

同水平居中一样,这里也将分别介绍当子元素的样式为内联块级以及绝对定位时的垂直居中布局方案。

2.1 子元素为块级元素

CSS

.parent {width: 200px;height: 100px;position: relative;background-color: #374858;}
.parent .child {width: 100px;height: 50px;background-color: #9dc3e6;} .parent {
display: table-cell;
vertical-align: middle;
} .parent .child {
display: block;
}

2.2 子元素 position: absolute

CSS

.parent {width: 200px;height: 100px;position: relative;background-color: #374858;}
.parent .child {width: 100px;height: 50px;background-color: #9dc3e6;} .parent {} .parent .child {
position: absolute;
top: 50%;
transform: translate(0, -50%);
}

2.3 父元素 display: flex

说明:此时子元素可为内联、块级元素

浏览器支持:IE 11。

CSS

.parent {width: 200px;height: 100px;position: relative;background-color: #374858;}
.parent .child {width: 100px;height: 50px;background-color: #9dc3e6;} .parent {
display: flex;
align-items: center;
} .parent .child {
display: inline;
}

3. 水平居中+垂直居中

3.1 子元素 display: inline-block

说明:设置子元素的display: inline-block。

注意:此时的子元素需要设置width和height。

CSS

.parent {width: 200px;height: 100px;position: relative;background-color: #374858;}
.parent .child {width: 100px;height: 50px;background-color: #9dc3e6;} .parent {
text-align: center;
display: table-cell;
vertical-align: middle;
} .parent .child {
display: inline-block;
}

3.2 子元素 position: absolute

说明:此时的子元素为绝对定位。

CSS

.parent {width: 200px;height: 100px;position: relative;background-color: #374858;}
.parent .child {width: 100px;height: 50px;background-color: #9dc3e6;} .parent {
position: relative;
} .parent .child {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}

3.3 父元素 display: flex

浏览器支持IE 11。

CSS

.parent {width: 200px;height: 100px;position: relative;background-color: #374858;}
.parent .child {width: 100px;height: 50px;background-color: #9dc3e6;} .parent {
display: flex;
justify-content: center;
align-items: center;
} .parent .child {}
End
菜单加载中...

CSS 水平居中与垂直居中的更多相关文章

  1. CSS水平居中和垂直居中解决方案

    一.CSS 居中 — 水平居中 DIV等标签本身没有定义自己居中的属性,网上很多的方法都是介绍用上级的text-align: center,然后嵌套一层DIV来解决问题. 可是这个方法有时候完全不起作 ...

  2. CSS 水平居中/布局 垂直居中 (月经问题)

    水平居中 如果它是一个行内元素 对其父元素使用 text-align:center 即可实现. <p style = " text-align:center; width:300px; ...

  3. css水平居中和垂直居中

    水平居中:内联元素:text-align:center;相对于父级居中显示块级元素:margin:0 auto;但是需要同时width,否则无法看到效果多个块级元素居中:在此想要探讨一下display ...

  4. CSS 水平居中和垂直居中

    1.水平居中——行内元素 text-align: center; 2.水平居中——定宽块状元素 margin: auto,满足定宽和块状两个条件的元素是可以通过设置“左右margin”值为“auto” ...

  5. CSS水平居中与垂直居中的方法

    一.水平居中 1.行内元素水平居中 在父元素里添加text-align:center即可.代码如下: <style> .container-1 { height: 50px; border ...

  6. CSS的水平居中和垂直居中解决方案

    在写CSS样式的时候,有时为了美观,会添加水平居中和垂直居中,这时候你有可能会遇到很棘手的问题,有些水平居中和垂直居中的属性添加上去完全没反应,下面给大家列举一些CSS水平居中和垂直居中的终极解决方案 ...

  7. css确定元素水平居中和垂直居中

    ---恢复内容开始--- 首先,我们在了解如何通过css了解元素水平和垂直居中之前,先要了解下html都有哪些元素,这些元素与偶有哪些分类,因为不同类别的元素的水平垂直居中方法是完全不同的,究其根本当 ...

  8. CSS设置行内元素和块级元素的水平居中、垂直居中

    CSS设置行内元素的水平居中 div{text-align:center} /*DIV内的行内元素均会水平居中*/ CSS设置行内元素的垂直居中 div{height:30px; line-heigh ...

  9. CSS中元素水平居中和垂直居中的方法

    #CSS中水平居中和垂直居中的方法 一. 水平居中 1.行内元素(文本,图片等) 如果被设置元素为文本.图片等行内元素时,可以通过给父元素设置` text-align:center;` 来实现 2.定 ...

随机推荐

  1. tensorflow下基于DNN实现实时分辨人脸微表情

    参加学校的国创比赛的时候,我们小组的项目有一部分内容需要用到利用摄像头实现实时检测人脸的表情,因为最近都在看深度学习方面的相关知识,所以就自己动手实现了一下这个小Demo.参考网上的资料,发现大部分是 ...

  2. JavaScript中值类型与引用类型

    JavaScript中的变量类型有哪些? 值类型:字符串(string).数值(number).布尔值(boolean).null.undefined 引用类型:对象(Object).数组(Array ...

  3. 负载均衡---ribbon

    Ribbon:提供云端负载均衡,有多种负载均衡策略可供选择,可配合服务发现和断路器使用. 上一篇简单讲解了eureka的使用,这一篇文章基于上一篇的基础上,讲一下springcloud的另一个重要的组 ...

  4. 如何生成WebAssembly文件?

    许多3D游戏都是用C/C++语言写的,如果能将C/C++语言编译成JavaScript代码,它们不就能在浏览器里运行了吗?Emscripten的底层是LLVM编译器,Emscripten可以将c/c+ ...

  5. DataGridView修改数据并传到数据库

    1. 两个属性设置: 第一个:设置自动创建列,默认为True DataGridView1. AutoGenerateColumns = True; 虽然默认为True,但写下去总是好的!!! 第二个: ...

  6. 手动添加jar包到本地仓库

    最近写一个小项目需要用到阿里大鱼的短信功能,安装官网提供的maven及demo做了下测试,在测试过程中,发现导入的pom文件是无效的,也就是说本地的maven仓库中并么有我们pom中的依赖.于是我在网 ...

  7. bootstrap datepicker显示日历

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...

  8. php数组和对象转换函数

    /**  * 数组 转 对象  *  * @param array $arr 数组  * @return object  */ function array_to_object($arr) {     ...

  9. python系统编程(五)

    多线程-threading python的thread模块是比较底层的模块,python的threading模块是对thread做了一些包装的,可以更加方便的被使用 1. 使用threading模块 ...

  10. java 上传图片 并压缩图片大小(转)

    Thumbnailator 是一个优秀的图片处理的Google开源Java类库.处理效果远比Java API的好.从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生 ...