15种CSS居中的方式
1 、水平居中
01、 内联元素水平居中
利用 text-align: center 可以实现在块级元素内部的内联元素水平居中。 此方法对内联元素( inline ), 内联块( inline-block ), 内联表( inline-table ), inline-flex 元素水平居中都有效。 Demo代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<strong><!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>42度空间-内联元素水平居中-测试1</title> <style> div { height:60px; border: 2px dashed #f69c55; } .center-text { text-align: center; }</style></head><body><div class="center-text"> 简单是稳定的前提。</div></body></html></strong> |
02、 块级元素水平居中
通过把固定宽度块级元素的 margin-left 和 margin-right 设成auto,就可以使块级元素水平居中。 Demo代码:
|
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
|
<strong><!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>42度空间-块级元素水平居中</title> <style> div { height:100px; border: 2px dashed #f69c55; } .center-block { margin: 0 auto; width: 8rem; padding:1rem; color:#fff; background:#000; }</style></head><body><div> <p class="center-block"> 简单不先于复杂,而是在复杂之后。 </p></div></body></html></strong> |
03、多块级元素水平居中,利用 inline-block
如果一行中有两个或两个以上的块级元素,通过设置块级元素的显示类型为inline-block和父容器的text-align属性从而使多块级元素水平居中。
Demo代码:
|
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
|
<strong><!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>42度空间-多块级元素水平居中-inline-block</title> <style> .container { height:100px; padding: 8px; text-align: center; border: 2px dashed #f69c55; } .inline-block { padding: 8px; width: 4rem; margin: 0 8px; color: #fff; background: #000; display: inline-block; }</style></head><body><div class="container"> <div class="inline-block"> 简单不先于复杂 </div> <div class="inline-block"> 而是在复杂之后 </div> <div class="inline-block"> 简单不先于复杂,而是在复杂之后。 </div></div></body></html></strong> |
04、 多块级元素水平居中,利用 display: flex
利用弹性布局( flex ),实现水平居中,其中 justify-content 用于设置弹性盒子元素在主轴(横轴)方向上的对齐方式,本例中设置子元素水平居中显示。 Demo代码:
|
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
|
<strong><!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>42度空间-多块级元素水平居中-弹性布局</title> <style> .flex-center { padding: 8px; display: flex; justify-content: center; border: 2px dashed #f69c55; } .flex-center >div { padding: 8px; width: 4rem; margin: 0 8px; color: #fff; background: #000; }</style></head><body><div class="flex-center"> <div> 简单不先于复杂。 </div> <div> 简单不先于复杂,而是在复杂之后。 </div> <div> 而是在复杂之后。 </div></div></body></html></strong> |
2、 垂直居中
05、单行内联( inline- )元素垂直居中
通过设置内联元素的高度( height )和行高( line-height )相等,从而使元素垂直居中。 Demo代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<strong><!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>42度空间-单行内联元素垂直居中-line-height</title> <style> #box { height: 120px; line-height: 120px; border: 2px dashed #f69c55; }</style></head><body><div id="box"> 软件在能够复用前必须先能用。</div></body></html></strong> |
06、多行元素垂直居中, 利用表布局( table )
利用表布局的 vertical-align: middle 可以实现子元素的垂直居中。 Demo代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<strong><!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>42度空间-多行内联元素垂直居中-table</title> <style> .center-table { display: table; height: 140px; border: 2px dashed #f69c55; } .v-cell { display: table-cell; vertical-align: middle; }</style></head><body><div class="center-table"> <p class="v-cell">The more technology you learn, the more you realize how little you know.</p></div></body></html></strong> |
07、 多行元素垂直居中, 利用flex布局( flex )
利用flex布局实现垂直居中,其中 flex-direction: column 定义主轴方向为纵向。 因为flex布局是CSS3中定义,在较老的浏览器存在兼容性问题。 Demo代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<strong><!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>42度空间-多行内联元素垂直居中-flex</title> <style> .center-flex { height: 140px; display: flex; flex-direction: column; justify-content: center; border: 2px dashed #f69c55; }</style></head><body><div class="center-flex"> <p>Dance like nobody is watching, code like everybody is.</p></div></body></html></strong> |
08、多行元素垂直居中, 利用“精灵元素”
利用“精灵元素”(ghost element)技术实现垂直居中,即在父容器内放一个100%高度的伪元素,让文本和伪元素垂直对齐,从而达到垂直居中的目的。 Demo代码:
|
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
|
<strong><!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>42度空间-多行内联元素垂直居中-伪元素</title> <style> .ghost-center { position: relative; border: 2px dashed #f69c55; padding: 10px 0; } .ghost-center::before { content: " "; display: inline-block; height: 100%; width: 1%; vertical-align: middle; } .ghost-center p { display: inline-block; vertical-align: middle; width: 12rem; padding:1rem; color:#fff; background:#000; }</style></head><body><div class="ghost-center"> <p>“你毕业才两年,这三年工作经验是怎么来的?”程序员答:“加班。”</p></div></body></html></strong> |
09、 块级元素垂直居中,固 定高度的块级元素
我们知道居中元素的高度和宽度,垂直居中问题就很简单。 通过绝对定位元素距离顶部50%,并设置 margin-top 向上偏移元素高度的一半,就可以实现垂直居中了。 Demo代码:
|
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
|
<strong><!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>42度空间-固定高度的块元素垂直居中</title> <style> .parent { height: 140px; position: relative; border: 2px dashed #f69c55; } .child { position: absolute; top: 50%; height: 100px; margin-top: -50px; color:#fff; background: #000; }</style></head><body><div class="parent"> <div class="child">控制复杂性是计算机编程的本质。</div></div></body></html></strong> |
10、块级元素垂直居中, 未知高度的块级元素
当垂直居中的元素的高度和宽度未知时,我们可以借助CSS3中的 transform 属性向Y轴反向偏移50%的方法实现垂直居中。 但是部分浏览器存在兼容性的问题。 Demo代码:
|
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
|
<strong><!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>42度空间-未知高度的块元素垂直居中</title> <style> .parent { height: 140px; position: relative; border: 2px dashed #f69c55; } .child { position: absolute; top: 50%; transform: translateY(-50%); background: black; color: #fff; padding: 1rem; width: 12rem; }</style></head><body><div class="parent"> <div class="child">世界上有 10 种人,懂二进制的和不懂二进制的。</div></div></body></html></strong> |
3 、水平垂直居中
11、固定宽高元素水平垂直居中
通过margin平移元素整体宽度的一半,使元素水平垂直居中。 Demo代码:
|
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>42度空间-固定宽高元素水平垂直居中</title> <style> .parent { height: 140px; position: relative; border: 2px dashed #f69c55; } .child { width: 200px; height: 80px; padding: 10px; position: absolute; top: 50%; left: 50%; margin: -50px 0 0 -110px; background: black; color: #fff; }</style></head><body><div class="parent"> <div class="child">控制复杂性是计算机编程的本质。</div></div></body></html> |
12、未知宽高元素水平垂直居中
利用2D变换,在水平和垂直两个方向都向反向平移宽高的一半,从而使元素水平垂直居中。 Demo代码:
|
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>42度空间-未知宽高元素水平垂直居中</title> <style> .parent { height: 140px; position: relative; border: 2px dashed #f69c55; } .child { padding: 10px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: #fff; background: black; }</style></head><body><div class="parent"> <div class="child">当你试图解决一个你不理解的问题时,复杂化就产成了。</div></div></body></html> |
13、 利用flex布局
利用flex布局,其中 justify-content 用于设置或检索弹性盒子元素在主轴(横轴)方向上的对齐方式; 而 align-items 属性定义flex子项在flex容器的当前行的侧轴(纵轴)方向上的对齐方式。 Demo代码:
|
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>42度空间-利用flex布局实现元素水平垂直居中</title> <style> .parent { height: 140px; display: flex; justify-content: center; align-items: center; border: 2px dashed #f69c55; } .child { padding: 20px; background: black; color: #fff; }</style></head><body><div class="parent"> <div class="child">Facebook wasn't built in a day.</div></div></body></html> |
14、 利用grid布局
利用grid实现水平垂直居中。 Demo代码:
|
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><head> <meta charset="utf-8"> <title>42度空间-利用grid布局实现元素水平垂直居中</title> <style> .parent { height: 140px; display: grid; align-items:center; border: 2px dashed #f69c55; } .child { margin:auto; padding: 20px; width:10rem; color: #fff; background: black; }</style></head><body><div class="parent"> <div class="child">好的程序员能写出人能读懂的代码。</div></div></body></html> |
15、 屏幕上水平垂直居中
屏幕上水平垂直居中十分常用,常规的登录及注册页面都需要用到。 要保证较好的兼容性,还需要用到表布局。 Demo代码:
|
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
39
40
41
42
43
44
45
46
|
<!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>42度空间-如何使DIV在屏幕上水平垂直居中显示?兼容性要好</title> <style> .outer { display: table; position: absolute; height: 100%; width: 100%; } .middle { display: table-cell; vertical-align: middle; } .inner { margin-left: auto; margin-right: auto; background: #2b2b2b; color:#fff; padding: 2rem; max-width: 320px; } </style></head><body><div class="outer"> <div class="middle"> <div class="inner"> <p>一个好的程序员应该是那种过单行线都要往两边看的人。</p> <button value="add" id="add">增加内容</button> </div> </div></div><script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script><script type="text/javascript"> $(document).ready(function () { $("#add").click(function () { $("p").after("<p>解决问题大多数都很容易;找到问题出在哪里却很难。</p>"); }); });</script></body></html> width: 400px;} |

浙公网安备 33010602011771号