-
Notifications
You must be signed in to change notification settings - Fork 1
/
水平垂直居中大法大全.html
159 lines (159 loc) · 4.13 KB
/
水平垂直居中大法大全.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
/*第一种*/
* {
margin: 0;
padding: 0;
}
.box {
position: relative;
width: 198px;
height: 198px;
border: 1px solid #000000;
}
.center1 {
position: absolute;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -50px;
width: 100px;
height: 100px;
background: #007edf;
}
/*第二种*/
.center2 {
position: absolute;
margin: auto;
top: 0;
left: 0;
bottom: 0;
right: 0;
width: 50px;
height: 50px;
background: red;
}
/*第三种*/
.center3 {
background: red;
display: table-cell;
vertical-align: middle;
width: 198px;
height: 198px;
text-align: center;
float: -left; /*添加浮动后垂直居中失效*/
}
.center3 img {
width: 50%;
}
/*第四种*/
.inner-table {
display: table;
width: 100%;
height: 100%;
}
.center4 {
background: red;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.center4 img {
width: 50%;
display: block;
margin: 0 auto;
}
/*第五种*/
.center5 {
position: fixed;
margin: auto;
top: 0;
left: 0;
bottom: 0;
right: 0;
width: 150px;
height: 150px;
background: blue;
}
/*第六种*/
.center6 {
height: 198px;
line-height: 198px;
text-align: center;
}
/*第七种*/
.center7 {
height: 198px;
display: -webkit-box;
-webkit-box-pack:center;
-webkit-box-align:center;
}
.center7 img {
width: 20%;
display: block;
}
/*第八种*/
.center8 {
width: 80px;
position: absolute;
top: 50%; left: 50%;
-webkit-transform: translate(-50%,-50%);
-ms-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
background: red;
}
/*第9种*/
.center9 {
position: relative;
text-align: center;
height: 198px;
}
.center9:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
background: #000;
}
.centered {
display: inline-block;
vertical-align: middle;
width: 198px;
background: red;
}
</style>
</head>
<body>
<div class="box">
<div class="center1"></div>
</div>
<div class="box">
<div class="center2"></div>
</div>
<div class="box">
<a class="center3"><img src="http://b.hiphotos.baidu.com/image/pic/item/cb8065380cd791234e622e6baf345982b2b7802b.jpg"></a>
</div>
<div class="box">
<div class="inner-table">
<div class="center4"><img src="http://b.hiphotos.baidu.com/image/pic/item/cb8065380cd791234e622e6baf345982b2b7802b.jpg"></div>
</div>
</div>
<div class="center5"></div>
<div class="box">
<div class="center6">居中....</div>
</div>
<div class="box">
<div class="center7">居中....<img src="http://b.hiphotos.baidu.com/image/pic/item/cb8065380cd791234e622e6baf345982b2b7802b.jpg"></div>
</div>
<div class="box">
<div class="center8">居中</div>
</div>
<div class="box" style="margin: 0 auto">
<div class="center9"><div class="centered">居中</div></div>
</div>
</body>
</html>