-
Notifications
You must be signed in to change notification settings - Fork 14
/
009.元素居中的几种方式.html
69 lines (59 loc) · 1.45 KB
/
009.元素居中的几种方式.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./assets/global.css">
<style>
p {
padding-top: 1em;
margin-top: 0;
}
.center1 {
width: 100px;
height: 100px;
background-color: #f06;
color: #fff;
line-height: 100px;
text-align: center;
}
.center2 {
width: 100px;
height: 100px;
background-color: #f06;
color: #fff;
position: relative;
}
.center2 .box {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.center3 {
width: 100px;
height: 100px;
background-color: #f06;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
}
</style>
</head>
<body>
<p>通过line-height和text-align来控制居中</p>
<div class="center1">
1
</div>
<p>通过position的relative和absolute居中</p>
<div class="center2">
<div class="box">1</div>
</div>
<p>通过flex居中</p>
<div class="center3">
1
</div>
</body>
</html>