css实现水平垂直居中方法

水平居中

行内元素居中

利用text-align 实现行内元素的居中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./css_center.css">
<style>
.parent{
text-align: center;
}
.child{
display: inline-block;
}
</style>
</head>
<body>

<div class="parent">
<div class="child">Demo</div>
</div>
</body>
</html>

块级元素水平居中

可以使用外边距margin-left margin-right 设置为auto即可。则两边都可以水平空出距离。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./css_center.css">
<style>
.parent{
margin: 0 auto;
width: 100px; /* 必须设置width */
}


</style>
</head>
<body>

<div class="parent">
Demo
</div>
</body>
</html>

使用table+margin方法。将子元素设置为块级表格来显示

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 lang="en">

<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./css_center.css">
<style>
.parent{
display: table;
margin: 0 auto;
width:100px;
/* 必须设置width */
}


</style>
</head>
<body>

<div class="parent">
Demo
</div>
</body>
</html>

使用absolute+transform的方法。
将父元素设置为相对定位,再将子元素设置为绝对定位,向右移动子元素,移动距离为父元素的一半,
最后通过向左移动子元素的一半宽度来达到水平居中。
tranform属于css3的属性。

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 lang="en">

<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./css_center.css">
<style>
.parent{
position: relative;
}
.child{
position: absolute;
left: 50%;
transform: translateX(-50%);
}


</style>
</head>
<body>

<div class="parent">
<div class="child">
Demo
</div>
</div>
</body>
</html>

使用flex+justify-content

1
2
3
4
5
6
7
8
9
10
<style>

.parent {

display: flex;

justify-content:center;

}
</style>

浮动元素水平居中

1.对于定宽的浮动元素,通过子元素设置relative+负margin
2.对于不定宽的浮动元素,父子容器都用相对定位
3.通用方法(不管定宽还是不定宽):都使用flex布局。
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
30
31
32
33
34
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./css_center.css">
<style>

.child {

position:relative;

left:50%;

margin-left:-250px;

}


</style>
</head>
<body>



<div class="parent">

<span class="child" style="float: left;width: 500px;">我是要居中的浮动元素</span>

</div>

</body>
</html>

不定宽居中

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
31
32
33
34
35
36
37
38
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./css_center.css">
<style>

.parent {

position:relative;

left:50%;

float: left;

}
p{
float: left;
position: relative;
right: 50%;
}


</style>
</head>
<body>



<div class="parent">

<p class="child">demo</p>
</div>

</body>
</html>

使用flex+justify-content来实现居中

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
31
32
33
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./css_center.css">
<style>

.parent {
display: flex;
justify-content: center;
}
.child{
width: 200px;
text-align: center;
float: left;
}


</style>
</head>
<body>



<div class="parent">

<p class="child">demo</p>
</div>

</body>
</html>

垂直居中

单行内联元素垂直居中

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 lang="en">

<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./css_center.css">
<style>

#box{
height:120px;
line-height: 120px;
border:2px dashed;
}


</style>
</head>
<body>
<div id = "box">
<span>单行内联元素</span>
</div>

</body>
</html>

2.多行内联元素垂直居中
1.使用flex布局
利用flex布局实现垂直居中,其中flex-direction为column,column定义主轴方向为纵轴,

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
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./css_center.css">
<style>

#box{
display: flex;
flex-direction: column;
justify-content: center;
height: 100px;
border: solid;
}


</style>
</head>
<body>
<div id = "box">
<span>单行内联元素</span>
</div>

</body>
</html>

利用表布局的属性

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
31
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./css_center.css">
<style>

.parent{
display: table;
height: 100px;
border: solid;
}
.child{
display: table-cell;
vertical-align: middle;
}


</style>
</head>
<body>
<div class="parent">
<div class="child">
the more
</div>
</div>

</body>
</html>

参考链接