一、水平居中

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

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

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

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

  2. css样式水平居中和垂直居中的方法

    水平居中(包含块中居中) 1. 定宽,左右margin为auto.(常规流块盒.弹性项目[不用定宽]) 例子:在box1盒子上设置宽,再设置margin:auto: <style> .bo ...

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

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

  4. CSS设置图片垂直居中的方法

    如果是应用了表格,那么设置单元格为align="center"就可以使其中的一切内容居中.如果没有应用表格要想设置图片居中就有点困难了.困难来自不按"常规出牌" ...

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

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

  6. css的div垂直居中的方法,百分比div垂直居中

    前言 我们都知道,固定高宽的div在网页中垂直居中很简单,相信大家也很容易的写出来,但是不是固定高宽的div如何垂直居中呢?我们在网页布局,特别是手机等web端网页经常是不固定高宽的div,那么这些d ...

  7. 使用CSS完美实现垂直居中的方法

    使用XHTML+CSS来实现元素的垂直居中一直是前端开发中的一个比较复杂且棘手的问题,作为网页设计师或前端开发工程师,这个垂直居中问题也是必须掌握的技巧之一,一些互联网公司面试题中也会出现这类题目.今 ...

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

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

  9. CSS 水平居中与垂直居中

    前言 在CSS布局中,水平居中与垂直居中一直是用到比较多的,在本篇中将介绍水平居中.垂直居中的几种方式. 示例 HTML: <div class="parent"> & ...

随机推荐

  1. 文本编辑_Vim&Vi

    一.Vim.Vi文本编辑器 1️⃣:vi: Visual Interface,文本编辑器 2️⃣:文本:ASCII, Unicode 3️⃣:VIM - Vi IMproved 二.Vim.vi的特点 ...

  2. 创建第一个django工程

    一.环境搭建 使用anaconda + pycharm的环境. 1.创建虚拟环境并安装django1.8的包 # 创建虚拟环境 conda create -n django python=3.6 # ...

  3. Linux进阶之给nginx设置登录用户验证

    一.nginx开启访问验证 使用nginx搭建的站点,如果不想让所有人都能正常访问,那么可以设置访问认证,只有用户输入正确的用户名和密码才能正常访问. 在nginx下,提供了ngx_http_auth ...

  4. 2017-11-20 崂应工作总结,含LTC3780模块分析,含运放原理

    学习了运算放大器的分类 运放的单点输入 差动模式 共模抑制输入模式 反相位比例运放 正相比例运放 电压跟随器 运放的放大比例计算 LTC3780模块的原理 因为: R19  这个电阻不确定他的接法 暂 ...

  5. crontab 的简要介绍

    1.概述: crontab 用于周期性被执行的指令,该指令从标准设备输入指令,并将指令存放在crontab文件中,供之后读取和执行. 与crontab相关的文件一共有三个: /etc/crontab ...

  6. sklearn中,数据集划分函数 StratifiedShuffleSplit.split() 使用踩坑

    在SKLearn中,StratifiedShuffleSplit 类实现了对数据集进行洗牌.分割的功能.但在今晚的实际使用中,发现该类及其方法split()仅能够对二分类样本有效. 一个简单的例子如下 ...

  7. 行业资讯 | Android WebView 致安卓应用闪退,mPaaS 助你规避这波 Bug

    根据外媒 9to5 Google 报道,3 月 23 日几个小时内,大量安卓用户遇到一些安卓应用连续闪退的情况,有人发现卸载安卓系统 WebView 就可以停止闪退.目前,谷歌已经发布了应用崩溃问题的 ...

  8. CoSky-Mirror 就像一个镜子放在 Nacos、CoSky 中间,构建一个统一的服务发现平台

    CoSky 基于 Redis 的服务治理平台(服务注册/发现 & 配置中心) Consul + Sky = CoSky CoSky 是一个轻量级.低成本的服务注册.服务发现. 配置服务 SDK ...

  9. 2. Servlet3.0注解方式 @WebServlet

    web.xml配置还是比较麻烦,这次使用注解方式 编写servlet import javax.servlet.ServletException; import javax.servlet.annot ...

  10. BTC芯片介绍

    BTC芯片介绍 Innosilicon宣布全球第一和最佳的28nm比特币ASIC和参考矿机 A1Craft(也称为A1)是2013年世界上最好的BTC ASIC,这是比特币区块哈希算法的易于使用,定制 ...