forked from davegregg/flexbox-demo
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
427 lines (389 loc) · 20.8 KB
/
index.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Flexbox Intro Demo</title>
<link rel="stylesheet" href="./stylesheets/base-style.css" />
</head>
<body>
<header>
<h1>A Demonstration of CSS Flexbox</h1>
</header>
<main>
<div class="buttons">
<button type="reset">Start Over</button>
<button id="view-solutions">View Solutions</button>
</div>
<article id="jump-to-container-1">
<h1><a href="#jump-to-container-1"><span>1</span> – <em>No Flex</em></a></h1>
<details open>
<p>
Unless we've changed something (like using <code>float</code> or <code>display: inline-block;</code>), block-level elements, like DIVs, will flow naturally into the <em>appearance</em> of a column. Why?
</p>
<p>
Because that's the natural flow of an HTML document for block-level elements.
</p>
</details>
<div class="flex">
<!-- The unusual whitespace on the next few lines is intentional. We do not want
extra whitespace (which is meant to keep our HTML clean) to be inserted into
the text content of <style>. -->
<style
contenteditable="plaintext-only"
spellcheck="false"
tabindex="-1">.container-1 {
display: block;
}</style>
<div class="container container-1">
<div class="item item-1"><div>1</div></div>
<div class="item item-2"><div>2</div></div>
<div class="item item-3"><div>3</div></div>
</div>
</div>
</article>
<article id="jump-to-container-2">
<h1><a href="#jump-to-container-2"><span>2</span> – <em>Basic Flex</em></a></h1>
<details open>
<p>
By default, a flex container will adjust the flow of its content as a row. It's <em>main-axis</em> is "row".
</p>
<p>
The elements immediately inside (direct-descendant) a <em>flex container</em> are called, <em>"flex items."</em>
</p>
</details>
<div class="flex">
<style
contenteditable="plaintext-only"
spellcheck="false"
tabindex="-1">.container-2 {}</style>
<div class="container container-2">
<div class="item item-1"><div>1</div></div>
<div class="item item-2"><div>2</div></div>
<div class="item item-3"><div>3</div></div>
</div>
</div>
</article>
<article id="jump-to-container-3">
<h1><a href="#jump-to-container-3"><span>3</span> – <em>Flex-End</em></a></h1>
<details open>
<p>
The <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content">justify-content</a></code> property is responsible for spacing things out along the current <em>main-axis</em> (which is <code>row</code>, by default).
</p>
<p>
Setting <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content">justify-content</a></code> on a flex container to <code>flex-end</code> will move its items to the end of the <em>main-axis</em> – in this case, to the end of the row.
</p>
</details>
<div class="flex">
<style
contenteditable="plaintext-only"
spellcheck="false"
tabindex="-1">.container-3 {}</style>
<div class="container container-3">
<div class="item item-1"><div>1</div></div>
<div class="item item-2"><div>2</div></div>
<div class="item item-3"><div>3</div></div>
</div>
</div>
</article>
<article id="jump-to-container-4">
<h1><a href="#jump-to-container-4"><span>4</span> – <em>Center</em></a></h1>
<details open>
<p>
Setting <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content">justify-content</a></code> on a flex container to <code>center</code> will keep its items always in the center of the <em>main-axis</em>.
</p>
</details>
<div class="flex">
<style
contenteditable="plaintext-only"
spellcheck="false"
tabindex="-1">.container-4 {}</style>
<div class="container container-4">
<div class="item item-1"><div>1</div></div>
<div class="item item-2"><div>2</div></div>
<div class="item item-3"><div>3</div></div>
</div>
</div>
</article>
<article id="jump-to-container-5">
<h1><a href="#jump-to-container-5"><span>5</span> – <em>Space-Between</em></a></h1>
<details open>
<p>
Setting <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content">justify-content</a></code> on a flex container to <code>space-between</code> will distribute any extra space in the main-axis <em>between the items equally.</em>
</p>
<p>
No space will be distributed to the start or the end of the main-axis.
</p>
</details>
<div class="flex">
<style
contenteditable="plaintext-only"
spellcheck="false"
tabindex="-1">.container-5 {}</style>
<div class="container container-5">
<div class="item item-1"><div>1</div></div>
<div class="item item-2"><div>2</div></div>
<div class="item item-3"><div>3</div></div>
</div>
</div>
</article>
<article id="jump-to-container-6">
<h1><a href="#jump-to-container-6"><span>6</span> – <em>Space-Evenly</em></a></h1>
<details open>
<p>
Setting <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content">justify-content</a></code> on a flex container to <code>space-evenly</code> will distribute any extra space in the main-axis <em>between the items equally,</em> just like <code>space-between</code>.
</p>
<p>
However, it will <em>also</em> distribute an equal amount to the start and the end of the axis. You'll get space on the ends – not just in-between.
</p>
</details>
<div class="flex">
<style
contenteditable="plaintext-only"
spellcheck="false"
tabindex="-1">.container-6 {}</style>
<div class="container container-6">
<div class="item item-1"><div>1</div></div>
<div class="item item-2"><div>2</div></div>
<div class="item item-3"><div>3</div></div>
</div>
</div>
</article>
<article id="jump-to-container-7">
<h1><a href="#jump-to-container-7"><span>7</span> – <em>Align: Flex-End</em></a></h1>
<details open>
<p>
The <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/align-items">align-items</a></code> and <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content">justify-content</a></code> properties are identical to each other – with one important distinction: whereas <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content">justify-content</a></code> distributes items along the <em>main-axis,</em> <code>align-items</code> distributes items along the <em>cross-axis.</em>
</p>
<p>
If the <code>main-axis</code> is <code>row</code>, then <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/align-items">align-items</a></code> will distribute things along columns.
</p>
<p>
So, setting <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/align-items">align-items</a></code> to <code>flex-end</code> will move the items each to the end of their respective columns – the bottom.
</p>
</details>
<div class="flex">
<style
contenteditable="plaintext-only"
spellcheck="false"
tabindex="-1">.container-7 {}</style>
<div class="container container-7">
<div class="item item-1"><div>1</div></div>
<div class="item item-2"><div>2</div></div>
<div class="item item-3"><div>3</div></div>
</div>
</div>
</article>
<article id="jump-to-container-8">
<h1><a href="#jump-to-container-8"><span>8</span> – <em>Align: Center</em></a></h1>
<details open>
<p>
Since the <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/align-items">align-items</a></code> property speaks in "columns" (by default), setting its value to <code>center</code> will center the container's items <em>vertically.</em>
</p>
<p>
And all the people rejoiced.
</p>
</details>
<div class="flex">
<style
contenteditable="plaintext-only"
spellcheck="false"
tabindex="-1">.container-8 {}</style>
<div class="container container-8">
<div class="item item-1"><div>1</div></div>
<div class="item item-2"><div>2</div></div>
<div class="item item-3"><div>3</div></div>
</div>
</div>
</article>
<article id="jump-to-container-9">
<h1><a href="#jump-to-container-9"><span>9</span> – <em>Justify-Content + Align-Items</em></a></h1>
<details open>
<p>
Now, we're going to combine some tricks. We can use <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content">justify-content</a></code> and <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/align-items">align-items</a></code> <em>together</em> to distribute flex items along both the row and column axes.
</p>
<p>
And again all the people rejoiced.
</p>
<p>
Let's set <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content">justify-content</a></code> to <code>space-around</code> and <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/align-items">align-items</a></code> to <code>center</code>.
</p>
<p>
(<code>space-around</code> is just like <code>space-evenly</code>, except it only distributes <em>half</em> the usual portion to the ends of the axis. But the items still get evenly spaced out amongst themselves.)
</p>
</details>
<div class="flex">
<style
contenteditable="plaintext-only"
spellcheck="false"
tabindex="-1">.container-9 {}</style>
<div class="container container-9">
<div class="item item-1"><div>1</div></div>
<div class="item item-2"><div>2</div></div>
<div class="item item-3"><div>3</div></div>
</div>
</div>
</article>
<article id="jump-to-container-10">
<h1><a href="#jump-to-container-10"><span>10</span> – <em>Flex-Direction: Row-Reverse</em></a></h1>
<details open>
<p>
Now, let's start toying with <em>main-axis.</em> This we can do by changing the <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction">flex-direction</a></code> to something other than the default of <code>row</code>.
</p>
<p>
One option we have is to use <code>row-reverse</code> to change the starting position of the row to its opposite. The effect will appear as though we're looking at the row in a mirror.
</p>
<p>
The items themselves should remain in the order in which we wrote them, relative to the axis – not necessarily to our eyes.
</p>
<p>
So <code>item-1</code> will now start on the right-hand side of the row, <code>item-2</code> will follow, etc.
</p>
</details>
<div class="flex">
<style
contenteditable="plaintext-only"
spellcheck="false"
tabindex="-1">.container-10 {}</style>
<div class="container container-10">
<div class="item item-1"><div>1</div></div>
<div class="item item-2"><div>2</div></div>
<div class="item item-3"><div>3</div></div>
</div>
</div>
</article>
<article id="jump-to-container-11">
<h1><a href="#jump-to-container-11"><span>11</span> – <em>Flex-Direction: Column</em></a></h1>
<details open>
<p>
Now, we get to something we've been expecting: we can change the main-axis from <code>row</code> to <code>column</code>.
</p>
<p>
Initially, you'll notice this <em>appears</em> identical to the way these boxes display in <a href="#jump-to-container-1">Example 1</a> – where the container had not yet been set to <code>display: flex;</code>.
</p>
<p>
BUT despite the initial visual similarity right now, we also have the flexibility of Flexbox to work with.
</p>
<p>
Just remember: when we change the <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction">flex-direction</a></code>, we change the axis, and when we change the axis, the meanings of <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content">justify-content</a></code> (for main-axis alignment) and <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/align-items">align-items</a></code> (for cross-axis alignment) change.
</p>
</details>
<div class="flex">
<style
contenteditable="plaintext-only"
spellcheck="false"
tabindex="-1">.container-11 {}</style>
<div class="container container-11">
<div class="item item-1"><div>1</div></div>
<div class="item item-2"><div>2</div></div>
<div class="item item-3"><div>3</div></div>
</div>
</div>
</article>
<article id="jump-to-container-12">
<h1><a href="#jump-to-container-12"><span>12</span> – <em>Column + Space-Between + Align: End</em></a></h1>
<details open>
<p>
Let's combine together the three properties we've covered: <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content">justify-content</a></code>, <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/align-items">align-items</a></code>, and <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction">flex-direction</a></code>.
</p>
<p>
Let's observe what happens when we set:
<ul>
<li><code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction">flex-direction</a></code> to <code>column</code></li>
<li><code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content">justify-content</a></code> to <code>space-between</code></li>
<li><code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/align-items">align-items</a></code> to <code>flex-end</code></li>
</ul>
</p>
<p>
Break down what each is doing, in order.
</p>
<p>
Also, bear in mind that, if you are going to change the <em>main-axis</em> using <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction">flex-direction</a></code>, it is <em>far easier</em> to do that <em>before setting any other flex property</em> – because everything is based on the <em>main-axis.</em>
</p>
</details>
<div class="flex">
<style
contenteditable="plaintext-only"
spellcheck="false"
tabindex="-1">.container-12 {}</style>
<div class="container container-12">
<div class="item item-1"><div>1</div></div>
<div class="item item-2"><div>2</div></div>
<div class="item item-3"><div>3</div></div>
</div>
</div>
</article>
<article id="jump-to-container-13">
<h1><a href="#jump-to-container-13"><span>13</span> – <em>Flex-Basis</em></a></h1>
<details open>
<p>
<code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/flex-basis">flex-basis</a></code> can be thought of as an ideal and flow-safe width for a <em>flex item</em>. It is designed to avoid breaking the layout – something <code>width</code> will often do. It takes priority over <code>width</code>.
</p>
<p>
If you provide a <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/flex-basis">flex-basis</a></code> value other than <code>auto</code>, you are saying to the browser,
<blockquote>
Listen, I want <em>this</em> size ideally, but I am willing for it to shrink as much as <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/min-width">min-width</a></code> and grow as much as <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/max-width">max-width</a></code>. Just get as close as you can to the size I want, without breaking the layout.
</blockquote>
</p>
<p>
If you switch the flex container's <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction">flex-direction</a></code> to <code>column</code> or <code>column-reverse</code>, <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/flex-basis">flex-basis</a></code> will manage the item's height, instead of its width.
</p>
</details>
<div class="flex">
<!-- The unusual whitespace on the next few lines is intentional. We do not want
extra whitespace (which is meant to keep our HTML clean) to be inserted into
the text content of <style>. -->
<style
contenteditable="plaintext-only"
spellcheck="false"
tabindex="-1">.container-13 {}
.container-13 .item {}
.container-13 .item-1 {}</style>
<div class="container container-13">
<div class="item item-1"><div>1</div></div>
<div class="item item-2"><div>2</div></div>
<div class="item item-3"><div>3</div></div>
</div>
</div>
</article>
<article id="jump-to-container-14">
<h1><a href="#jump-to-container-14"><span>14</span> – <em>Flex-Grow</em></a></h1>
<details open>
<p><code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/flex-grow">flex-grow</a></code> determines how much of the <em>available space</em> this <em>flex item</em> can grow to take up.</p>
<p>If you provide a <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/flex-grow">flex-grow</a></code> value other than <code>0</code>, you are saying to the browser,
<blockquote>
Listen, if there is some extra space remaining, I want this flex item to eat up some of it. The more this flex item eats, <em>compared to other flex items in the container,</em> the bigger it can grow.
</blockquote>
</p>
<p>
SCENARIO – If the first item has a <code>flex-grow: 3;</code>, and the second item has a <code>flex-grow: 1;</code>, then the first item will consume 3 portions of the extra space, and the second item will consume 1 portion of that extra space. If the <em>third</em> item has been given no <code><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/flex-grow">flex-grow</a></code> value, it will default to 0 portions of the extra space.
</p>
</details>
<div class="flex">
<!-- The unusual whitespace on the next few lines is intentional. We do not want
extra whitespace (which is meant to keep our HTML clean) to be inserted into
the text content of <style>. -->
<style
contenteditable="plaintext-only"
spellcheck="false"
tabindex="-1">.container-14 {}
.container-14 .item {}
.container-14 .item-1 {}</style>
<div class="container container-14">
<div class="item item-1"><div>1</div></div>
<div class="item item-2"><div>2</div></div>
<div class="item item-3"><div>3</div></div>
</div>
</div>
</article>
</main>
<footer>
<h5><em>(Credit to Cramer Grimes for the initial concept and design, and DMG for expanding on it.)</em></h5>
</footer>
<script src="./scripts/globals.js"></script>
<script src="./scripts/initStyleCache.js"></script>
<script src="./scripts/restoreScroll.js"></script>
<script src="./scripts/solutions.js"></script>
<script src="./scripts/enableSoftTabs.js"></script>
</body>
</html>