-
Notifications
You must be signed in to change notification settings - Fork 790
Expand file tree
/
Copy pathOverview.src.html
More file actions
1239 lines (941 loc) · 66.6 KB
/
Overview.src.html
File metadata and controls
1239 lines (941 loc) · 66.6 KB
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>CSS Flexible Box Layout Module</title>
<link rel="stylesheet" type="text/css" href="http://dev.w3.org/csswg/default.css">
<link rel="stylesheet" type="text/css" href="http://www.w3.org/StyleSheets/TR/W3C-ED">
<style>
code.one-line { white-space: pre; }
.code-and-figure {
display: table;
margin: 0 auto;
border-spacing: 1em;
}
.code-and-figure > div {
display: table-cell;
vertical-align: middle;
}
</style>
</head><body>
<div class="head">
<!--logo-->
<h1 id="head-box-flexible">CSS Flexible Box Layout Module</h1>
<h2 class="no-num no-toc" id="w3c-working">[LONGSTATUS], [DATE]</h2>
<dl>
<dt>This version:
<!--<dd><a href="[VERSION]">[VERSION]</a>-->
<dd><a href="http://dev.w3.org/csswg/[SHORTNAME]/">http://dev.w3.org/csswg/[SHORTNAME]/</a>
<dt>Latest version:
<!--<dd><a href="[LATEST]">[LATEST]</a>-->
<dd><a href="http://www.w3.org/TR/css3-flexbox/">http://www.w3.org/TR/css3-flexbox/</a>
<dt>Editor's Draft:
<dd><a href="http://dev.w3.org/csswg/css3-flexbox/">http://dev.w3.org/csswg/css3-flexbox/</a>
<dt>Previous versions:
<dd><a href="http://www.w3.org/TR/2011/WD-css3-flexbox-20111129/">http://www.w3.org/TR/2011/WD-css3-flexbox-20111129/</a>
<dd><a href="http://www.w3.org/TR/2011/WD-css3-flexbox-20110322/">http://www.w3.org/TR/2011/WD-css3-flexbox-20110322/</a>
<dd><a href="http://www.w3.org/TR/2009/WD-css3-flexbox-20090723/">http://www.w3.org/TR/2009/WD-css3-flexbox-20090723/</a>
<dt>Issues List:</dt>
<dd><p class=issue>Will be a bugzilla URL once I get a component added.</p>
<dt>Discussion:</dt>
<dd><a href="http://lists.w3.org/Archives/Public/www-style/">www-style@w3.org</a> with subject line "<code>[css3-flexbox] …message topic…</code>"
<dt>Editors:
<dd><a href="http://www.xanthir.com/contact">Tab Atkins Jr.</a>, Google Inc.</dd>
<dd>Alex Mogilevsky, Microsoft Corporation, <a href="mailto:alexmog@microsoft.com">alexmog@microsoft.com</a>
<dd>L. David Baron, Mozilla Corporation, <a href="mailto:dbaron@dbaron.org">dbaron@dbaron.org</a>
<dt>Authors and former editors:
<dd>Neil Deakin, Mozilla Corporation, <a href="mailto:enndeakin@gmail.com">enndeakin@gmail.com</a>
<dd>Ian Hickson, formerly of Opera Software, <a href="mailto:ian@hixie.ch">ian@hixie.ch</a>
<dd>David Hyatt, formerly of Netscape Corporation, <a href="mailto:hyatt@apple.com">hyatt@apple.com</a>
</dl>
<!--copyright-->
<hr title="Separator for header">
</div>
<h2 class="no-num no-toc" id="abstract">
Abstract</h2>
<p>The specification describes a CSS box model optimized for user interface design. In the flexbox layout model, the children of a flexbox can be laid out in any direction, and can "flex" their sizes, either growing to fill unused space or shrinking to avoid overflowing the parent. Both horizontal and vertical alignment of the children can be easily manipulated. Nesting of these boxes (horizontal inside vertical, or vertical inside horizontal) can be used to build layouts in two dimensions.</p>
<h2 class="no-num no-toc" id="status">Status of this document</h2>
<!--status-->
<h2 class="no-num no-toc" id="table">Table of contents</h2>
<!--toc-->
<p class=issue>Add "Canonical Order" fields to all the propdef tables, per <a href="http://wiki.csswg.org/spec/format-update">http://wiki.csswg.org/spec/format-update</a>.</p>
<h2 id="intro">
Introduction</h2>
<p><em>This section is not normative.</em>
<p>CSS 2.1 defined four layout modes — algorithms which determine the size and position of boxes based on their relationships with their sibling and ancestor boxes: block layout, designed for laying out documents and simple applications; inline layout, designed for laying out text; table layout, designed for laying out information in a tabular format; and positioned layout, designed for very explicit positioning without much regard for other elements in the document. This module introduces a new layout mode, flexbox layout, which is designed for laying out more complex applications and webpages.</p>
<p>Flexbox layout is superficially similar to block layout. It lacks many of the more complex text or document-formatting properties that can be used in block layout, such as 'float' and 'columns', but in return it gains more simple and powerful tools for aligning its contents in ways that webapps and complex web pages often need. </p>
<p>The contents of a flexbox can be laid out in any direction (left, right, down, or even up!), can have their order swapped around dynamically (i.e., display order is independent of source order), and can "flex" their sizes and positions to respond to the available space. If a flexbox is <i>multi-line</i>, the flexbox items flow in two dimensions, wrapping into separate lines in a fashion similar to how text is wrapped into multiple lines.</p>
<h3 id="placement">
Module interactions</h3>
<p>This module extends the definition of the 'display' property. It also defines a new type of primitive value, the <a href="#flex-function">flex() notation</a>, which is accepted by the 'width' and 'height' properties.
<h3 id="values">
Values</h3>
<p>This specification follows the <a href="http://www.w3.org/TR/CSS21/about.html#property-defs">CSS property definition conventions</a> from [[!CSS21]]. Value types not defined in this specification are defined in CSS Level 2 Revision 1 [[!CSS21]]. Other CSS modules may expand the definitions of these value types: for example [[CSS3COLOR]], when combined with this module, expands the definition of the <color> value type as used in this specification.</p>
<p>In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the <a href="http://www.w3.org/TR/CSS21/cascade.html#value-def-inherit">inherit</a> keyword as their property value. For readability it has not been repeated explicitly.
<h2 id='box-model'>
The Flexbox Box Model</h2>
<p>An element with ''display:flexbox'' or ''display:inline-flexbox'' is a <dfn>flexbox</dfn>. Block-level children of a flexbox, and some other types of children, are called <dfn title="flexbox item|flexbox items|flexbox item's" id="flexbox-item">flexbox items</dfn> and are laid out using the flexbox box model. <span class='note'>(See the <a href="#flex-items">Flexbox Items</a> chapter, below, for specifics on which children are <i>flexbox items</i> directly and which children are instead wrapped in anonymous boxes which are then <i>flexbox items</i>)</span></p>
<p>Unlike block layout, which is normally biased towards laying things out vertically, and inline layout, which is normally biased toward laying things out horizontally, the flexbox layout algorithm is agnostic as to the direction the flexbox happens to be laid out in. To make it easier to talk about flexbox layout in a general way, we will define several direction-agnostic terms here to make the rest of the spec easier to read and understand.</p>
<div class='figure'>
<img src='images/flex-direction-terms.svg' width=510 height=300>
<p class='caption'>An illustration of the various directions and sizing terms used in this specification, respectively for ''row'' and ''column'' flexboxes.</p>
</div>
<p>The <dfn>main axis</dfn> of a flexbox is the axis on which <i>flexbox items</i> are laid out along. The <i>flexbox items</i> are ordered such that they start on the <dfn>main-start</dfn> side of the flexbox, and go toward the <dfn>main-end</dfn> side. A <i>flexbox item's</i> width or height, whichever is in the <i>main axis</i>, is the item's <dfn>main size</dfn>. The <i>flexbox item's</i> <dfn>main size property</dfn> is either the 'width' or 'height' property, whichever is in the <i>main axis</i>.</p>
<p>The axis perpendicular to the <i>main axis</i> is called the <dfn>cross axis</dfn>, and similarly has <dfn>cross-start</dfn> and <dfn>cross-end</dfn> directions and sides defined. The width or height of a <i>flexbox item</i>, whichever is in the <i>cross axis</i>, is the item's <dfn>cross size</dfn>, and similarly the <dfn>cross size property</dfn> is whichever of 'width' or 'height' that is in the <i>cross axis</i>.</p>
<p>The contents of a flexbox can be easily and powerfully manipulated with a handful of properties. Most significantly, <i>flexbox items</i> can "flex" their <i>main size</i> by using the ''flex()'' function in the 'width' or 'height' property. This "flexing" allows the items to get bigger or smaller based on the available space in the page. If there is leftover space in the flexbox after all of the <i>flexbox items</i> have finished flexing, the items can be aligned, centered, or distributed with the 'flex-pack' property. <i>Flexbox items</i> can also be completely rearranged within the flexbox with the 'flex-order' property.</p>
<p>In the <i>cross axis</i>, <i>flexbox items</i> can either "flex" to fill the available space or be aligned within the space with the 'flex-align' property. If a flexbox is <i>multi-line</i>, new lines are added in the <i>cross-end</i> direction, and can similarly be aligned, centered, or distributed within the flexbox with the 'flex-line-pack' property.</p>
<div class="example">
<p>For example, the following HTML snippet uses flexbox to create a toolbar with icons. The flexbox is horizontal, and the children's widths don't fill the flexbox's width, so the additional space is distributed around and between the children. As the flexbox grows (perhaps because the user is viewing the page on a wider screen), the children spread out evenly and automatically:</p>
<pre>
<ul>
<li><button><img src='new.svg' alt="New"></button></li>
<li><button><img src='upload.svg' alt="Upload"></button></li>
<li><button><img src='save.svg' alt="Save"></button></li>
<li>&
7292
;lt;button><img src='trash.svg' alt="trash"></button></li>
</ul>
<style>
ul { display: flexbox; flex-pack: distribute; }
/* Irrelevant styling for this example removed. */
</style></pre>
<div class='figure'>
<img src="images/toolbar-example.svg" width=400 height=140>
<p class='caption'>Example rendering of the above code snippet, at two different flexbox widths.</p>
</div>
</div>
<h3 id='display-flexbox'>
New values for 'display' property</h3>
<table class="propdef">
<tr>
<th>Name:
<td>display
<tr>
<th><a href="http://dev.w3.org/csswg/css-module/#values">New value</a>:
<td>flexbox | inline-flexbox
</table>
<p>You can declare that an element is a flexbox, and thus should use flexbox layout for its contents, by setting the 'display' property on the element to the value 'flexbox' or 'inline-flexbox'.</p>
<p>The ''flexbox'' value makes the flexbox act like a block in other layout modes. The ''inline-flexbox'' value makes the flexbox act like an inline-block in other layout modes. When it is necessary to distinguish them, this specification will refer to the former kind of flexbox as <dfn id="block-flexbox" title="block flexbox|block flexboxes">block flexboxes</dfn> and the latter type as <dfn id="inline-flexbox" title="inline flexbox|inline flexboxes">inline flexboxes</dfn>.</p>
<p>Flexboxes use a new layout algorithm, and so some properties that were designed with the assumption of block layout don't make sense in a flexbox context. In particular:</p>
<ul>
<li>all of the 'column-*' properties in the Multicol module compute to their initial values on a flexbox ('break-before', 'break-inside', and 'break-after' are still valid on a flexbox).</li>
<li>'float' and 'clear' have no effect on a flexbox item. Using 'float' on an element still causes that element's 'display' property to compute to ''block'', as normal, because that occurs before flexbox items are determined (the algorithm for wrapping children of a flexbox into <i>flexbox items</i> needs the computed value of 'display').</li>
<li>'vertical-align' has no effect on a flexbox item</li>
</ul>
<p>If an element's specified value for 'display' is ''inline-flexbox'' and the element is floated or absolutely positioned, the computed value of 'display' must be ''flexbox''.</p>
<p>A flexbox creates a new flexbox formatting context for its contents. This is similar to a block formatting context: floats must not intrude into the flexbox, and the flexbox's margins do not collapse with the margins of its contents. Additionally, all of the <i>flexbox items</i> establish new block formatting contexts for their contents.</p>
<p class='issue'>Figure out the right terms to use here.</p>
<h3 id='flex-items'>
Flexbox Items</h3>
<p>Flexbox layout algorithm operates on <i>flexbox items</i>, which are boxes that satisfy at least one of the following criteria:</p>
<ol>
<li>Immediate block-level children of flexbox</li>
<li>Atomic inline-level children of flexbox</li>
<li>Contiguous run of non-replaced inline children, wrapped into an anonymous box</li>
</ol>
<p>Some values of 'display' trigger "fixup" to ensure a sensible tree structure. For example, a lone ''table-cell'' box is fixed up by generating ''table'', ''table-row-group'', and ''table-row'' boxes around it. This fixup must occur <em>before</em> a flexbox's contents are checked to see if it's necessary to generate anonymous flexbox items.</p>
<p class='note'>In the future, other kinds of fixup such as ''display:run-in'' or ''display:ruby'' should also run before flexbox fixup.</p>
<div class="example">
<p>Examples of flexbox items:</p>
<pre>
<div style="display:flexbox">
<!-- flexbox item: block-level child -->
<div id="item1">block</div>
<!-- not a flexbox item, because it's out-of-flow -->
<div id="not-an-item1.5" style="position: absolute;">block</div>
<!-- flexbox item: block-level child -->
<div id="item2" style="display:table">table</div>
<!-- flexbox item: anonymous table wrapped around table-cell -->
<div id="item3" style="display:table-cell">table-cell</div>
<!-- flexbox item: anonymous block around inline content -->
anonymous item 4
<!-- flexbox item: block-level child -->
<div id="item5">block</div>
<!-- flexbox item: anonymous block around inline content -->
anonymous item 6.1
<span id="item6.1">
text 6.2
<div id="not-an-item6.3">block</div>
text 6.4
</span>
<!-- flexbox item: block-level replaced element -->
<iframe id="item7"></iframe>
<!-- flexbox item: inline-level replaced element -->
<img id="item7" style="display:inline">
<!-- flexbox item: inline-level replaced element -->
<button id="item8">button</button>
<!-- flexbox item: inline-table -->
<div id="item9" style="display:inline-table">table</div>
<!-- flexbox item: floated inline, which changes to a block -->
<span id="item10" style="float: left;">span</span>
</div></pre>
<p>Notice that block element "not-an-item6.3" is not a separate flexbox item, because it is contained inside an inline element which is being wrapped into an anonymous flex item. Similarly, the block element "not-an-item1.5" is not a flexbox item, because it's absolutely positioned and thus out of flow.</p>
</div>
<h4 id='abspos-flexbox-items'>
Positioning Absolutely Positioned Flexbox Items</h4>
<p>Absolutely positioned children of a flexbox are not <i>flexbox items</i>, but they leave behind "placeholders" in their normal position in the box tree. These placeholders are anonymous inline boxes with a width and height of ''0px'', and they interact normally with the flexbox layout algorithm. In particular, they'll trigger the creation an anonymous flexbox item wrapper boxes, or join neighboring inline elements in <em>their</em> anonymous flexbox item wrapper boxes.</p>
<p>The "static position" of an absolutely positioned child of a flexbox (the position when the 'top'/'right'/'bottom'/'left' properties are ''auto''), then, is the final position of their corresponding placeholder, after flexbox layout has been performed.</p>
<p class='note'>Note: In most cases, this means that absolutely positioned items will have no effect on flexbox layout, even if they force the generation of an anonymous flexbox item wrapper, because those wrapper items will also collapse to zero size and have no effect. The only exception is when the flexbox has ''flex-pack:justify'', in which case the anonymous flexbox item will cause there to be two packing spaces where there would otherwise be only one, which will appear as a double-size space between two "real" items.</p>
<h2 id='ordering-orientation'>
Ordering and Orientation</h2>
<p>The first level of flexbox functionality is the ability to lay out a flexbox's contents in any direction and in any order. This allows an author to trivially achieve effects that would previously have required complex or fragile methods, such as using the 'float' property to lay out a horizontal navigation bar (which then requires further effort with the 'clear' property or others to make the elements interact nicely with the rest of the page). This functionality is exposed through the 'flex-flow' and 'flex-order' properties.</p>
<h3 id='flex-flow'>
Flexbox Flow Direction: the 'flex-direction', 'flex-wrap', and 'flex-flow' properties</h3>
<table class=propdef>
<tr>
<th>Name:</th>
<td><dfn>flex-direction</dfn>
<tr>
<th><a href="http://dev.w3.org/csswg/css-module/#values">Value</a>:
<td>row | row-reverse | column | column-reverse
<tr>
<th>Initial:
<td>row
<tr>
<th>Applies To:
<td>flexboxes
<tr>
<th>Inherited:
<td>no
<tr>
<th>Computed Value:
<td>specified value
<tr>
<th>Media:
<td>visual
<tr>
<th>Animatable:
<td>no
</table>
<table class=propdef>
<tr>
<th>Name:</th>
<td><dfn>flex-wrap</dfn>
<tr>
<th><a href="http://dev.w3.org/csswg/css-module/#values">Value</a>:
<td>no-wrap | wrap | wrap-reverse
<tr>
<th>Initial:
<td>no-wrap
<tr>
<th>Applies To:
<td>flexboxes
<tr>
<th>Inherited:
<td>no
<tr>
<th>Computed Value:
<td>specified value
<tr>
<th>Media:
<td>visual
<tr>
<th>Animatable:
<td>no
</table>
<table class=propdef>
<tr>
<th>Name:</th>
<td><dfn>flex-flow</dfn>
<tr>
<th><a href="http://dev.w3.org/csswg/css-module/#values">Value</a>:
<td><'flex-direction'> || <'flex-wrap'>
<tr>
<th>Initial:
<td>see individual properties
<tr>
<th>Applies To:
<td>flexboxes
<tr>
<th>Inherited:
<td>see individual properties
<tr>
<th>Computed Value:
<td>see individual properties
<tr>
<th>Media:
<td>visual
<tr>
<th>Animatable:
<td>no
</table>
<p>The 'flex-direction' property specifies how <i>flexbox items</i> are placed in the flexbox, by setting the direction of the flexbox's <i>main axis</i>. This affects the direction that flexbox items are laid out in, and the meaning of the 'flex-pack' property.</p>
<dl>
<dt><dfn id='flex-flow-row'>row</dfn></dt>
<dd>The flexbox's <i>main axis</i> has the same orientation as the inline axis of the current writing mode (the direction that text is laid out in). The <i>main-start</i> and <i>main-end</i> directions are equivalent to the "start" and "end" directions, respectively, of the current writing mode.</dd>
<dt><dfn id='flex-flow-row-reverse'>row-reverse</dfn></dt>
<dd>Same as <i>row</i>, except the <i>main-start</i> and <i>main-end</i> directions are swapped.</dd>
<dt><dfn id='flex-flow-column'>column</dfn></dt>
&
1898
lt;dd>The flexbox's <i>main axis</i> has the same orientation as the block axis of the current writing mode (the direction that blocks are laid out in). The <i>main-start</i> and <i>main-end</i> directions are equivalent to the "before" and "after" directions, respectively, of the current writing mode.</dd>
<dt><dfn id='flex-flow-column-reverse'>column-reverse</dfn></dt>
<dd>Same as <i>column</i>, except the <i>main-start</i> and <i>main-end</i> directions are swapped.</dd>
</dl>
<p>The 'flex-wrap' property controls whether the flexbox is <i>single-line</i> or <i>multi-line</i>, and the direction of the <i>cross axis</i>, which affects the direction new lines are stacked in and the meaning of the 'flex-align', 'flex-item-align', and 'flex-line-pack' properties.</p>
<dl>
<dt><dfn id='flex-flow-no-wrap'>no-wrap</dfn></dt>
<dd>The flexbox is <i>single-line</i>. The <i>cross-start</i> direction is equivalent to either the "start" or "before" direction of the current writing mode, whichever is in the <i>cross-axis</i>, and the <i>cross-end</i> direction is the opposite direction of <i>cross-start</i>.</dd>
<dt><dfn id='flex-flow-wrap'>wrap</dfn></dt>
<dd>The flexbox is <i>multi-line</i>. The <i>cross-start</i> direction is equivalent to either the "start" or "before" direction of the current writing mode, whichever is in the <i>cross-axis</i>, and the <i>cross-end</i> direction is the opposite direction of <i>cross-start</i>.</dd>
<dt><dfn id='flex-flow-wrap-reverse'>wrap-reverse</dfn></dt>
<dd>Same as <i>wrap</i>, except the <i>cross-start</i> and <i>cross-end</i> directions are swapped.</dd>
</dl>
<p>The 'flex-flow' property is a shorthand for setting the 'flex-direction' and 'flex-wrap' properties together.</p>
<div class="example">
<p>Some examples of valid flows:</p>
<table style="margin: 0 auto; vertical-align: middle; border-spacing: 2em 1em;">
<tr>
<td><pre>
div { flex-flow: row; }
/* Initial value. Main axis is
inline, no wrap. */</pre>
<td><img src='images/flex-flow1.svg' width=240 height=80>
<tr>
<td><pre>
div { flex-flow: column wrap; }
/* Main axis is block-direction and lines
wrap in the inline direction. For an
English page, the main axis is top-to-bottom
and lines wrap to the right. */</pre>
<td><img src='images/flex-flow2.svg' width=170 height=200>
<tr>
<td><pre>
div { writing-mode: tb-rl;
flex-flow: column wrap-reverse; }
/* Main axis is block direction (right to
left). New lines wrap upwards. */</pre>
<td><img src='images/flex-flow3.svg' width=200 height=170>
</table>
</div>
<h3 id='flex-order'>
Display Order: the 'flex-order' property</h3>
<p><i>Flexbox items</i> are, by default, displayed and laid out in the same order as they appear in the source document. The 'flex-order' property may be used to change this ordering.</p>
<table class=propdef>
<tr>
<th>Name:
<td><dfn>flex-order</dfn>
<tr>
<th><a href="http://dev.w3.org/csswg/css-module/#values">Value</a>:
<td><number>
<tr>
<th>Initial:
<td>0
<tr>
<th>Applies to:
<td><i>flexbox items</i></td>
<tr>
<th>Inherited:
<td>no
<tr>
<th>Computed value:
<td>specified value
<tr>
<th>Media:
<td>visual
<tr>
<th>Animatable:
<td>yes
</table>
<p>The 'flex-order' property assigns <i>flexbox items</i> to ordinal groups.</p>
<p>Ordinal groups control the order in which <i>flexbox items</i> appear. A flexbox will lay out its content starting from the lowest numbered ordinal group and going up. Items with the same ordinal group are laid out in the order they appear in the source document.
<div class='example'>
<p>The following figure shows a simple tabbed interface, where the tab for the active pane is always in front:</p>
<div class='figure'><img src="images/flex-order-example.png"></div>
<p>This could be implemented with the following CSS (showing only the flexbox-relevant code):</p>
<pre>
.tabs {
display: flexbox;
}
.tabs > .current {
flex-order: -1; /* Lower than the default of 0 */
}</pre>
<p>You can also see a <a href="flex-order-example.html">working example</a> in some modern browsers (modern WebKit, Firefox, or any browser that supports the Flexbox spec).</p>
</div>
<div class='example'>
<p>Many web pages have a similar shape in the markup, with a header on top, a footer on bottom, and then a content area and one or two additional columns in the middle. Generally, it's desirable that the content come first in the page's source code, before the additional columns. However, this makes many common designs, such as simply having the additional columns on the left and the content area on the right, difficult to achieve. This has been addressed in many ways over the years, often going by the name "Holy Grail Layout" when there are two additional columns. 'flex-order' makes this trivial. For example, take the following sketch of a page's code and desired layout:</p>
<div class='code-and-figure'>
<div><pre>
<!DOCTYPE html>
<header>...</header>
<div id='main'>
<article>...</article>
<nav>...</nav>
<aside>...</aside>
</div>
<footer>...</footer></pre></div>
<div><img src="images/flex-order-page.svg" width=400 height=360></div>
</div>
<p>This layout can be easily achieved with Flexbox:</p>
<pre>
#main { display: flexbox; }
#main > article { width: flex(1); flex-order: 2; }
#main > nav { width: 200px; flex-order: 1; }
#main > aside { width: 200px; flex-order: 3; }</pre>
<p>As an added bonus, the columns will all be equal-height by default, and the main content will be as wide as necessary to fill the screen. Additionally, this can then be combined with media queries to switch to an all-vertical layout on narrow screens:</p>
<pre>
@media all and (max-width: 600px) {
/* Too narrow to support three columns */
#main { flex-flow: column; }
#main > article, #main > nav, #main > aside {
/* Return them to document order */
flex-order: 0; width: auto;
}
}</pre>
<p><small>(Further use of multiline flexboxes to achieve even more intelligent wrapping left as an exercise for the reader.)</small></p>
</div>
<h2 id='flexibility'>
Flexibility: the ''flex()'' notation</h2>
<p>The defining aspect of flexbox layout is the ability to make the <i>flexbox items</i> "flex", altering their width or height to fill the available space. This is done by setting an element's 'width' or 'height' to a <i>flexible length</i> value.</p>
<p>The ''flex()'' notation is used to specify the parameters of a <dfn title="flexible length|flexible lengths|flexible length's">flexible length</dfn>: the <dfn id="positive-flexibility" title="positive flexibility">positive</dfn> and <dfn>negative flexibility</dfn>, and the <dfn>preferred size</dfn>. The syntax of the ''flex()'' notation is:</p>
<pre class=prod><dfn><flex></dfn> = flex( [ <pos-flex> <neg-flex>? ]? || <preferred-size>? )</pre>
<p><code><pos-flex></code> and <code><neg-flex></code> are non-negative <code><numbers>s</code>, while <code><preferred-size></code> is any value (other than another ''<flex>'') that would be valid in the 'width' or 'height' property in which the notation is used. If the <code><preferred-size></code> is ''0'', it <strong>must</strong> be specified with a unit (like ''0px'') to avoid ambiguity; unitless zero will either be interpreted as as one of the flexibilities, or is a syntax error.</p>
<p>The <code><pos-flex></code> component sets the length's <i>positive flexibility</i>; if omitted, the <i>positive flexibility</i> defaults to ''1''. The <code><neg-flex></code> component sets the length's <i>negative flexibility</i>; if omitted, it defaults to ''0''. The <code><preferred-size></code> component sets the length's <i>preferred size</i>; if omitted, it defaults to ''0px''.</p>
<p>If the ''flex()'' notation is specified on an element that is not a <i>flexbox item</i>, it computes to the <i>preferred size</i>. If the ''flex()'' notation is used on a <i>flexbox item's</i> <i>cross size property</i>, it computes to the <i>preferred size</i>. Otherwise, it computes to itself, and is resolved to a length at used-value time by the flexbox layout algorithm.</p>
<p>A ''<flex>'' value is transitionable, by transitioning the <i>preferred size</i>, <i>positive flexibility</i>, and <i>negative flexibility</i> independently. ''<flex>'' can also transition to and from a <length>, by treating the length as if it were a flexible length with a positive and negative flexibility of zero and a preferred size of the length.</p>
<p class='issue'>Transitioning to/from a <length> (or to/from a <flex> with 0 flexibility) doesn't work well if it's the only flexible item in the flexbox. Rather than smoothly transitioning, it suddenly jumps in size when the flexibility becomes non-zero. Can we fix this, perhaps with a value that represents a percentage of the free space or something?</p>
<div class='example'>
<p>The ''flex()'' notation allows elements to respond directly to the available space, in ways that are difficult or impossible in normal CSS. One common example is a page with one segment intended to stay on screen, and another section which may be long but which shouldn't scroll the page, like a chat room:</p>
<pre>
<!DOCTYPE html>
<body>
<header>Channel: #csswg Topic: CSS IS AWE SOME</header>
<article id='messages'>
<p><b>Tab Atkins:</b>
I think the layout algorithm is done.</p>
<p><b>Fantasai:</b>
SUBMIT BEFORE ZOD.</p>
...
</article>
<form>
<input name='message'>
<button type='submit'>Send</button>
</form>
</body>
<style>
body { display:flexbox; flex-flow: column; }
#messages { height: flex(1); }
</style></pre>
<p class='issue'>Illustrate this example.</p>
</div>
<h2 id='alignment'>
Alignment</h2>
<p>After a flexbox's contents have finished their flexing, they can be aligned in both the <i>main axis</i> with 'flex-pack' and the <i>cross axis</i> with 'flex-align' and 'flex-item-align'. These properties make many common types of alignment trivial, including some things that were very difficult in CSS 2.1, like horizontal and vertical centering.</p>
<h3 id='flex-pack'>
Main Axis Alignment: the 'flex-pack' property</h3>
<table class=propdef>
<tr>
<th>Name:
<td><dfn>flex-pack</dfn>
<tr>
<th><a href="http://dev.w3.org/csswg/css-module/#values">Value</a>:
<td>start | end | center | justify | distribute
<tr>
<th>Initial:
<td>start
<tr>
<th>Applies to:
<td>flexboxes
<tr>
<th>Inherited:
<td>no
<tr>
<th>Computed Value:
<td>specified value
<tr>
<th>Media:
<td>visual
<tr>
<th>Animatable:
<td>no
</table>
<p>The 'flex-pack' property aligns <i>flexbox items</i> in the <i>main axis</i> of the current line of the flexbox. This is done <em title=''>after</em> any flexible lengths have been resolved. Typically it helps distribute extra free space leftover when either all the <i>flexbox items</i> on a line are inflexible, or are flexible but have reach their maximum size, but it also exerts some control over the alignment of items when they overflow the line.</p>
<dl>
<dt><dfn id='flex-pack-start'>start</dfn></dt>
<dd><i>Flexbox items</i> are packed toward the start of the line. The <i>main-start</i> margin edge of the first <i>flexbox item</i> on the line is placed flush with the <i>main-start</i> edge of the line, and each subsequent <i>flexbox item</i> is placed flush with the preceding item.</dd>
<dt><dfn id='flex-pack-end'>end</dfn></dt>
<dd><i>Flexbox items</i> are packed toward the end of the line. The <i>main-end</i> margin edge of the last <i>flexbox item</i> is placed flush with the <i>main-end</i> edge of the line, and each preceding <i>flexbox item</i> is placed flush with the subsequent item.</dd>
<dt><dfn id='flex-pack-center'>center</dfn></dt>
<dd><i>Flexbox items</i> are packed toward the center of the line. The <i>flexbox items</i> on the line are placed flush with each other and aligned in the center of the line, with equal amounts of empty space between the <i>main-start</i> edge of the line and the first item on the line and between the <i>main-end</i> edge of the line and the last item on the line. (If the leftover free-space is
1898
negative, the <i>flexbox items</i> will overflow equally in both directions.)</dd>
<dt><dfn id='flex-pack-justify'>justify</dfn></dt>
<dd><i>Flexbox items</i> are evenly distributed in the line. If the leftover free-space is negative or there is only a single <i>flexbox item</i> on the line, this value is identical to ''start''. Otherwise, the <i>main-start</i> margin edge of the first <i>flexbox item</i> on the line is placed flush with the <i>main-start</i> edge of the line, the <i>main-end</i> margin edge of the last <i>flexbox item</i> on the line is placed flush with the <i>main-end</i> edge of the line, and the remaining <i>flexbox items</i> on the line are distributed so that the empty space between any two adjacent items is the same.</dd>
<dt><dfn id='flex-pack-distribute'>distribute</dfn></dt>
<dd><i>Flexbox items</i> are evenly distributed in the line, with half-size spaces on either end. If the leftover free-space is negative or there is only a single <i>flexbox item</i> on the line, this value is identical to ''center''. Otherwise, the <i>flexbox items</i> on the line are distributed such that the empty space between any two adjacent <i>flexbox items</i> on the line is the same, and the empty space before the first and after the last <i>flexbox items</i> on the line are half the size of the other empty spaces.</dd>
</dl>
<div class="figure">
<img src="images/flex-pack.svg" width=504 height=270>
<p class='caption'>An illustration of the five 'flex-pack' keywords and their effects on a flexbox with three colored items.</p>
</div>
<p class='note'>A previous revision of this spec allowed margins to flex directly, which allowed an effect similar to 'flex-pack'. In particular, it allowed an author to, for example, split a flexbox in half, with some of the items pushed toward the start and the rest pushed toward the end, by flexing exactly one margin. This sort of effect is no longer possible without either using the ::before or ::after pseudoelements or adding additional elements to the document, to act as an empty item that can then flex. It's expected that we will develop something in the future to make this easier, such as a more general pseudoelement or perhaps the ability to explicitly control individual spaces between flexbox items.</p>
<h3 id='flex-align'>
Cross Axis Alignment: the 'flex-align' and 'flex-item-align' properties</h3>
<table class=propdef>
<tr>
<th>Name:
<td><dfn>flex-align</dfn>
<tr>
<th><a href="http://dev.w3.org/csswg/css-module/#values">Value</a>:
<td>start | end | center | baseline | stretch
<tr>
<th>Initial:
<td>stretch
<tr>
<th>Applies to:
<td>flexboxes
<tr>
<th>Inherited:
<td>no
<tr>
<th>Computed Value:
<td>specified value
<tr>
<th>Media:
<td>visual
<tr>
<th>Animatable:
<td>no
</table>
<table class=propdef>
<tr>
<th>Name:
<td><dfn>flex-item-align</dfn>
<tr>
<th><a href="http://dev.w3.org/csswg/css-module/#values">Value</a>:
<td>auto | start | end | center | baseline | stretch
<tr>
<th>Initial:
<td>auto
<tr>
<th>Applies to:
<td><i>flexbox items</i>
<tr>
<th>Inherited:
<td>no
<tr>
<th>Computed Value:
<td>''auto'' computes to parent's 'flex-align'; otherwise as specified
<tr>
<th>Media:
<td>visual
<tr>
<th>Animatable:
<td>no
</table>
<p><i>Flexbox items</i> can be aligned in the <i>cross axis</i> of the current line of the flexbox, similar to 'flex-pack' but in the perpendendicular direction. 'flex-align' sets the default alignment for all of the flexbox's <i title="flexbox items">items</i>, including anonymous <i>flexbox items</i>. 'flex-item-align' allows this default alignment to be overridden for individual <i>flexbox items</i> (for anonymous flexbox items, 'flex-item-align' always matches the value of 'flex-align' on their associated flexbox).</p>
<p>A value of <dfn id="flex-item-align-auto">''auto''</dfn> for 'flex-item-align' computes to the value of 'flex-align' on the <i>flexbox item's</i> flexbox. The alignments are defined as:</p>
<dl>
<dt><dfn id="flex-align-start">start</dfn></dt>
<dd>The <i>cross-start</i> margin edge of the <i>flexbox item</i> is placed flush with the <i>cross-start</i> edge of the line.</dd>
<dt><dfn id="flex-align-end">end</dfn></dt>
<dd>The <i>cross-end</i> margin edge of the <i>flexbox item</i> is placed flush with the <i>cross-end</i> edge of the line.</dd>
<dt><dfn id="flex-align-center">center</dfn></dt>
<dd>The <i>flexbox item's</i> margin box is centered in the <i>cross axis</i> within the line.</dd>
<dt><dfn id="flex-align-baseline">baseline</dfn></dt>
<dd>
<p>If the <i>flexbox item's</i> inline axis is the same as the <i>cross axis</i>, this value is identical to ''start''.</p>
<p>Otherwise, all <i>flexbox items</i> on the line with an alignment of ''baseline'' that don't run afoul of the previous paragraph are aligned such that their baselines align, and the item with the largest distance between its baseline and its <i>cross-start</i> margin edge is placed flush against the <i>cross-start</i> edge of the line.</p>
</dd>
<dt><dfn id="flex-align-stretch">stretch</dfn></dt>
<dd>
<p>If the <i>cross size property</i> of the <i>flexbox item</i> is ''auto'', it resolves to the length necessary to make the <i>cross size</i> of the item's margin box the same size as the line, while still respecting 'min/max-width/height' constraints as normal.</p>
<p>The <i>cross-start</i> margin edge of the <i>flexbox item</i> is placed flush with the <i>cross-start</i> edge of the line.</p>
</dd>
</dl>
<div class='figure'>
<img src="images/flex-align.svg" width=508 height=377>
<p class='caption'>An illustration of the five 'flex-align' keywords and their effects on a flexbox with four colored items.</p>
</div>
<div class=example>
<p>By using a vertical flexbox and 'flex-align', we can emulate the functionality of HTML's <code><center></code> element:</p>
<div class='code-and-figure'>
<div><pre>
<div>
<p>foo foo foo foo</p>
<p>bar bar<br>bar bar</p>
<p>foo foo foo foo foo foo foo
foo foo foo foo foo</p>
</div>
<style>
div {
display: flexbox;
flex-flow: column;
flex-align: center;
width: 200px;
}
</style></pre></div>
<div><p><img alt="" src="images/basic-vertical-flexbox.png"></p></div>
</div>
</div>
<p>The precise effects of this property are articulated in the <a href="#layout-algorithm">Layout Algorithm</a> section.</p>
<h2 id='multi-line'>
Multi-line Flexbox</h2>
<p>A flexbox can be either <i>single-line</i> or <i>multi-line</i>, depending on the 'flex-wrap' property. A <dfn>single-line</dfn> flexbox lays out all of its children in a single line, even if that would cause the flexbox to overflow its bounds. A <dfn>multi-line</dfn> flexbox breaks its <i>flexbox items</i> across multiple lines to avoid overflowing, similar to how text is broken onto a new line when it gets too wide to fit on the existing line. Every line contains at least one <i>flexbox item</i>, unless the flexbox itself is completely empty.</p>
<p>When additional lines are created, they are stacked in the flexbox in the <i>cross axis</i>. Each line is completely independent; flexible lengths and the 'flex-pack' and 'flex-align' properties only pay attention to the items on a single line at a time. The <i>main size</i> of a line is the same as the <i>main size</i> of the flexbox's content box. The <i>cross size</i> of a line depends on whether the flexbox is <i>single-line</i> or <i>multi-line</i>: the <i>cross size</i> of the sole line in a <i>single-line</i> flexbox is the same as the <i>cross size</i> of the flexbox's content box, while the <i>cross size</i> of a line in a <i>multi-line</i> flexbox is the minimum size necessary to contain the <i>flexbox items</i> on the line, after aligning them with 'flex-align'. The lines themselves are then aligned within a flexbox with the 'flex-line-pack' property.</p>
<p class='issue'>Should flexbox lines be actual boxes in the box-tree, or just a theoretical construct to help define things?</p>
<div class=example>
<p>This example shows four buttons that do not fit horizontally.</p>
<pre><style>
#div1 {
display: flexbox;
flex-flow: row wrap;
width: 300px;
}
button {
width: flex(80px 1);
}
<style>
<div id="div1">
<button id="button1">Elephant</button>
<button id="button2">Tiger</button>
<button id="button3">Antelope</button>
<button id="button4">Wildebeest</button>
</div></pre>
<p>The buttons are first set to their preferred widths, in this case 80 pixels. This will allow the first three buttons to fit in 240 pixels with 60 pixels left over of remaining space. Because the 'flex-flow' property specifies a multi-line flexbox (due to the ''wrap'' keyword appearing in its value), the flexbox will create an additional line to contain the last button.</p>
<p>Flexibility is applied to each element, separately for each line. The first line has 60 pixels of remaining space and all of the buttons have the same flexibility, so each of the three buttons on that line will receive 20 pixels of extra width, ending up 100px wide. The remaining button is on a line of its own and will stretch to the entire width of the line, or 300 pixels.</p>
<div style="width:300px; border:medium solid green; overflow: hidden;">
<button style="width:100px; float: left;">Elephant</button>
<button style="width:100px; float: left;">Tiger</button>
<button style="width:100px; float: left;">Antelope</button>
<button style="width:300px; float: left;">Wildebeest</button>
</div>
<p>If the box was resized, the buttons may rearrange onto different lines as necessary.</p>
<p>If the style rules in the example above were changed to the following:</p>
<pre>
#div1 {
display: flexbox;
flex-flow: row wrap;
flex-pack: center;
width: 300px;
}
button {
width: flex(80px 1);
max-width: 90px;
}</pre>
<p>Similar to the previous example, the first three buttons will fit on the first line, and the last button will wrap onto a new line. However, when the buttons attempt to flex they can only grow to 90px each, due to their 'max-width' property. This leaves 30px of free space on the first line and 210px of free space on the second line. Because 'flex-pack' is set to ''center'', the buttons will be centered on each line, with the free space split equally on either side.</p>
<div style="width:300px; border:medium solid green; text-align:center;">
<button style="width:90px; float: left; margin-left: 15px;">Elephant</button>
<button style="width:90px; float: left;">Tiger</button>
<button style="width:90px; float: left;">Antelope</button>
<button style="width:90px">Wildebeest</button>
</div>
</div>
<h3 id='flex-line-pack'>
'flex-line-pack' property</h3>
<table class=propdef>
<tr>
<th>Name:
<td><dfn>flex-line-pack</dfn>
<tr>
<th><a href="http://dev.w3.org/csswg/css-module/#values">Value</a>:
<td>start | end | center | justify
<tr>
<th>Initial:
<td>start
<tr>
<th>Applies to:
<td><i>multi-line</i> flexboxes
<tr>
<th>Inherited:
<td>no
<tr>
<th>Computed Value:
<td>specified value
<tr>
<th>Media:
<td>visual
<tr>
<th>Animatable:
<td>no
</table>
<p>The 'flex-line-pack' property aligns a flexbox's lines within the flexbox when there is extra space in the <i>cross axis</i>, similar to how 'flex-pack' aligns individual items within the <i>main axis</i>:</p>
<dl>
<dt><dfn id='flex-line-pack-start'>start</dfn></dt>
<dd>Lines are packed toward the start of the flexbox. The <i>cross-start</i> edge of the first line in the flexbox is placed flush with the <i>cross-start</i> edge of the flexbox, and each subsequent line is placed flush with the preceding line.</dd>
<dt><dfn id='flex-line-pack-end'>end</dfn></dt>
<dd>Lines are packed toward the end of the flexbox. The <i>cross-end</i> edge of the last line is placed flush with the <i>cross-end</i> edge of the flexbox, and each preceding line is placed flush with the subsequent line.</dd>
<dt><dfn id='flex-line-pack-center'>center</dfn></dt>
<dd>Lines are packed toward the center of the flexbox. The lines in the flexbox are placed flush with each other and aligned in the center of the flexbox, with equal amounts of empty space between the <i>cross-start</i> content edge of the flexbox and the first line in the flexbox and between the <i>cross-end</i> content edge of the flexbox and the last line in the flexbox. (If the leftover free-space is negative, the lines will overflow equally in both directions.)</dd>
<dt><dfn id='flex-line-pack-justify'>justify</dfn></dt>
<dd>Lines are evenly distributed in the flexbox. If the leftover free-space is negative or there is only a single line in the flexbox, this value is identical to ''start''. Otherwise, the <i>cross-start</i> edge of the first line in the flexbox is placed flush with the <i>cross-start</i> content edge of the flexbox, the <i>cross-end</i> edge of the last line in the flexbox is placed flush with the <i>cross-end</i> content edge of the flexbox, and the remaining lines in the flexbox are distributed so that the empty space between any two adjacent lines is the same.</dd>
</dl>
<p class='note'>Note: Only <i>multi-line</i> flexboxes ever have free space in the <i>cross axis</i> for lines to be aligned in, because in a <i>single-line</i> flexbox the sole line automatically stretches to fill the space.</p>
<p class="issue">TODO: examples</p>
<h2 id='layout-algorithm'>
Flexbox Layout Algorithm</h2>
<p>This section contains normative algorithms detailing the exact layout behavior of a flexbox and its contents. The algorithms here were designed to optimize readability and theoretical simplicity, and may not necessarily be the most efficient. Implementations may use whatever actual algorithms they wish, but must produce the same results as the algorithms described here.</p>
<
67F6
div id="LC847" class="react-file-line html-div" data-testid="code-cell" data-line-number="847" style="position:relative">
<div class=note>
<p>This note will outline the general structure of the layout algorithm, before I go into the ugly details below, to aid in reading the relatively-long and complex algorithm.</p>
<ol>
<li>Reorder flexbox items according to 'flex-order'.</li>
<li>Find the "hypothetical size" of every flexbox item.</li>
<li>Linebreak the flexbox, if it's multi-line.</li>
<li>Based on the hypothetical sizes of the items, find the real main size of the flexbox and the hypothetical cross size.</li>
<li>Resolve any flexible lengths. All items now have a real main size.</li>
<li>Align in the main axis, per 'flex-pack'.</li>
<li>Based on 'flex-align', find the real cross size of the flexbox, its lines, and the items.</li>
<li>Align in the cross axis, per 'flex-align'.</li>
<li>Align the lines, per 'flex-line-pack'.</li>
</ol>
</div>
<p>The layout algorithm for flexbox is, unfortunately, too complex and interdependent to usefully describe in pieces within each property's description. As such, the normative layout algorithm is described in this chapter. Note that this is the <em>definitive</em> description of the flexbox layout algorithm - any information located elsewhere in this spec is informative only. Note as well that only the <strong>output</strong> of the algorithm is normative. The precise steps described in this section are informative, and written with an eye toward clarity and simplicity, not efficiency. Any algorithm that achieves the same results is acceptable.</p>
<p>This section is mainly intended for implementors. Authors writing web pages should generally be served well by the individual property descriptions, and do not need to read this section unless they have a deep-seated urge to understand arcane details of CSS layout.</p>
<p>To lay out a flexbox and its contents, follow these steps:</p>
<ol>
<li>Re-order the flexbox items according to their 'flex-order'. The items with the lowest (most negative) 'flex-order' values are first in the ordering. If multiple items share a 'flex-order' value, they're ordered by document order. This affects the order in which the flexbox items generate boxes in the box-tree, and how the rest of this algorithm deals with the items.</li>
<li>
<p>Resolve the width and height of every flexbox item into an absolute length. If the width or height is already an absolute length (like ''50px'') or was transformed into an absolute length at computed-value time (like ''10em''), skip the rest of this step for that dimension.</p>
<p>Otherwise, determining the size requires a layout to transform it into a pair of absolute lengths. For the purpose of this step:</p>
<ul>
<li>Treat <i>block flexboxes</i> as being ''display:block'' and <i>inline flexboxes</i> as being ''display:inline-block''.</li>
<li>Treat the flexbox and every flexbox item as establishing a block formatting context.</li>
<li>If the main size of a flexbox item is flexible, treat it as the preferred size of the flexible length. <strong>Do not apply min/max-width/height constraints to the preferred widths/heights of flexible lengths - those constraints are handled elsewhere in this algorithm, and doing so will produce incorrect results.</strong> Do apply those constraints to non-flexible lengths, as normal.</li>
<li>If the main size of a flexbox item is 'auto', or is being treated as 'auto' due to the above step, treat it as 'fit-content'. <span class='note'>IIRC, this is the name we expect to give to the "shrinkwrap" behavior.</span>.</li>
</ul>
<p>For each flexbox item:</p>
<ol>
<li>Ignore all other flexbox items (do layout as if the flexbox had only a single child - the flexbox item in question). Using normal block layout (and the preceding additional assumptions), resolve the width and height of the flexbox item.</li>
</ol>
<p>Each flexbox item now has a hypothetical width and hypothetical height.</p>
</li>
<li>
<p>If the flexbox is single-line, collect all the flexbox items into a single flexbox line.</p>
<p>If the flexbox is multi-line, group the flexbox items into multiple lines:</p>
<ol>
<li>Determine the flexbox's available space by finding the size of the flexbox's parent's content box in the flexbox's main axis, and subtracting the size of the flexbox's margins, borders, and padding in the main axis.</li>
<li>Collect as many consecutive flexbox items as possible, starting from the first item, while keeping the sum of their main size smaller than the flexbox's available space. The items so collected form a single flexbox line.</li>
<li>Repeat the previous step, starting each time from the first flexbox item not yet collected into a flexbox line, until all flexbox items have been collected into flexbox lines.</li>
</ol>
</li>
<li>
<p>Find the actual main size of the flexbox, per the standard rules for blocks (for block flexboxes) or inline-blocks (for inline flexboxes).</p>
<p>The min-content, max-content, or fit-content size of a flexbox in the main axis can be determined by, for each flexbox line, summing the min-content, max-content, or fit-content (as appropriate) sizes in the main axis of the flexbox items, and then returning the maximum of these sums.</p>
</li>
<li>For each flexbox line, <i>resolve the flexible lengths</i> of the items contained within it. All flexbox items now have a final main size. Update each item's hypothetical cross size based on this main size.</li>
<li>For each flexbox line, align the flexbox items per 'flex-pack'. The leftover free-space for each line is calculated by subtracting the sum of the main sizes of the flexbox items on the line from the main size of the flexbox's content box.</li>
<li>Calculate the cross size of each flexbox line. For each flexbox line:
<ol>
<li>If the flexbox is single-line and has a definite cross size, the single flexbox line's cross size is the cross size of the flexbox's content box. End this step of the algorithm.</li>
<li>Collect all the flexbox items with a 'flex-item-align' of ''start'', ''end'', ''center
7ED7
039;', or ''stretch'', or a value of ''baseline'' and a cross axis parallel to their inline axis, and find the maximum of the cross sizes of their margin boxes.</li>
<li>Collect all the flexbox items with a 'flex-item-align' of ''baseline'' and a cross axis perpendicular to their inline axis. Find the maximum of the distances from their baseline to the cross-start edge of their margin box, and the maximum of the distances from their baseline to the cross-end edge of their margin box. Sum these two values.</li>
<li>The cross size of the flexbox line is the larger of the numbers found in the previous two steps.</li>
</ol>
<p>If the flexbox doesn't have a definite cross size, the cross size of its content box is the sum of the cross sizes of all of its lines.</p>
</li>
<li>Determine the final cross size of each flexbox item. For each flexbox item, if it has ''flex-item-align:stretch'', its final cross size is the cross size of its flexbox line. For all other flexbox items, its final cross size is its hypothetical cross size.</li>
<li>For each flexbox line, align the flexbox items per 'flex-align'.</li>
<li>Align the flexbox lines per 'flex-item-align'. The leftover free-space is calculated by subtracting the sum of the flexbox line's cross sizes from the cross size of the flexbox's content box.</li>
</ol>
<p>To <dfn>resolve the flexible lengths</dfn> of the items within a flexbox line:</p>
<ol>
<li>If this is the first time this step is reached in the current invocation of this algorithm, <i>distribute the free space</i> in the flexbox line, then <i>fix max size violations</i>, then <i>fix min size violations</i>.</li>
<li>If the current set of items violating a min size constraint is different from the set in the previous iteration, remove the marks denoting any element as being in violation of a max size constraint, <i>distribute the free space</i>, then <i>fix max size violations</i>.</li>
<li>If the current set of items violating a max size constraint is different from the set in the previous iteration, remove the marks denoting any element as being in violation of a min size constraint, <i>distribute the free space</i>, then <i>fix min size violations</i>.</li>
<li>If either of the previous two steps changed the set of items violating their constraints, restart this algorithm.
<li>Otherwise, the used value of the main size property of every flexible item is the item's preferred size.</li>
</ol>
<p class='issue'>I'm pretty sure the previous algorithm is guaranteed to terminate. I'm not absolutely certain, though. Help? (It's clear that in practice, it will almost always terminate in one or two iterations.)</p>
<p>To <dfn>distribute the free space</dfn> in a flexbox line:</p>
<ol>
<li>If the preferred size of a flexible length was changed from a previous invocation of this algorithm, reset it to its specified value.</li>
<li>Find the available free space. Sum the main sizes of the margin boxes of every flexbox item in the line, treating flexible lengths as their preferred size, and then subtract that from the main size of the flexbox's content box. This value is the available free space. It may be zero, positive, or negative.</li>
<li>
<dt>If the available free space is zero:</dt>
<dd>Do nothing.</dd>
<dt>If the available free space is positive:</dt>
<dd>Calculate the total flexibility by summing the positive flexibility of every flexible length in the line. For each flexible length, increment its preferred size by a fraction of the available free space equal to its positive flexibility divided by the total flexibility.</dd>
<dt>If the available free space is negative:</dt>
<dd>Calculate the total flexibility by summing the negative flexibility of every flexible length in the line. For each flexible length, increment its preferred size by a fraction of the available free space equal to its negative flexibility divided by the total flexibility. <span class='note'>Note: due to the signs involved, incrementing will decrease the preferred size.)</span></dd>
</li>
</ol>
<p>To <dfn title="fix max size violations|fix min size violations">fix size constraint violations</dfn> in a flexbox line:</p>
<ol>
<li>If none of the items on the line violate their min or max size constraints (as appropriate), exit this algorithm.</li>
<li>Otherwise, for every item in violation of a min or max size constraint (as appropriate), mark them as being in violation of that constraint. While an item is so marked, treat it as if it were inflexible, with its main size property set to its min or max size constraint, as appropriate.</li>
<li><i>Distribute the free space</i>, then restart this algorithm.</li>
</ol>
<h2 id="pagination">
Page breaks in flexbox</h2>
<p class="issue">TODO: define how flexbox should break on pages, columns, etc. This may or may not be normative until there is more than one implementation.</p>