-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path水平垂直居中.html
333 lines (299 loc) · 8.52 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<!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="https://necolas.github.io/normalize.css/8.0.1/normalize.css">
<title>居中的几种解决方案</title>
<style>
.son {
width: 100px;
}
img {
width: 100%;
height: 100%;
}
.div {
width: 200px;
height: 200px;
background: yellow;
border: 1px solid black;
}
/* 1.绝对居中 + transform 位移 */
.father1 {
position: relative;
}
.father1 .son {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%)
}
/* 2.flex 属性居中 */
.father2 {
display: flex;
justify-content: center;
align-items: center;
}
/* 3.flex + margin auto */
.father3 {
display: flex;
}
.father3 .son {
margin: auto;
}
/* 4.grid 属性居中 */
.father4 {
display: grid;
justify-content: center;
align-items: center;
}
/* 5.grid + 子项属性居中 */
.father5 {
display: grid;
}
.father5 .son {
justify-self: center;
align-self: center;
}
/* 6.-webkit-box 属性居中 */
.father6 {
display: -webkit-box;
-webkit-box-pack: center;
-webkit-box-align: center;
}
/* 7.table-cell + text-align */
.father7 {
display: table-cell;
vertical-align: middle;
text-align: center;
}
.father7 .son {
display: inline-block;
}
/* 8.line-height + text-align */
.father8 {
line-height: 200px;
text-align: center;
}
.father8 .son {
display: inline-block;
vertical-align: middle;
line-height: initial;
}
/* 9.writing-mode */
.father9 {
writing-mode: tb-lr;
writing-mode: vertical-lr;
text-align: center;
}
.father9 .son {
writing-mode: lr-tb;
writing-mode: horizontal-tb;
text-align: center;
width: 100%;
display: inline-block;
}
.father9 .son .sonson {
display: inline-block;
text-align: initial;
}
/* 10.table */
.father10 {
text-align: center;
}
.father10 .son {
display: inline-block;
}
/* 11.须知宽高 + absolute + 负 margin */
.father11 {
position: relative;
}
.father11 .son {
position: absolute;
top: 50%;
left: 50%;
width: 100px;
height: 100px;
margin-top: -50px;
margin-left: -50px;
}
/* 12.须知宽高 + absolute + calc */
.father12 {
position: relative;
}
.father12 .son {
width: 100px;
height: 100px;
position: absolute;
top: calc(50% - 50px);
left: calc(50% - 50px);
}
/* 13.须知宽高 absolute + margin: auto */
.father13 {
position: relative;
}
.father13 .son {
width: 100px;
height: 100px;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
/* 14.须知宽高 + fixed + transform */
.father14 .son {
width: 100px;
height: 100px;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: blue;
}
/* 15.须知宽高 + fixed + 负 margin */
.father15 .son {
width: 100px;
height: 100px;
position: fixed;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
background: blue;
}
/* 16.须知宽高 + fixed + calc */
.father16 .son {
width: 100px;
height: 100px;
position: fixed;
top: calc(50% - 50px);
left: calc(50% - 50px);
background: blue;
}
/* 17.须知宽高 + fixed + margin: auto */
.father17 .son {
width: 100px;
height: 100px;
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;
background: blue;
}
</style>
</head>
<body>
<p>1.绝对居中 + transform 位移</p>
<div class="div father1">
<div class="son">
<img src="https://i.loli.net/2021/10/22/yWdM3QPrtfb9amD.jpg" alt="">
</div>
</div>
<p>2.flex 属性居中</p>
<div class="div father2">
<div class="son">
<img src="https://i.loli.net/2021/10/22/yWdM3QPrtfb9amD.jpg" alt="">
</div>
</div>
<p>3.flex + margin auto</p>
<div class="div father3">
<div class="son">
<img src="https://i.loli.net/2021/10/22/yWdM3QPrtfb9amD.jpg" alt="">
</div>
</div>
<p>4.grid 属性居中</p>
<div class="div father4">
<div class="son">
<img src="https://i.loli.net/2021/10/22/yWdM3QPrtfb9amD.jpg" alt="">
</div>
</div>
<p>5.grid + 子项属性居中</p>
<div class="div father5">
<div class="son">
<img src="https://i.loli.net/2021/10/22/yWdM3QPrtfb9amD.jpg" alt="">
</div>
</div>
<p>6.-webkit-box 属性居中</p>
<div class="div father6">
<div class="son">
<img src="https://i.loli.net/2021/10/22/yWdM3QPrtfb9amD.jpg" alt="">
</div>
</div>
<p>7.table-cell + text-align</p>
<div class="div father7">
<div class="son">
<img src="https://i.loli.net/2021/10/22/yWdM3QPrtfb9amD.jpg" alt="">
</div>
</div>
<p>8.line-height + text-align</p>
<div class="div father8">
<div class="son">
<img src="https://i.loli.net/2021/10/22/yWdM3QPrtfb9amD.jpg" alt="">
</div>
</div>
<p>9.writing-mode</p>
<div class="div father9">
<div class="son">
<div class="sonson">
hello world!
</div>
</div>
</div>
<p>10.table</p>
<table>
<tbody>
<tr>
<td class="div father10">
<div class="son">
<img src="https://i.loli.net/2021/10/22/yWdM3QPrtfb9amD.jpg" alt="">
</div>
</td>
</tr>
</tbody>
</table>
<p>11.须知宽高 absolute + 负 margin</p>
<div class="div father11">
<div class="son">
<img src="https://i.loli.net/2021/10/22/yWdM3QPrtfb9amD.jpg" alt="">
</div>
</div>
<p>12.须知宽高 absolute + calc</p>
<div class="div father12">
<div class="son">
<img src="https://i.loli.net/2021/10/22/yWdM3QPrtfb9amD.jpg" alt="">
</div>
</div>
<p>13.须知宽高 absolute + margin: auto</p>
<div class="div father13">
<div class="son">
<img src="https://i.loli.net/2021/10/22/yWdM3QPrtfb9amD.jpg" alt="">
</div>
</div>
<p>14.须知宽高 + fixed + transform</p>
<div class="div father14">
<div class="son">
</div>
</div>
<p>15.须知宽高 + fixed + 负margin</p>
<div class="div father15">
<div class="son">
</div>
</div>
<p>16.须知宽高 + fixed + calc</p>
<div class="div father16">
<div class="son">
</div>
</div>
<p>17.须知宽高 + fixed + margin auto</p>
<div class="div father17">
<div class="son">
</div>
</div>
</body>
</html>