一、水平居中

1、行内元素水平居中

在父元素里添加text-align:center即可。代码如下:

<style>
.container-1 {
height: 50px;
border: 2px solid grey;
margin-bottom: 50px;
/* 行内元素水平居中 */
text-align: center;
}
</style>
<div class="container-1">
<span>this is item1</span>
</div>

效果图:

2、块级元素水平居中

(1)方法一:使用绝对定位和margin负值(缺点:若子元素宽高改变,margin值也要跟着改变)

(2)方法二:使用绝对定位和transform(缺点:存在兼容性问题)

(3)方法三:使用绝对定位和margin:auto(推荐)

(4)方法四:使用flex布局的justify-content:center(缺点:存在兼容性问题)

代码如下:

 1 <style>
2 /* @块级元素水平居中方法: */
3 /* 方法一:使用绝对定位和margin负值*/
4 .container-2 {
5 position: relative;
6 height: 100px;
7 border: 3px solid blue;
8 margin-bottom: 50px;
9 }
10
11 .item2 {
12 position: absolute;
13 height: 50px;
14 width: 300px;
15 left: 50%;
16 /* 向左平移宽度的一半 */
17 margin-left: -150px;
18 background-color: burlywood;
19 }
20
21 /* 方法二:使用绝对定位和transform */
22 .container-3 {
23 position: relative;
24 height: 200px;
25 border: 5px solid rgb(182, 60, 12);
26 margin-bottom: 50px;
27 }
28
29 .item3 {
30 position: absolute;
31 left: 50%;
32 /*水平方向向左平移自身宽度的50%*/
33 transform: translateX(-50%);
34 background-color: yellow;
35 }
36
37 /* 方法三:使用绝对定位和margin:auto*/
38 .container-4 {
39 position: relative;
40 height: 100px;
41 border: 4px solid green;
42 margin-bottom: 50px;
43 }
44
45 .item4 {
46 position: absolute;
47 left: 0%;
48 right: 0%;
49 height: 50px;
50 width: 500px;
51 background-color: pink;
52 /* 平分子元素左右的剩余空间 */
53 margin: auto;
54 }
55
56 /* 方法四:使用flex布局 */
57 .container-5 {
58 display: flex;
59 height: 100px;
60 border: 4px solid rgb(27, 164, 189);
61 margin-bottom: 50px;
62 /* 主轴方向默认为水平方向,使用justify-content实现居中对齐 */
63 justify-content: center;
64 }
65
66 .item5 {
67 height: 50px;
68 width: 500px;
69 background-color: rgb(194, 255, 192);
70 }
71 </style>
72 <div class="container-2">
73 <div class="item2">使用绝对定位和margin负值</div>
74 </div>
75 <div class="container-3">
76 <div class="item3">使用绝对定位和transform</div>
77 </div>
78 <div class="container-4">
79 <div class="item4">使用绝对定位和margin:auto</div>
80 </div>
81 <div class="container-5">
82 <div class="item5">使用flex布局</div>
83 </div>

效果图:

二、垂直居中

1、行内元素垂直居中

当line-hight等于height时,可实现行内元素垂直居中,代码如下:

 1 <style>
2 .container-1 {
3 height: 50px;
4 border: 2px solid grey;
5 margin-bottom: 50px;
6 /* 行内元素水平垂直居中 */
7 text-align: center;
8 line-height: 50px;//行高与高相等可实现垂直居中
9 }
10 </style>
11 <div class="container-1">
12 <span class="item1">this is item1</span>
13 </div>

效果图:

2、块级元素垂直居中

(1)方法一:使用绝对定位和margin负值(缺点:若子元素宽高改变,margin值也要跟着改变)

(2)方法二:使用绝对定位和transform(缺点:存在兼容性问题)

(3)方法三:使用绝对定位和margin:auto(推荐)

(4)方法四:使用flex布局,改变主轴方向为垂直方向再使用justify-content:center(缺点:存在兼容性问题)

代码如下:

<style>
/* @块级元素垂直居中方法: */
/* 方法一:使用绝对定位和margin负值*/
.container-2 {
position: relative;
height: 100px;
border: 3px solid blue;
margin-bottom: 50px;
} .item2 {
position: absolute;
height: 50px;
width: 300px;
top: 50%;
/* 向上平移高度的一半 */
margin-top: -25px;
background-color: burlywood;
} /* 方法二:使用绝对定位和transform */
.container-3 {
position: relative;
height: 300px;
border: 5px solid rgb(182, 60, 12);
margin-bottom: 50px;
} .item3 {
position: absolute;
height: 100px;
width: 300px;
top: 50%;
/* 垂直方向向上平移自身高度的50% */
transform: translateY(-50%);
background-color: yellow;
} /* 方法三:使用绝对定位和margin:auto*/
.container-4 {
position: relative;
height: 100px;
border: 4px solid green;
margin-bottom: 50px;
} .item4 {
position: absolute;
top: 0%;
bottom: 0%;
height: 50px;
width: 500px;
background-color: pink;
/* 自动平分上下的剩余空间 */
margin: auto;
} /* 方法四:使用flex布局 */
.container-5 {
display: flex;
height: 100px;
border: 4px solid rgb(27, 164, 189);
margin-bottom: 50px;
/* 将主轴方向改为垂直方向 */
flex-direction: column;
/* 对齐方式对居中对齐 */
justify-content: center;
} .item5 {
height: 50px;
width: 500px;
background-color: rgb(194, 255, 192);
}
</style>
<div class="container-2">
<div class="item2">使用绝对定位和margin负值</div>
</div>
<div class="container-3">
<div class="item3">使用绝对定位和transform</div>
</div>
<div class="container-4">
<div class="item4">使用绝对定位和margin:auto</div>
</div>
<div class="container-5">
<div class="item5">使用flex布局</div>
</div>

效果图:

若想实现水平垂直居中,结合二者一起使用即可。

参考文档

https://www.jianshu.com/p/7bbc4860a45c

最新文章

  1. 【ORM】--FluentNHibernate之基本映射详解
  2. 使用spring提供的LocalSessionFactoryBean来得到SessionFactory
  3. iOS开发 Apple Pay
  4. MySql连接JDBC数据库------借鉴的红客联盟的
  5. 负margin在布局中的运用
  6. 小菜鸟学 MQ(三)
  7. Gumshoe - Microsoft Code Coverage Test Toolset
  8. mac端口占用查找进程并杀死
  9. MST(Kruskal’s Minimum Spanning Tree Algorithm)
  10. Android attrs.xml文件中属性类型format值的格式
  11. JavaScript模块化编程 - CommonJS, AMD 和 RequireJS之间的关系
  12. javascript Error对象详解
  13. TestNG并行测试
  14. 用Jmeter实现mysql数据库的增删查改
  15. 在 ASP.NET Core 中发送邮件遇到的坑_学习笔记
  16. wx小程序获取组件属性数据data-prop
  17. ZooKeeper安装,部署
  18. html5 web 摇一摇切换歌曲
  19. test命令详解
  20. SSD回归类物体检测

热门文章

  1. 源码安装Apache(httpd)
  2. Centos 7 进入单用户模式更改root密码方法
  3. Linux中级之netfilter防火墙(iptables)
  4. STM32F4-IAP学习笔记--(转)
  5. Centos6.9以下查看端口占用情况和开启端口命令
  6. 永远的Ace 实验四 团队作业1:软件研发团队组建
  7. 人脸标记检测:ICCV2019论文解析
  8. 微信架构 &amp; 支付架构(上)
  9. 语义分割:基于openCV和深度学习(二)
  10. 十三、给已安装的nginx动态添加模块