元素完全居中

元素完全居中的方法

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.box{
display: inline-block;
width: 400px;
height: 400px;
position: relative;
background: red;
}
.box1{
position: absolute;
width: 100px;
height: 100px;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
background: #ccc;
}
.box2{
position: absolute;
width: 100px;
height: 100px;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
background: #ccc;
}
.boxc{
width: 400px;
height: 400px;
display: flex;
align-items: center;
justify-content: center;
background: #ccc;
}
.box3{
width: 200px;
height: 200px;
border: 1px solid red;
}
.box4{
height: 200px;
width: 100px;
margin: auto;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background: #ccc;
overflow: auto;
}
.box4 .cc{
width: 100px;
height:50px;
background: blue;
position: fixed;
z-index: 999;
}
</style>
</head>
<body>
<div class="box">
<div class="box1">

</div>
</div>
<div class="box">
<div class="box2">

</div>
</div>
<div class="boxc">
<div class="box3">

</div>
</div>
<div class="box">
<div class="box4">
<div class="cc"></div>
</div>
</div>
</body>
</html>