一、行高(line-height)法
如果要垂直居中的只有一行或几个文字,那它的制作最为简单,只要让文字的行高和容器的高度相同即可,比如:
p { height:30px; line-height:30px; width:100px; overflow:hidden; }
二、内边距(padding)法
和行高法很相似,它同样适合一行或几行文字垂直居中,原理就是利用padding将内容垂直居中,比如:
p { padding:20px 0; }
一:css如何设置文本文字垂直居中
1、line-height 使文字垂直居中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<!DOCTYPE html>
< html >
< head >
< meta charset = "UTF-8" >
< title >css 垂直居中</ title >
< style >
.box{
width: 300px;
height: 300px;
background: #ddd;
line-height:300px;
}
</ style >
</ head >
< body >
< div class = "box" >css 垂直居中了--文本文字</ div >
</ body >
</ html >
|
这样就能让div中的文字水平垂直居中了
2、CSS3的flex布局 使文字垂直居中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<!DOCTYPE html>
< html >
< head >
< meta charset = "UTF-8" >
< title >css 垂直居中</ title >
< style >
.box{
width: 300px;
height: 300px;
background: #ddd;
line-height:300px;
/*设置为伸缩容器*/
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
/*垂直居中*/
-webkit-box-align: center;/*旧版本*/
-moz-box-align: center;/*旧版本*/
-ms-flex-align: center;/*混合版本*/
-webkit-align-items: center;/*新版本*/
align-items: center;/*新版本*/
}
</ style >
</ head >
< body >
< div class = "box" >css 垂直居中--文本文字(弹性布局)</ div >
</ body >
</ html >
|
二:css如何设置div盒子容器(块状元素)垂直居中
1.使用绝对定位和负外边距对块级元素进行垂直居中 (已知元素的高度)
如果我们知道元素的高度,可以这样来实现垂直居中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<!DOCTYPE html>
< html >
< head >
< meta charset = "UTF-8" >
< title >css 垂直居中</ title >
< style >
.box{
width: 300px;
height: 300px;
background: #ddd;
position: relative;
}
.child{
width: 150px;
height: 100px;
background: orange;
position: absolute;
top: 50%;
margin: -50px 0 0 0;
line-height: 100px;
}
</ style >
</ head >
< body >
< div class = "box" >
< div class = "child" >css 垂直居中</ div >
</ div >
</ body >
</ html >
|
这个方法兼容性不错,但是有一个小缺点:必须提前知道被居中块级元素的尺寸,否则无法准确实现垂直居中。
2. 使用绝对定位和transform(未知元素高度)
如果我们不知道元素的高度,那么就需要先将元素定位到容器的中心位置,然后使用 transform 的 translate 属性,将元素的中心和父容器的中心重合,从而实现垂直居中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<!DOCTYPE html>
< html >
< head >
< meta charset = "UTF-8" >
< title >css 垂直居中</ title >
< style >
.box{
width: 300px;
height: 300px;
background: #ddd;
position: relative;
}
.child{
background: #93BC49;
position: absolute;
top: 50%;
transform: translate(0, -50%);
}
</ style >
</ head >
< body >
< div class = "box" >
< div class = "child" >css 垂直居中,css 垂直居中,css 垂直居中,css 垂直居中,css 垂直居中</ div >
</ div >
</ body >
</ html >
|
这种方法有一个非常明显的好处就是不必提前知道被居中元素的尺寸了,因为transform中translate偏移的百分比就是相对于元素自身的尺寸而言的。
3. 绝对定位结合margin: auto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<!DOCTYPE html>
< html >
< head >
< meta charset = "UTF-8" >
< title >css 垂直居中</ title >
< style >
.box{
width: 300px;
height: 300px;
background: #ddd;
position: relative;
}
.child{
width: 200px;
height: 100px;
background: orange;
position: absolute;
top: 0;
bottom: 0;
margin: auto;
line-height: 100px;
}
</ style >
</ head >
< body >
< div class = "box" >
< div class = "child" >css 垂直居中...</ div >
</ div >
</ body >
</ html >
|
这种方法需要先 把要垂直居中的元素相对于父元素绝对定位,top和bottom设为相等的值,不管设置成多少值,只要两者相等就行;然后再将要居中元素的margin设为auto,这样便可以实现垂直居中了。被居中元素的宽高也可以不设置,但不设置的话就必须是图片这种自身就包含尺寸的元素,否则无法实现。
4. 使用padding实现子元素的垂直居中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<!DOCTYPE html>
< html >
< head >
< meta charset = "UTF-8" >
< title >css 垂直居中</ title >
< style >
.box{
width: 300px;
background: #ddd;
padding: 100px 0;
}
.child{
width: 200px;
height: 100px;
background: orange;
line-height: 100px;
}
</ style >
</ head >
< body >
< div class = "box" >
< div class = "child" >css 垂直居中了</ div >
</ div >
</ body >
</ html >
|
这种实现方式非常简单,就是给父元素设置相等的上下内边距,则子元素自然是垂直居中的,当然这时候父元素是不能设置高度的,要让它自动被填充起来,除非设置了一个正好等于上内边距+子元素高度+下内边距的值,否则无法精确的垂直居中。这种方式看似没有什么技术含量,但其实在某些场景下也是非常好用的。
5. 使用flex布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<!DOCTYPE html>
< html >
< head >
< meta charset = "UTF-8" >
< title >css 垂直居中</ title >
< style >
.box{
width: 300px;
height: 300px;
background: #ddd;
display: flex;
flex-direction: column;
justify-content: center;
}
.child{
width: 300px;
height: 100px;
background: #08BC67;
line-height: 100px;
}
</ style >
</ head >
< body >
< div class = "box" >
< div class = "child" >css 垂直居中了--弹性布局</ div >
</ div >
</ body >
</ html >
|
关于flex布局(弹性布局/伸缩布局)里门道颇多,这里先针对用到的东西简单说一下:
flex也就是flexible,意为灵活的、柔韧的、易弯曲的。
元素可以通过设置display:flex;将其指定为flex布局的容器,指定好了容器之后再为其添加align-items属性,该属性定义项目在交叉轴(这里是纵向轴)上的对齐方式,可能的取值有五个,分别如下:
flex-start::交叉轴的起点对齐;
flex-end:交叉轴的终点对齐;
center:交叉轴的中点对齐;
baseline:项目第一行文字的基线对齐;
stretch(该值是默认值):如果项目没有设置高度或者设为了auto,那么将占满整个容器的高度。