CSS 水平居中与垂直居中
前言
在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 {}
CSS 水平居中与垂直居中的更多相关文章
- CSS水平居中和垂直居中解决方案
一.CSS 居中 — 水平居中 DIV等标签本身没有定义自己居中的属性,网上很多的方法都是介绍用上级的text-align: center,然后嵌套一层DIV来解决问题. 可是这个方法有时候完全不起作 ...
- CSS 水平居中/布局 垂直居中 (月经问题)
水平居中 如果它是一个行内元素 对其父元素使用 text-align:center 即可实现. <p style = " text-align:center; width:300px; ...
- css水平居中和垂直居中
水平居中:内联元素:text-align:center;相对于父级居中显示块级元素:margin:0 auto;但是需要同时width,否则无法看到效果多个块级元素居中:在此想要探讨一下display ...
- CSS 水平居中和垂直居中
1.水平居中——行内元素 text-align: center; 2.水平居中——定宽块状元素 margin: auto,满足定宽和块状两个条件的元素是可以通过设置“左右margin”值为“auto” ...
- CSS水平居中与垂直居中的方法
一.水平居中 1.行内元素水平居中 在父元素里添加text-align:center即可.代码如下: <style> .container-1 { height: 50px; border ...
- CSS的水平居中和垂直居中解决方案
在写CSS样式的时候,有时为了美观,会添加水平居中和垂直居中,这时候你有可能会遇到很棘手的问题,有些水平居中和垂直居中的属性添加上去完全没反应,下面给大家列举一些CSS水平居中和垂直居中的终极解决方案 ...
- css确定元素水平居中和垂直居中
---恢复内容开始--- 首先,我们在了解如何通过css了解元素水平和垂直居中之前,先要了解下html都有哪些元素,这些元素与偶有哪些分类,因为不同类别的元素的水平垂直居中方法是完全不同的,究其根本当 ...
- CSS设置行内元素和块级元素的水平居中、垂直居中
CSS设置行内元素的水平居中 div{text-align:center} /*DIV内的行内元素均会水平居中*/ CSS设置行内元素的垂直居中 div{height:30px; line-heigh ...
- CSS中元素水平居中和垂直居中的方法
#CSS中水平居中和垂直居中的方法 一. 水平居中 1.行内元素(文本,图片等) 如果被设置元素为文本.图片等行内元素时,可以通过给父元素设置` text-align:center;` 来实现 2.定 ...
随机推荐
- 利用cookie实现iframe刷新时停留在当前页面
这段时间第一次用iframe,发现问题还挺多,这次主要解决了一个用cookie实现iframe刷新时停留在当前页面,具体步骤如下: 1.必须在每一个页面中记录下当前的url并存入cookie中,具体代 ...
- 835.Hamming距离
描述 两个整数的Hamming距离是对应比特位不同的个数. 给定两个整数x和y,计算两者的Hamming距离. 0 ≤ x, y < 2^31. 您在真实的面试中是否遇到过这个题? 样例 输入: ...
- re模块、hashlib模块
一.re模块 1.什么是正则? 正则就是用一系列具有特殊含义的字符组成一套规则,该规则用来描述具有某一特征的字符串,正则就是用来去一个大的字符串中匹配出符合规则的子字符串 2.为何要用正则? 用户注册 ...
- 在UnrealEngine中用Custom节点实现径向模糊
//input NotUse 为了开启SceneTextureLookup函数而连接的节点,但是不参与逻辑 //input UV 屏幕缓存的坐标坐标 //input Strength 力度 //inp ...
- [Web]flask-excel实现excel文件下载的前后端实现
之前同事写了前端表格导出的功能, 前后端逻辑没有梳理, 导致后端代码十分臃肿. 接手之后, 重新选择了前端table插件, 从jqxGrid变更为bootstrapTable. 本来想依赖集成的tab ...
- [python] 查询mysql返回datetime类型数据的处理
Python 查询Mysql,如果是datetime类型,在json序列化的时候会出现问题. 在网上查了一下,解决方案基本都是遍历dict数据,如果是datetime则转化为字符串. from dat ...
- (网络数据交互)Android解析Internet的Json资源文件
常用的Internet数据解析格式和工具: 网页显示的json数据如下: {"type":"FeatureCollection","metadata& ...
- Markdown使用简介 及 学习资源整理
Markdown资源整理 官网 http://daringfireball.net/projects/markdown/ http://jgm.github.io/stmd/spec.html htt ...
- React Native小白入门学习路径——一
前言 过去这段时间一直忙着实验室考核任务,拼尽全力完成了自己的任务之后.正准备开始高强度的实验室的学习的时候,实验室组织了新老生交流会,这也应该是头一次这么近距离的面对大四前辈交流想法.感觉自己受益颇 ...
- [OPENCV]cvHoughLines2使用说明
1.cvHoughLines2函数定义: CvSeq *cvHoughLines2 { CvArr *image, void *line_storage, int method, double rho ...