-
Notifications
You must be signed in to change notification settings - Fork 108
/
CSS Flex布局.html
410 lines (409 loc) · 12.1 KB
/
CSS Flex布局.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//效果见JSBin https://jsbin.com/xivutud/edit?html,css
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Flex Deom</title>
<style>
.box {
width: 50px;
height: 50px;
line-height: 50px;
margin: 10px;
text-align: center;
background-color: yellow;
}
.flex {
display: flex;
}
.direction-row {
flex-direction: row;
}
.direction-row-reverse {
flex-direction: row-reverse;
}
.direction-column {
flex-direction: column;
}
.direction-column-reverse {
flex-direction: column-reverse;
}
.nowrap {
flex-wrap: nowrap;
}
.wrap {
flex-wrap: wrap;
}
.wrap-reverse {
flex-wrap: wrap-reverse;
}
.flex-flow {
flex-flow: row-reverse wrap;
}
.flex-start {
justify-content: flex-start;
}
.flex-end {
justify-content: flex-end;
}
.center {
justify-content: center;
}
.space-between {
justify-content: space-between;
}
.space-around {
justify-content: space-around;
}
.box-wrap {
width: 100%;
height: 300px;
box-sizing: border-box;
border: 1px solid red;
}
.box-stretch {
margin: 10px;
text-align: center;
background-color: yellow;
}
.items-stretch {
align-items: stretch;
}
.items-flex-start {
align-items: flex-start;
}
.items-flex-end {
align-items: flex-end;
}
.items-center {
align-items: center;
}
.items-baseline {
align-items: baseline;
}
.box-wrap2 {
width: 50px;
height: 200px;
border: 1px solid green;
}
.box2 {
width: 50px;
height: 50px;
line-height: 50px;
text-align: center;
box-sizing: border-box;
border: 1px solid blue;
background-color: yellow;
}
.content-stretch {
flex-wrap: wrap;
align-content: stretch;
}
.content-flex-start {
flex-wrap: wrap;
align-content: flex-start;
}
.content-flex-end {
flex-wrap: wrap;
align-content: flex-end;
}
.content-center{
flex-wrap: wrap;
align-content: center;
}
.content-space-between {
flex-wrap: wrap;
align-content: space-between;
}
.content-space-around {
flex-wrap: wrap;
align-content: space-around;
}
.order {
order: -1;
}
.flex-grow {
flex-grow: 2;
}
.flex-shrink {
flex-shrink: 3;
}
.flex-basis {
flex-basis: 200px;
}
.flex-attribution {
flex: 3 0 90px;
}
.self-auto {
align-self: auto;
}
.self-stretch {
align-self: stretch;
}
.self-flex-start {
align-self: flex-start;
}
.self-flex-end {
align-self: flex-end;
}
.self-center {
align-self: center;
}
.self-baseline {
align-self: baseline;
}
</style>
</head>
<body>
<h4>Flex-direction属性决定主轴的方向(即项目的排列方向)。</h4>
<span>row(默认值):主轴为水平方向,起点在左端。</span>
<div class="flex direction-row">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
<span>row-reverse:主轴为水平方向,起点在右端。</span>
<div class="flex direction-row-reverse">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
<span>column:主轴为垂直方向,起点在上沿。</span>
<div class="flex direction-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
<span>column-reverse:主轴为垂直方向,起点在下沿。</span>
<div class="flex direction-column-reverse">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
<hr />
<h4>Flex-wrap属性定义,如果一条轴线排不下,如何换行。</h4>
<span>nowrap(默认):不换行。</span>
<div class="flex nowrap">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
</div>
<span>wrap:换行,第一行在上方。</span>
<div class="flex wrap">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
</div>
<span>wrap-reverse:换行,第一行在下方。</span>
<div class="flex wrap-reverse">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
</div>
<hr />
<h4>Flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。</h4>
<div class="flex flex-flow">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
</div>
<hr />
<h4>Justify-content属性定义了项目在主轴上的对齐方式。</h4>
<span>flex-start(默认值):左对齐</span>
<div class="flex flex-start">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
<span>flex-end右对齐</span>
<div class="flex flex-end">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
<span>center居中</span>
<div class="flex center">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
<span>space-between两端对齐,项目之间的间隔都相等。</span>
<div class="flex space-between">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
<span>space-around每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。</span>
<div class="flex space-around">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
</div>
<hr />
<h4>align-items属性定义项目在交叉轴上如何对齐。</h4>
<span>stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。</span>
<div class="box-wrap flex items-stretch">
<div class="box-stretch">1</div>
<div class="box-stretch">2</div>
<div class="box-stretch">3</div>
<div class="box-stretch">4</div>
<div class="box-stretch">5</div>
</div>
<span>flex-start: 交叉轴的起点对齐。</span>
<div class="box-wrap flex items-flex-start">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
</div>
<span>flex-end: 右对齐</span>
<div class="box-wrap flex items-flex-end">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
</div>
<span>center: 交叉轴的中点对齐。</span>
<div class="box-wrap flex items-center">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
</div>
<span>baseline: 项目的第一行文字的基线对齐。</span>
<div class="box-wrap flex items-baseline">
<div class="box" style="line-height: 10px">1</div>
<div class="box" style="line-height: 20px">2</div>
<div class="box" style="line-height: 30px">3</div>
<div class="box" style="line-height: 40px">4</div>
<div class="box" style="line-height: 50px">5</div>
</div>
<hr />
<h4>align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。</h4>
<span>stretch(默认值):轴线占满整个交叉轴。</span>
<div class="box-wrap2 flex content-stretch">
<div class="box2">1</div>
<div class="box2">2</div>
<div class="box2">3</div>
</div>
<span>flex-start: 交叉轴的起点对齐。</span>
<div class="box-wrap2 flex content-flex-start">
<div class="box2">1</div>
<div class="box2">2</div>
<div class="box2">3</div>
</div>
<span>flex-end: 与交叉轴的终点对齐。</span>
<div class="box-wrap2 flex content-flex-end">
<div class="box2">1</div>
<div class="box2">2</div>
<div class="box2">3</div>
</div>
<span>center: 与交叉轴的中点对齐。</span>
<div class="box-wrap2 flex content-center">
<div class="box2">1</div>
<div class="box2">2</div>
<div class="box2">3</div>
</div>
<span>space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。</span>
<div class="box-wrap2 flex content-space-between">
<div class="box2">1</div>
<div class="box2">2</div>
<div class="box2">3</div>
</div>
<span>space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。</span>
<div class="box-wrap2 flex content-space-around">
<div class="box2">1</div>
<div class="box2">2</div>
<div class="box2">3</div>
</div>
<hr />
<h4>order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。</h4>
<div class="flex">
<div class="box">1</div>
<div class="box">2</div>
<div class="box order">99</div>
<div class="box">4</div>
<div class="box">5</div>
</div>
<hr />
<h4>flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。</h4>
<div class="flex">
<div class="box">1</div>
<div class="box flex-grow">2</div>
<div class="box">1</div>
</div>
<hr />
<h4>flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。</h4>
<div class="flex">
<div class="box">1</div>
<div class="box flex-shrink">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<hr>
<h4>flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。</h4>
<div class="flex">
<div class="box">1</div>
<div class="box flex-basis">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<hr>
<h4>flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。</h4>
<div class="flex">
<div class="box">1</div>
<div class="box flex-attribution">2</div>
<div class="box">3</div>
<div class="box flex-attribution">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<hr />
<h4>align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。</h4>
<span>auto</span>
<div class="box-wrap flex">
<div class="box self-auto">auto</div>
<div class="box-stretch self-stretch">stretch</div>
<div class="box self-flex-start">start</div>
<div class="box self-flex-end">end</div>
<div class="box self-center">center</div>
<div class="box self-baseline" style="line-height: 20px">baseline</div>
</div>
<span>stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。</span>
<span>flex-start: 交叉轴的起点对齐。</span>
<span>flex-end: 右对齐</span>
<span>center: 交叉轴的中点对齐。</span>
<span>baseline: 项目的第一行文字的基线对齐。</span>
<div class="box-wrap flex">
<div class="box" style="line-height: 10px">1</div>
<div class="box self-baseline" style="line-height: 20px">2</div>
<div class="box" style="line-height: 30px">3</div>
<div class="box" style="line-height: 40px">4</div>
<div class="box" style="line-height: 50px">5</div>
</div>
</body>
</html>